Friday, 17 September 2010

Discovery alive hosts using nmap

This script searches alive hosts, using ping, tcp and udp scanning. Nmap input syntax is accepted, for example 192.168.1.0/24.
Two files are generated at the end:
network_number_alive_hosts - results of script, list of alive ip addresses
network_number_log - log of the scan

#!/bin/bash
#variables
name=$(echo $1 | tr '/' '_')_alive_hosts
log=$(echo $1 | tr '/' '_')_log
tcp_ports=21,22,23,25,53,80,88,110,135,137,148,139,443,445,990,8080,3128
udp_ports=53,88,123,137,138,161,500,514
echo -e "$(date)\nDiscovery of network $1 started" > $name

#ping scan
nmap -sP -oG ping.txt $1 > $log
cat ping.txt | grep Up | cut -d" " -f2 >> $name
cat ping.txt | grep Down | cut -d" " -f2 > nmap.txt
#tcp&udp scan
nmap -PN -T4 -sT -sU -p T:$tcp_ports,U:$udp_ports -iL nmap.txt -oG ports.txt >> $lcat ports.txt | egrep "open/|closed" | cut -d" " -f2 >> $name
echo "Finish of discovery: $(date)" >> $name
#delete temp files
rm nmap.txt ping.txt ports.txt
Search alive hosts from network list and compares with list of existed addresses

#!/bin/bash
folder=$(date +%d-%m-%g)
mkdir $folder
while read line
do
#variables:
name=$(echo $line | tr '/' '_')_alive_hosts
log=$(echo $line | tr '/' '_')_log
tcp_ports=21,22,23,25,53,80,88,110,135,137,148,139,443,445,990,8080,3128
udp_ports=53,88,123,137,138,161,500,514
echo -e "$(date) - Discovery of network $1 started" > $folder/$name
#ping scan
nmap -sP -oG ping.txt $line > $folder/$log
cat ping.txt | grep Up | cut -d" " -f2 >> $folder/$name
cat ping.txt | grep Down | cut -d" " -f2 > nmap.txt

#tcp&udp scan
nmap -PN -T4 -sT -sU -p T:$tcp_ports,U:$udp_ports -iL nmap.txt -oG ports.txt >> $folder/$log
cat ports.txt | egrep "open/|closed" | cut -d" " -f2 >> $folder/$name
echo "Finish of discovery: $(date)" >> $folder/$name
cat $folder/$name | egrep -iv "discovery" >> $folder/final

#delete temp files
rm nmap.txt ping.txt ports.txt

done

sort ips > ips2\
sort $folder/final > final
comm -23 final ips2 > newhosts

rm final ips2

No comments:

Post a Comment

Ping does not work

Today I would like to discuss a banal situation: host A is directly connected to host B, ping from host A to host B does not work. What are...