#!/bin/bash # Normally parameter is an IP address. wi=`whois $1` wilc=`echo "$wi" | wc -l` if [ ! "$wi" -o $wilc -lt 5 ]; then sleep 5 wi=`whois -h whois.arin.net $1` fi geoi=`curl -s ipinfo.io/$1 | tr '\012' ' '` geoip=`echo "$geoi" | sed -n -e 's/.*"ip":[ ]*"\([^"]*\)".*/GEO-IP: \1/p'` geohostname=`echo "$geoi" | sed -n -e 's/.*"hostname":[ ]*"\([^"]*\)".*/GEO-HOSTNAME: \1/p'` geocity=`echo "$geoi" | sed -n -e 's/.*"city":[ ]*"\([^"]*\)".*/GEO-CITY: \1/p'` georegion=`echo "$geoi" | sed -n -e 's/.*"region":[ ]*"\([^"]*\)".*/GEO-REGION: \1/p'` geocountry=`echo "$geoi" | sed -n -e 's/.*"country":[ ]*"\([^"]*\)".*/GEO-COUNTRY: \1/p'` geoloc=`echo "$geoi" | sed -n -e 's/.*"loc":[ ]*"\([^"]*\)".*/GEO-LOC: \1/p'` geoorg=`echo "$geoi" | sed -n -e 's/.*"org":[ ]*"\([^"]*\)".*/GEO-ORG: \1/p'` gi="$geoip $geohostname $geocity $georegion $geocountry $geoloc $geoorg " whoisout=`(echo "whois $1"; echo "$wi"; echo "$gi") | gawk ' function range2cidr(ipStart, ipEnd, bits, mask, newip) { bits = 1 mask = 1 result = "" while (bits < 32) { newip = or(ipStart, mask) if ((newip>ipEnd) || ((lshift(rshift(ipStart,bits),bits)) != ipStart)) { bits-- mask = rshift(mask,1) break } bits++ mask = lshift(mask,1)+1 } newip = or(ipStart, mask) bits = 32 - bits result = result dec2ip(ipStart) "/" bits if (newip < ipEnd) result = result "," range2cidr(newip + 1, ipEnd) return result } # convert dotted quads to long decimal ip # int ip2dec("192.168.0.15") # function ip2dec(ip, slice) { split(ip, slice, ".") return (slice[1] * 2^24) + (slice[2] * 2^16) + (slice[3] * 2^8) + slice[4] } # convert decimal long ip to dotted quads # str dec2ip(1171259392) # function dec2ip(dec, ip, quad) { for (i=3; i>=1; i--) { quad = 256^i ip = ip int(dec/quad) "." dec = dec%quad } return ip dec } # convert decimal ip to binary # str dec2binary(1171259392) # function dec2binary(dec, bin) { while (dec>0) { bin = dec%2 bin dec = int(dec/2) } return bin } # Convert binary ip to decimal # int binary2dec("1000101110100000000010011001000") # function binary2dec(bin, slice, l, dec) { split(bin, slice, "") l = length(bin) for (i=l; i>0; i--) { dec += slice[i] * 2^(l-i) } return dec } # convert dotted quad ip to binary # str ip2binary("192.168.0.15") # function ip2binary(ip) { return dec2binary(ip2dec(ip)) } # count the number of ips in a dotted quad ip range # int countIp ("192.168.0.0" ,"192.168.1.255") + 1 # function countQuadIp(ipStart, ipEnd) { return (ip2dec(ipEnd) - ip2dec(ipStart)) } # count the number of ips in a CIDR block # int countCidrIp ("192.168.0.0/12") # function countCidrIp (cidr) { sub(/.+\//, "", cidr) return 2^(32-cidr) } BEGIN { ci=0; di=0; ai=0; lasttype=""; ip=""; city=""; state=""; postalcode=""; codetocountry["AF"]="Afghanistan"; codetocountry["AL"]="Albania"; codetocountry["DZ"]="Algeria"; codetocountry["AS"]="American Samoa"; codetocountry["AD"]="Andorra"; codetocountry["AO"]="Angola"; codetocountry["AI"]="Anguilla"; codetocountry["AQ"]="Antarctica"; codetocountry["AG"]="Antigua and Barbuda"; codetocountry["AR"]="Argentina"; codetocountry["AM"]="Armenia"; codetocountry["AW"]="Aruba"; codetocountry["AU"]="Australia"; codetocountry["AT"]="Austria"; codetocountry["AZ"]="Azerbaijan"; codetocountry["BS"]="Bahamas"; codetocountry["BH"]="Bahrain"; codetocountry["BD"]="Bangladesh"; codetocountry["BB"]="Barbados"; codetocountry["BY"]="Belarus"; codetocountry["BE"]="Belgium"; codetocountry["BZ"]="Belize"; codetocountry["BJ"]="Benin"; codetocountry["BM"]="Bermuda"; codetocountry["BT"]="Bhutan"; codetocountry["BO"]="Bolivia"; codetocountry["BA"]="Bosnia and Herzegovina"; codetocountry["BW"]="Botswana"; codetocountry["BV"]="Bouvet Island"; codetocountry["BR"]="Brazil"; codetocountry["BQ"]="British Antarctic Territory"; codetocountry["IO"]="British Indian Ocean Territory"; codetocountry["VG"]="British Virgin Islands"; codetocountry["BN"]="Brunei"; codetocountry["BG"]="Bulgaria"; codetocountry["BF"]="Burkina Faso"; codetocountry["BI"]="Burundi"; codetocountry["KH"]="Cambodia"; codetocountry["CM"]="Cameroon"; codetocountry["CA"]="Canada"; codetocountry["CT"]="Canton and Enderbury Islands"; codetocountry["CV"]="Cape Verde"; codetocountry["KY"]="Cayman Islands"; codetocountry["CF"]="Central African Republic"; codetocountry["TD"]="Chad"; codetocountry["CL"]="Chile"; codetocountry["CN"]="China"; codetocountry["CX"]="Christmas Island"; codetocountry["CC"]="Cocos [Keeling] Islands"; codetocountry["CO"]="Colombia"; codetocountry["KM"]="Comoros"; codetocountry["CG"]="Congo - Brazzaville"; codetocountry["CD"]="Congo - Kinshasa"; codetocountry["CK"]="Cook Islands"; codetocountry["CR"]="Costa Rica"; codetocountry["HR"]="Croatia"; codetocountry["CU"]="Cuba"; codetocountry["CY"]="Cyprus"; codetocountry["CZ"]="Czech Republic"; codetocountry["CI"]="Côte d’Ivoire"; codetocountry["DK"]="Denmark"; codetocountry["DJ"]="Djibouti"; codetocountry["DM"]="Dominica"; codetocountry["DO"]="Dominican Republic"; codetocountry["NQ"]="Dronning Maud Land"; codetocountry["DD"]="East Germany"; codetocountry["EC"]="Ecuador"; codetocountry["EG"]="Egypt"; codetocountry["SV"]="El Salvador"; codetocountry["GQ"]="Equatorial Guinea"; codetocountry["ER"]="Eritrea"; codetocountry["EE"]="Estonia"; codetocountry["ET"]="Ethiopia"; codetocountry["FK"]="Falkland Islands"; codetocountry["FO"]="Faroe Islands"; codetocountry["FJ"]="Fiji"; codetocountry["FI"]="Finland"; codetocountry["FR"]="France"; codetocountry["GF"]="French Guiana"; codetocountry["PF"]="French Polynesia"; codetocountry["TF"]="French Southern Territories"; codetocountry["FQ"]="French Southern and Antarctic Territories"; codetocountry["GA"]="Gabon"; codetocountry["GM"]="Gambia"; codetocountry["GE"]="Georgia"; codetocountry["DE"]="Germany"; codetocountry["GH"]="Ghana"; codetocountry["GI"]="Gibraltar"; codetocountry["GR"]="Greece"; codetocountry["GL"]="Greenland"; codetocountry["GD"]="Grenada"; codetocountry["GP"]="Guadeloupe"; codetocountry["GU"]="Guam"; codetocountry["GT"]="Guatemala"; codetocountry["GG"]="Guernsey"; codetocountry["GN"]="Guinea"; codetocountry["GW"]="Guinea-Bissau"; codetocountry["GY"]="Guyana"; codetocountry["HT"]="Haiti"; codetocountry["HM"]="Heard Island and McDonald Islands"; codetocountry["HN"]="Honduras"; codetocountry["HK"]="Hong Kong SAR China"; codetocountry["HU"]="Hungary"; codetocountry["IS"]="Iceland"; codetocountry["IN"]="India"; codetocountry["ID"]="Indonesia"; codetocountry["IR"]="Iran"; codetocountry["IQ"]="Iraq"; codetocountry["IE"]="Ireland"; codetocountry["IM"]="Isle of Man"; codetocountry["IL"]="Israel"; codetocountry["IT"]="Italy"; codetocountry["JM"]="Jamaica"; codetocountry["JP"]="Japan"; codetocountry["JE"]="Jersey"; codetocountry["JT"]="Johnston Island"; codetocountry["JO"]="Jordan"; codetocountry["KZ"]="Kazakhstan"; codetocountry["KE"]="Kenya"; codetocountry["KI"]="Kiribati"; codetocountry["KW"]="Kuwait"; codetocountry["KG"]="Kyrgyzstan"; codetocountry["LA"]="Laos"; codetocountry["LV"]="Latvia"; codetocountry["LB"]="Lebanon"; codetocountry["LS"]="Lesotho"; codetocountry["LR"]="Liberia"; codetocountry["LY"]="Libya"; codetocountry["LI"]="Liechtenstein"; codetocountry["LT"]="Lithuania"; codetocountry["LU"]="Luxembourg"; codetocountry["MO"]="Macau SAR China"; codetocountry["MK"]="Macedonia"; codetocountry["MG"]="Madagascar"; codetocountry["MW"]="Malawi"; codetocountry["MY"]="Malaysia"; codetocountry["MV"]="Maldives"; codetocountry["ML"]="Mali"; codetocountry["MT"]="Malta"; codetocountry["MH"]="Marshall Islands"; codetocountry["MQ"]="Martinique"; codetocountry["MR"]="Mauritania"; codetocountry["MU"]="Mauritius"; codetocountry["YT"]="Mayotte"; codetocountry["FX"]="Metropolitan France"; codetocountry["MX"]="Mexico"; codetocountry["FM"]="Micronesia"; codetocountry["MI"]="Midway Islands"; codetocountry["MD"]="Moldova"; codetocountry["MC"]="Monaco"; codetocountry["MN"]="Mongolia"; codetocountry["ME"]="Montenegro"; codetocountry["MS"]="Montserrat"; codetocountry["MA"]="Morocco"; codetocountry["MZ"]="Mozambique"; codetocountry["MM"]="Myanmar [Burma]"; codetocountry["NA"]="Namibia"; codetocountry["NR"]="Nauru"; codetocountry["NP"]="Nepal"; codetocountry["NL"]="Netherlands"; codetocountry["AN"]="Netherlands Antilles"; codetocountry["NT"]="Neutral Zone"; codetocountry["NC"]="New Caledonia"; codetocountry["NZ"]="New Zealand"; codetocountry["NI"]="Nicaragua"; codetocountry["NE"]="Niger"; codetocountry["NG"]="Nigeria"; codetocountry["NU"]="Niue"; codetocountry["NF"]="Norfolk Island"; codetocountry["KP"]="North Korea"; codetocountry["VD"]="North Vietnam"; codetocountry["MP"]="Northern Mariana Islands"; codetocountry["NO"]="Norway"; codetocountry["OM"]="Oman"; codetocountry["PC"]="Pacific Islands Trust Territory"; codetocountry["PK"]="Pakistan"; codetocountry["PW"]="Palau"; codetocountry["PS"]="Palestinian Territories"; codetocountry["PA"]="Panama"; codetocountry["PZ"]="Panama Canal Zone"; codetocountry["PG"]="Papua New Guinea"; codetocountry["PY"]="Paraguay"; codetocountry["YD"]="Peoples Democratic Republic of Yemen"; codetocountry["PE"]="Peru"; codetocountry["PH"]="Philippines"; codetocountry["PN"]="Pitcairn Islands"; codetocountry["PL"]="Poland"; codetocountry["PT"]="Portugal"; codetocountry["PR"]="Puerto Rico"; codetocountry["QA"]="Qatar"; codetocountry["RO"]="Romania"; codetocountry["RU"]="Russia"; codetocountry["RW"]="Rwanda"; codetocountry["RE"]="Réunion"; codetocountry["BL"]="Saint Barthélemy"; codetocountry["SH"]="Saint Helena"; codetocountry["KN"]="Saint Kitts and Nevis"; codetocountry["LC"]="Saint Lucia"; codetocountry["MF"]="Saint Martin"; codetocountry["PM"]="Saint Pierre and Miquelon"; codetocountry["VC"]="Saint Vincent and the Grenadines"; codetocountry["WS"]="Samoa"; codetocountry["SM"]="San Marino"; codetocountry["SA"]="Saudi Arabia"; codetocountry["SN"]="Senegal"; codetocountry["RS"]="Serbia"; codetocountry["CS"]="Serbia and Montenegro"; codetocountry["SC"]="Seychelles"; codetocountry["SL"]="Sierra Leone"; codetocountry["SG"]="Singapore"; codetocountry["SK"]="Slovakia"; codetocountry["SI"]="Slovenia"; codetocountry["SB"]="Solomon Islands"; codetocountry["SO"]="Somalia"; codetocountry["ZA"]="South Africa"; codetocountry["GS"]="South Georgia and the South Sandwich Islands"; codetocountry["KR"]="South Korea"; codetocountry["ES"]="Spain"; codetocountry["LK"]="Sri Lanka"; codetocountry["SD"]="Sudan"; codetocountry["SR"]="Suriname"; codetocountry["SJ"]="Svalbard and Jan Mayen"; codetocountry["SZ"]="Swaziland"; codetocountry["SE"]="Sweden"; codetocountry["CH"]="Switzerland"; codetocountry["SY"]="Syria"; codetocountry["ST"]="São Tomé and Príncipe"; codetocountry["TW"]="Taiwan"; codetocountry["TJ"]="Tajikistan"; codetocountry["TZ"]="Tanzania"; codetocountry["TH"]="Thailand"; codetocountry["TL"]="Timor-Leste"; codetocountry["TG"]="Togo"; codetocountry["TK"]="Tokelau"; codetocountry["TO"]="Tonga"; codetocountry["TT"]="Trinidad and Tobago"; codetocountry["TN"]="Tunisia"; codetocountry["TR"]="Turkey"; codetocountry["TM"]="Turkmenistan"; codetocountry["TC"]="Turks and Caicos Islands"; codetocountry["TV"]="Tuvalu"; codetocountry["UM"]="U.S. Minor Outlying Islands"; codetocountry["PU"]="U.S. Miscellaneous Pacific Islands"; codetocountry["VI"]="U.S. Virgin Islands"; codetocountry["UG"]="Uganda"; codetocountry["UA"]="Ukraine"; codetocountry["SU"]="Union of Soviet Socialist Republics"; codetocountry["AE"]="United Arab Emirates"; codetocountry["GB"]="United Kingdom"; codetocountry["US"]="United States"; codetocountry["ZZ"]="Unknown or Invalid Region"; codetocountry["UY"]="Uruguay"; codetocountry["UZ"]="Uzbekistan"; codetocountry["VU"]="Vanuatu"; codetocountry["VA"]="Vatican City"; codetocountry["VE"]="Venezuela"; codetocountry["VN"]="Vietnam"; codetocountry["WK"]="Wake Island"; codetocountry["WF"]="Wallis and Futuna"; codetocountry["EH"]="Western Sahara"; codetocountry["YE"]="Yemen"; codetocountry["ZM"]="Zambia"; codetocountry["ZW"]="Zimbabwe"; codetocountry["AX"]="Åland Islands"; } /^whois / { ai=0; ci=0; di=0; ip=$2; netname=""; netrange=""; ipstart=""; ipend=""; city=""; state=""; postalcode=""; ipstart=""; ipend=""; cidr="" } /^IPv4 Address[ ]*:/ {ipstart=$4;ipend=$6;netrange=ipstart " - " ipend; patsplit(netrange,range,"([^ -]+)");ipstart=range[1];ipend=range[2];if (ipend != "") {cidr=range2cidr(ip2dec(ipstart),ip2dec(ipend));} else {cidr=ipstart;} } /[(]NET-[0-9].*[0-9] - [0-9][0-9.]*$/ {ipstart=$(NF-2);ipend=$NF;netrange=ipstart " - " ipend; patsplit(netrange,range,"([^ -]+)");ipstart=range[1];ipend=range[2];if (ipend != "") {cidr=range2cidr(ip2dec(ipstart),ip2dec(ipend));} else {cidr=ipstart;} } /^[Nn]et[Rr]ange:/ { $1="";netrange=substr($0,2); patsplit(netrange,range,"([^ -]+)");ipstart=range[1];ipend=range[2];if (ipend != "") {cidr=range2cidr(ip2dec(ipstart),ip2dec(ipend));} else {cidr=ipstart;} } /^inetnum:/ { $1="";netrange=substr($0,2); patsplit(netrange,range,"([^ -]+)");ipstart=range[1];ipend=range[2];if (ipend != "") {cidr=range2cidr(ip2dec(ipstart),ip2dec(ipend));} else {cidr=ipstart;} } /^[Nn]et[Nn]ame:/ { $1="";netname=substr($0,2); } /^[O]rg[Nn]ame:/ { $1="";orgname=substr($0,2); } /^[O]rganization [Nn]ame[ ][ ]*: / { $1="";$2="";$3="";orgname=substr($0,4); } /^[Cc]ity:/ {$1="";city=substr($0,2); } /^[Ss]tate[Pp]rov:/ {$1="";state=substr($0,2); } /^[Pp]ostal[Cc]ode:/ {$1="";postalcode=substr($0,2); } /^[Cc]ountry:/ { $1="";country=substr($0,2);countryname=codetocountry[country]; } /^GEO-IP:/ {$1="";geoip=substr($0,2); } /^GEO-HOSTNAME:/ {$1="";geohostname=substr($0,2); } /^GEO-CITY:/ {$1="";geocity=substr($0,2); } /^GEO-REGION:/ {$1="";georegion=substr($0,2); } /^GEO-COUNTRY:/ {$1="";geocountry=substr($0,2);geocountryname=codetocountry[geocountry]; } /^GEO-LOC:/ {$1="";geoloc=substr($0,2); } /^GEO-ORG:/ {$1="";geoorg=substr($0,2); } /^[Cc]omment:/ { if (comment[0] != "" && lasttype == "comment") { $1=""; comment[ci]=substr($0,2); ci++; lasttype="comment"; } else if (ci == 0) { $1=""; comment[ci]=substr($0,2); ci++; lasttype="comment"; } else { comment[ci]="-"; ci++; $1=""; comment[ci]=substr($0,2); ci++; lasttype="comment"; } next; } /^[Zz]ip Code[ ][ ]*: / { if (address[0] != "" && lasttype == "address") { $1=""; $2=""; $3=""; address[ai]=substr($0,4); ai++; lasttype="address"; } else if (ai == 0) { $1=""; $2=""; $3=""; address[ai]=substr($0,4); ai++; lasttype="address"; } else { address[ai]="-"; ai++; $1=""; $2=""; $3=""; address[ai]=substr($0,4); ai++; lasttype="address"; } next; } /^[Aa]ddress[ ][ ]*: / { if (address[0] != "" && lasttype == "address") { $1=""; $2=""; address[ai]=substr($0,3); ai++; lasttype="address"; } else if (ai == 0) { $1=""; $2=""; address[ai]=substr($0,3); ai++; lasttype="address"; } else { address[ai]="-"; ai++; $1=""; $2=""; address[ai]=substr($0,3); ai++; lasttype="address"; } next; } /^[Aa]ddress:/ { if (address[0] != "" && lasttype == "address") { $1=""; address[ai]=substr($0,2); ai++; lasttype="address"; } else if (ai == 0) { $1=""; address[ai]=substr($0,2); ai++; lasttype="address"; } else { address[ai]="-"; ai++; $1=""; address[ai]=substr($0,2); ai++; lasttype="address"; } next; } /^[Dd]escr:/ { if (descr[0] != "" && lasttype == "descr") { $1=""; descr[di]=substr($0,2); di++; lasttype="descr"; } else if (di == 0) { $1=""; descr[di]=substr($0,2); di++; lasttype="descr"; } else { descr[di]="-"; di++; $1=""; descr[di]=substr($0,2); di++; lasttype="descr"; } next; } { lasttype=""; } END { if (ip != "" && netname !~ /PRIVATE-ADDRESS.*/) { printf("%s\t%s\t%s", ip, country,countryname); printf("\t%s",orgname); printf("\t%s",city); printf("\t%s",state); printf("\t%s",postalcode); printf("\t%s",netname); printf("\t%s",netrange); printf("\t%s",ipstart); printf("\t%s",ipend); printf("\t%s",cidr); printf("\t%s",geoip); printf("\t%s",geohostname); printf("\t%s",geocity); printf("\t%s",georegion); printf("\t%s",geocountry); printf("\t%s",geocountryname); printf("\t%s",geoloc); printf("\t%s",geoorg); printf("\t"); for (i=0;i