Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested command line functions

Status
Not open for further replies.

Cybex1

Technical User
Sep 3, 2011
33
0
0
US
I am trying to keep this to a one-liner... How can I take the IP addresses as the are looping through and push them into another command? I have this part already.

Bash:
netstat -antu | awk '$5 ~ /[0-9]:/{split($5, a, ":"); ips[a[1]]++} END {for (ip in ips) print ips[ip], ip | "sort -k1 -nr"}'

I am am trying to also incorporate a "geoiplookup" for each of the IP's into the loop.

I am trying to get the output to look like this:

6 168.123.123.123 DE, Germany
5 134.23.43.56 US, United States
1 45.65.7.32 US, United States


The closest I have gotten so far will run the IP's but the variable of the IP is lost and can not be written out properly;

Bash:
netstat -antu | awk '$5 ~ /[0-9]:/{split($5, a, ":"); ips[a[1]]++} END {for (ip in ips) print ip | "sort -k1 -nr"}'| xargs -I{} geoiplookup {}

Thanks!
 
Try this:
Code:
netstat -antu | awk '$5 ~ /[0-9]:/{split($5, a, ":"); ips[a[1]]++} END {for (ip in ips) print ip}' | sort -k1 -nr | xargs -I^ geoiplookup ^
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thanks LKBrwnDBA,

I was able to get to that point as well but the problem is that the output does not show the IP related to the geoip return. So I am not able to correlate 123.23.43.125 to Germany. All I see in the output is Germany and then if I want to know which IP was the one that points back to Germany I would have to run them one-by-one. See the problem?

I was wondering if the geoiplookup could be moved into the awk statement and executed as a nested command with the output written in a format as I drafted above.
 
Unfortunately I do not have the geoiplookup program installed and cannot test it.
But if you could explain the input parameters and expected output I may be able to simulate it...
[noevil]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
No prob...found the definition, but still do not have the program...
Code:
geoiplookup(1) - Linux man page
Name
geoiplookup - look up country using IP Address or hostname

Synopsis
geoiplookup [-d directory] [-f filename] [-v] <ipaddress|hostname>

Description
geoiplookup uses the GeoIP library and database to find the Country that an IP address or hostname originates from.
:p



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 

Got it:
Code:
netstat -antu | awk '$5 ~ /[0-9]:/{split($5, a, ":"); ips[a[1]]++} END {for (ip in ips) print ip}' | sort -k1 -nr | xargs -I^ echo "^ `geoiplookup` ^"
[pipe]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Ooops, typo fixed:
Code:
netstat -antu | awk '$5 ~ /[0-9]:/{split($5, a, ":"); ips[a[1]]++} END {for (ip in ips) print ip}' | sort -k1 -nr | xargs -I^ echo "^ `geoiplookup ^`"
[thumbsup2]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top