The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-17-2008
Registered User
 

Join Date: Jan 2008
Posts: 25
help using awk

Hi I have an output of the nmap command scan for port 22 as below

Interesting ports on 172.29.143.221:
PORT STATE SERVICE
22/tcp open ssh

Interesting ports on 172.29.143.240:
PORT STATE SERVICE
22/tcp closed ssh

Interesting ports on 172.29.143.243:
PORT STATE SERVICE
22/tcp open ssh

I would like to print out the ip address with STATE "open" only.

Thank.
Reply With Quote
Forum Sponsor
  #2  
Old 01-17-2008
Registered User
 

Join Date: Mar 2007
Location: Chennai
Posts: 222
Please try this,

Code:
sed -nf /tmp/OpenIp.sed /tmp/YourInputFile
cat /tmp/OpenIp.sed
Code:
/Interesting/{
N
N
/open/{
P
}
}
Thanks
Nagarajan G

Last edited by ennstate; 01-17-2008 at 03:52 AM. Reason: Incorrect filename
Reply With Quote
  #3  
Old 01-17-2008
Tytalus's Avatar
Registered User
 

Join Date: Jun 2003
Location: Scotland
Posts: 285
Code:
awk '/Interesting/{r[NR+2]=$4}/open/{print r[NR]}' file | tr -d :
should work.
Reply With Quote
  #4  
Old 01-17-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,855
Or:

Code:
awk '/^Interesting/{ip=$4}/open/{print ip}' FS="[ :]" filename
Reply With Quote
  #5  
Old 01-17-2008
Registered User
 

Join Date: Jan 2008
Posts: 25
Thank you to radoulov. It works well just one single command.

Is it possible if i can take this output and insert this into an array?
If so, would you please advise the code ?
Reply With Quote
  #6  
Old 01-18-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,855
Yes,
but the implementation depends on the shell you're using.
Some examples:

[bash]

Code:
bash 3.2.25(1)$ cat file
Interesting ports on 172.29.143.221:
PORT STATE SERVICE
22/tcp open ssh

Interesting ports on 172.29.143.240:
PORT STATE SERVICE
22/tcp closed ssh

Interesting ports on 172.29.143.243:
PORT STATE SERVICE
22/tcp open ssh
bash 3.2.25(1)$ a=($(awk '/^Interesting/{ip=$4}/open/{print ip}' FS="[ :]" file))
bash 3.2.25(1)$ echo ${a[0]}
172.29.143.221
bash 3.2.25(1)$ echo ${a[1]}
172.29.143.243
[ksh]

Code:
$ # ksh88 doesn't support this  syntax 
$ a=($(awk '/^Interesting/{ip=$4}/open/{print ip}' FS="[ :]" file))
$ print ${a[0]}
172.29.143.221
$ # or
$ set -A a $(awk '/^Interesting/{ip=$4}/open/{print ip}' FS="[ :]" file)
$ print ${a[1]}
172.29.143.243
[zsh]

Code:
% a=($(awk '/^Interesting/{ip=$4}/open/{print ip}' FS="[ :]" file)) 
% print $a[1]
172.29.143.221
% print $a[2]
172.29.143.243
Or just:

[sh]

Code:
$ set -- `awk '/^Interesting/{ip=$4}/open/{print ip}' FS="[ :]" file`
$ echo $1
172.29.143.221
$ echo $2
172.29.143.243
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:33 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0