English 中文(简体)
Extract IP from netstat output
原标题:

The netstat output contains thing like...

tcp        0      0 0.0.0.0:80       221.126.149.99:51973    ESTABLISHED 23879/apache2
tcp        0      0 0.0.0.0:80        66.249.68.154:40883     ESTABLISHED 23899/apache2
tcp        0      0 0.0.0.0:80       66.249.68.81:41200      ESTABLISHED 23892/apache2
tcp        0      0 0.0.0.0:80       66.249.67.121:59355     ESTABLISHED 23905/apache2
tcp        0   4465 0.0.0.0:80       110.75.175.27:48139     ESTABLISHED 23901/apache2

I use this commands

netstat -anpt|grep apache2 |grep ESTABLISHED | awk -F "[ :]"  {print $4} 

I was not able to get the IP, any hints?

最佳回答

This will return a list of unique IP address you re connected too:

netstat -anpt | grep apache2 |grep ESTABLISHED | awk  { print $5 }  | cut -d: -f1 | sort -u

Well I think I need to change my glasses also =P

问题回答

You may try

netstat -anpt|awk  BEGIN {FS="[ :]+"};/ESTABLISHED/ && /apache/{print $6}   

For some reason I am counting 6 fields, while everybody else is counting 4 ... Should I buy new glasses? :)

HTH!

You re really close. You just need to change your field separator regular expression so that it s not considering a single whitespace or colon as the field separator:

netstat -anpt|grep apache2 |grep ESTABLISHED | awk -F "[ :]*"  {print $4} 
netstat -anpt | awk  /apache2/&&/ESTABLISHED/{sub(/:*/,"",$4);print $4}   
 netstat -ant | grep 80 | wc -l




相关问题
KornShell- Creating a fixed width text file

I need to create a simple fixed width text file in KornShell (ksh). My current attempt using printf to pad the string isn t working out very well. What s the shortest, cleanest way to create a fixed ...

unix find command

how to use the find command to find files/directories which are not matching the pattern. for eg: find <some options > -name "dontfile.txt" should give me output of all the find whose file ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

1. 露台式照像

在Windows XP中,没有一时出现指挥时,是否有办法操作一种灰色文字? 我常常需要把“善待”(工作)与“灰色”同起来,即使我的文字没有产出,......

热门标签