0

Using grep and cut I have returned the IP and ports of the nmap scan result which looks like this:

192.168.1.221
80
443
192.168.1.223
25

Desired output:

192.168.1.221:80
192.168.1.221:443
192.168.1.223:25
4
  • 1
    It would be useful to see the shape of your starting point.
    – roaima
    Mar 5 '19 at 8:46
  • Hey roaima you can find my starting point here initial post
    – KIllua
    Mar 5 '19 at 8:52
  • Posting on multiple sites is off-topc for U&L. If you want to delete your original post then then one becomes on-topic.
    – roaima
    Mar 5 '19 at 9:01
  • 1
    duly noted roaima
    – KIllua
    Mar 5 '19 at 9:03
2
awk '/\./{ip = $0; next}; {print ip":"$0}'

should be enough on that input.

Note that nmap has options to output in formats suitable for programmatic processing like xml.

1
0

You can do something as the following using awk:

awk '$1 ~ /[0-9]\.[0-9]+/{ip=$1; val=0; next } /^[0-9]+$/{port=$1; if(!(val++)); print ip":"port}' infile
0
0

One solution with a bash script:

for i in $(<input); do [ ${#i} -le 3 ] && printf '%s:%s\n' $ip $i || ip=$i; done

This saves the ip address in a variable if the input string length is greater than 3 characters and prints ip:port otherwise. Replace input with your input file. (You can choose a greater value to match if your port numbers have more than 3 characters)

Another one:

while read i; do [ ${i/.} != $i ] && ip=$i || printf "%s:%s\n" $ip $i; done <input

This assigns the ip address to variable ip if it contains a dot and prints ip:port otherwise.

0

I have done by below method

sed -n '/^[0-9]\{3\}\./,+1p' filename| sed "N;s/\n/:/g"

output

192.168.1.221:80
192.168.1.223:25
2
  • 1
    The output is missing port 443
    – RalfFriedl
    Mar 5 '19 at 17:38
  • Will check and update Mar 5 '19 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.