INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Nested command line functions

Nested command line functions

(OP)
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.

CODE --> 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;

CODE --> 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!

RE: Nested command line functions

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

RE: Nested command line functions

(OP)
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.

RE: Nested command line functions

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

RE: Nested command line functions

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

RE: Nested command line functions


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

RE: Nested command line functions

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close