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.
Please try this,
sed -nf /tmp/OpenIp.sed /tmp/YourInputFile
cat /tmp/OpenIp.sed
/Interesting/{
N
N
/open/{
P
}
}
Thanks
Nagarajan G
awk '/Interesting/{r[NR+2]=$4}/open/{print r[NR]}' file | tr -d :
should work.
Or:
awk '/^Interesting/{ip=$4}/open/{print ip}' FS="[ :]" filename
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 ?
Yes,
but the implementation depends on the shell you're using.
Some examples:
[bash]
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]
$ # 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]
% 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]
$ set -- `awk '/^Interesting/{ip=$4}/open/{print ip}' FS="[ :]" file`
$ echo $1
172.29.143.221
$ echo $2
172.29.143.243