Wednesday, May 28, 2014

Print out all the IPs in a Subnet Using Linux Command

In my particular case, I need to print out every IPs in a subnet, I used 'nmap' command together with option '-sL', it will list the IPs in the subnet, but not actually do any scan. Here is the first 5 lines and last 5 lines of the output. I checked a /22 subnet.
# nmap -n  -sL 10.188.75.0/22 | head -5

Starting Nmap 5.51 ( http://nmap.org ) at 2014-05-28 06:39 EDT
Nmap scan report for 10.188.72.0
Nmap scan report for 10.188.72.1
Nmap scan report for 10.188.72.2

# nmap -n  -sL 10.188.75.0/22 | tail -5
Nmap scan report for 10.188.75.252
Nmap scan report for 10.188.75.253
Nmap scan report for 10.188.75.254
Nmap scan report for 10.188.75.255
Nmap done: 1024 IP addresses (0 hosts up) scanned in 0.01 seconds
 Since every line I interested has string "Nmap scan report for", I use this to select them, and also delete the first line (network itself) and last line (broadcast address). Unfortunately, there are total 1022 IPs in a /22 subnet, I don't want to display them all, so I list the first 5 IPs, and last 5 IPs.
# nmap -n  -sL 10.188.75.0/22 | sed -n '/Nmap scan report for /s/Nmap scan report for //gp' | sed '$d' | sed '1d' | head -5
10.188.72.1
10.188.72.2
10.188.72.3
10.188.72.4
10.188.72.5

# nmap -n  -sL 10.188.75.0/22 | sed -n '/Nmap scan report for /s/Nmap scan report for //gp' | sed '$d' | sed '1d' | tail -5
10.188.75.250
10.188.75.251
10.188.75.252
10.188.75.253
10.188.75.254

No comments:

Post a Comment