2

I am new to bash scripting and am trying to parse out information from a whois result, specifically, the orgname and city fields. Is the proper way to save the result in a string and then parse the string for the relevant information? like:

    RESULT=whois <ip>
    # parse RESULT

Any help would be greatly appreciated.

user1190650
  • 3,207
  • 6
  • 27
  • 34
  • To record the result: `RESULT=$( whois )` The syntax you give attempts to run the command `` with the value of RESULT set to "whois" in the environment. – William Pursell Nov 05 '12 at 18:56
  • 1
    You can start parsing even before recording with `RESULT=$(whois | awk '/OrgName/ {print $0} /City/ {print $0}')`, which will only store in your variable the information you want. – Vincent Nivoliers Nov 05 '12 at 18:58
  • Or even add `awk -F : '/OrgName/{print $2}' and so on, assuming the whois information is formatted the same way everywhere. Note: I just tried whois for a host that had neither an OrgName nor a City field, so you won't always be able to get this information. – amaurea Nov 05 '12 at 19:00

2 Answers2

3

Sadly, the whois output seems to be meant for humans, not machines, to read. Its format depends on which root domain you are interested in. For example, whois uio.no returns stuff like

NORID Handle...............: UIO2O-NORID
Type.......................: organization
Name.......................: UNIVERSITETET I OSLO
Id Type....................: organization_number
Id Number..................: 971035854
Registrar Handle...........: REG2-NORID
Post Address...............: Postboks 1059, Blindern

while whois tasvideos.org produces

Registrant ID:ACTR120531657
Registrant Name:Andres Delikat
Registrant Organization:tasvideos.org
Registrant Street1:5505G Creek Ridge Ln
Registrant Street2:
Registrant Street3:
Registrant City:Raleigh

This is just to show that parsing this stuff will depend on what you're looking at, and the solution I provide will not work in all cases. But the easiest way to extract this information is by calling awk and keeping its output. For the ".org" case, which is the one you probably want, it would be something like this:

info=$(whois $ip)
org=$(echo "$info" | awk -F : '$1=="Registrant Organization"{print $2}')
city=$(echo "$info" | awk -F : '$1=="Registrant City"{print $2}')
amaurea
  • 4,950
  • 26
  • 35
0

See this question: How to set a variable to the output from a command in Bash?

cmd="yourcommand"
string=$($cmd)
echo $string
Community
  • 1
  • 1
tommy.carstensen
  • 8,962
  • 15
  • 65
  • 108