LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-15-2017, 12:33 AM   #1
sknsk
LQ Newbie
 
Registered: Mar 2017
Posts: 12

Rep: Reputation: Disabled
How to scan IP range using nmap?


Scripting language : Bash Shell Script

I have to create one function in which read IP addresses one by one from one file (iplist.txt) and scan these IP using nmap. This scan IP's output is saved in output.txt file and parse output.txt file to filter only open ports with particular IP which is saved in parse.txt file.

format of parse.txt file:

Code:
ip             open port
x.x.x.x            x
2. My goal:
1. Find all ports open on a whole range and in one file.
2. Save only open ports with IP address in another file. Don't save filtered or closed ports in this file.

eg. Format of file:
IP_address Open_port

Code:
192.168.0.1         21
192.168.0.1         80
....
so I have write the script which scan IP range and save the whole output. I just want script of second option.
I have attached the script. Please help me if possible to implement second option. In second option, my script take open port/tcp and all other details but i want only ip address with open port as shown above file format.

code:

Code:
readfile()
{
while read -r line
do
 name="$line"
 echo "$name"
 conunt=$line
 nmap -oN output.txt -vv -T4 -f -iL iplist.txt $line1
done < iplist.txt
grep -i "open" output.txt > parse.txt 
}

readfile
output is:

iplist.txt
Code:
192.168.0.1
192.168.0.2
output.txt
Code:
Nmap scan report for 192.168.0.1
Host is up, received echo-reply ttl 128 (1.0s latency).
Scanned at 2017-03-14 05:47:17 EDT for 90s
Not shown: 985 closed ports
Reason: 985 resets
PORT      STATE    SERVICE        REASON
135/tcp   open     msrpc          syn-ack ttl 128
139/tcp   open     netbios-ssn    syn-ack ttl 128
445/tcp   open     microsoft-ds   syn-ack ttl 128
514/tcp   filtered shell          no-response
554/tcp   open     rtsp           syn-ack ttl 128
902/tcp   open     iss-realsecure syn-ack ttl 128
912/tcp   open     apex-mesh      syn-ack ttl 128
1025/tcp  open     NFS-or-IIS     syn-ack ttl 128
1026/tcp  open     LSA-or-nterm   syn-ack ttl 128
1027/tcp  open     IIS            syn-ack ttl 128
1028/tcp  open     unknown        syn-ack ttl 128
1029/tcp  open     ms-lsa         syn-ack ttl 128
2869/tcp  open     icslap         syn-ack ttl 128
5357/tcp  open     wsdapi         syn-ack ttl 128
10243/tcp open     unknown        syn-ack ttl 128

Nmap scan report for 192.168.0.2
Host is up, received reset ttl 128 (0.18s latency).
All 1000 scanned ports on 192.168.0.1 are filtered (914) or closed (86) because of 914 no-responses and 86 resets
parse.txt
Code:
135/tcp   open     msrpc          syn-ack ttl 128
139/tcp   open     netbios-ssn    syn-ack ttl 128
445/tcp   open     microsoft-ds   syn-ack ttl 128
514/tcp   filtered shell          no-response
554/tcp   open     rtsp           syn-ack ttl 128
902/tcp   open     iss-realsecure syn-ack ttl 128
912/tcp   open     apex-mesh      syn-ack ttl 128
1025/tcp  open     NFS-or-IIS     syn-ack ttl 128
1026/tcp  open     LSA-or-nterm   syn-ack ttl 128
1027/tcp  open     IIS            syn-ack ttl 128
1028/tcp  open     unknown        syn-ack ttl 128
1029/tcp  open     ms-lsa         syn-ack ttl 128
2869/tcp  open     icslap         syn-ack ttl 128
5357/tcp  open     wsdapi         syn-ack ttl 128
10243/tcp open     unknown        syn-ack ttl 128
----------------------------------------------------------------------------------------------------------------------

Expected output of parse.txt:
Code:
192.168.0.1     135
192.168.0.1     139
192.168.0.1     445
192.168.0.1     514
192.168.0.1     554
192.168.0.1     902
192.168.0.1     912
192.168.0.1     1025
192.168.0.1     1026
192.168.0.1     1027
192.168.0.1     1028
192.168.0.1     1029
192.168.0.1     2869
192.168.0.1     5357
192.168.0.1     10243

Last edited by sknsk; 03-15-2017 at 12:36 AM.
 
Old 03-15-2017, 03:18 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,292
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
It's pretty easy with awk.

Just a quick guess: You'd just look for /^Nmap scan report for/ and clear your value for the 1st column, then if /host down/ is not present use the 5th or 6th field to set the subsequent 1st column value. Then until that happens again, print out the port number when 'open' is found in the appropriate column.

Functions like sub() or gsub() can be used to tidy the fields.

How far can you get with awk now?
 
2 members found this post helpful.
Old 03-17-2017, 05:28 AM   #3
sknsk
LQ Newbie
 
Registered: Mar 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
How to scan IP range using nmap?

Hello,

I have tried using awk command but I didn't got expected answer yet. Can you give me code for this.
 
Old 03-17-2017, 05:30 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,292
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
The way the forum works is that you show what you have been trying and ask questions about it. So please show what you tried with awk and how far you were able to get with either your own approach or the guidance given already.
 
Old 03-17-2017, 01:53 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
This should be fairly easy with awk, one method already indicated by Turbocapitalist in post #2.

Alternatively, you might want to think of 'Nmap scan report...' as the record separator and each subsequent line as a field, then get the IP address from the first "field" and port numbers from /^[0-9]+/ of each following field (line).

But as already said, it is more helpful to everyone if you show us your own attempt as the starting point, then we can suggest and refine our advice based on that example.
 
Old 03-18-2017, 07:10 AM   #6
sknsk
LQ Newbie
 
Registered: Mar 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
How to scan IP range using nmap?

Error in filter.awk :

nmap_scan.sh

Code:
readfile()
{
while read -r line
do
 name="$line"
 echo "$name"
 conunt=$line
 nmap -oG output.txt -T4 -f -iL iplist.txt $line1
done < iplist.txt
awk -f filter.awk output.txt > parse.txt 
}

readfile
filter.awk

Code:
awk '/^Nmap scan report/{cHost=$5;}
       /open/ { split($1,a,"/"); result[cHost][a[1]]=""}
       END {
       for (i in result) {
         printf i;
         for (j in result[i])
           printf ",%s", j ;
         print ""} }' |
  sed -e 's/,/\t/'
Output-

output.txt-

Code:
# Nmap 7.01 scan initiated Sat Mar 15 06:27:08 2017 as: nmap -oG output.txt -T4 -f -iL iplist.txt
Host: 192.168.1.99 ()    Status: Up
Host: 192.168.1.99 ()    Ports: 135/open/tcp//msrpc///, 139/open/tcp//netbios-ssn///, 445/open/tcp//microsoft-ds///, 514/filtered/tcp//shell///, 554/open/tcp//rtsp///, 902/open/tcp//iss-realsecure///, 912/open/tcp//apex-mesh///, 1025/open/tcp//NFS-or-IIS///, 1026/open/tcp//LSA-or-nterm///, 1027/open/tcp//IIS///, 1028/open/tcp//unknown///, 1036/open/tcp//nsstp///, 2869/open/tcp//icslap///, 5357/open/tcp//wsdapi///, 10243/open/tcp//unknown///    Ignored State: closed (985)
Host: 192.168.0.1 ()    Status: Up
Host: 192.168.0.1 ()    Status: Up
Host: 192.168.0.101 ()    Status: Up
Host: 192.168.0.101 ()    Status: Up
# Nmap done at Sat Mar 15 06:29:02 2017 -- 3 IP addresses (3 hosts up) scanned in 113.19 seconds
Error-

Code:
awk: 2: unexpected character '''
awk: filter.awk: line 4: syntax error at or near [
awk: filter.awk: line 7: syntax error at or near [
awk: 9: unexpected character '''
 
Old 03-18-2017, 07:52 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,292
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
Ok. With that kind of input and the way you are starting awk, I'd try something like the pieces below. The Output Field Separator (OFS) can be set to a tab if you like. However, the following does not really use it because of the printf. Also, since the string "open" could possibly come in several contexts, it's best to not rely on it by itself and add in an extra check.

Code:
awk 'BEGIN {
    OFS="\t";
}

/^Host/{
    cHost=$2;
    if( $4 == "Ports:" && /\/open\// ){
        cPorts=$0;
        sub( /^.*Ports:/, "", cPorts );
        split( cPorts, a, "," );
        for( i in a ) {
            if( a[i] ~ /\/open\// ) {
                sub( /\/.*$/, "", a[i] );
                printf( "%s\t%s\n",cHost,a[i] );
            }
        }
    }
}

END {
    print "Done!";
}' output.txt > parse.txt
 
1 members found this post helpful.
Old 03-21-2017, 01:20 AM   #8
sknsk
LQ Newbie
 
Registered: Mar 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
How to scan IP range using nmap?

Thank you so much......it works.
 
Old 03-21-2017, 02:13 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,292
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
No worries. But more important than it working is that you understand how it works and what was changed in the awk script.

How familiar are the following?

sub()
split()
~
printf()

They're there in the awk manual.

Code:
man awk
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Nmap OS scan sagar666 Linux - Server 5 12-23-2013 01:09 PM
LXer: Learn how to use nmap, and nmap GUI, a great port scan tool LXer Syndicated Linux News 0 01-03-2008 09:10 AM
nmap scan sucram2g Linux - Security 2 03-18-2006 02:21 AM
Nmap with Idle scan Ephracis Linux - Security 0 12-10-2004 05:08 AM
nmap scan loganwva Linux - Security 5 02-25-2003 07:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration