Logo
IT Dienstleistungen

NMAP your own network

Are you sometimes curious about the running hosts in your actual working enviroment? Well, nmap-ping the network is not that hard to do, but as I always have to look after the parameters and sometimes also the actual netaddress, I'd tried to automate this with a small script here

You need nmap installed and working as root!

/usr/bin/netscan

#!/bin/bash                                                   
# (c) 2009 Manuel Krischer - www.krischer.org                 
echo "Netscan - Scan your actual network for running hosts"   
 
#if no device is given, take the first active ethernet device
if [[ $1 == "" ]]; then                                      
        DEV=$(ifconfig | grep Link | grep -i ether -m 1 | cut -d ' ' -f 1)
else                                                                      
        DEV=$1                                                            
fi                                                                        
echo "Using Device $DEV"                                                  
 
#analyze the ip-address and split it
NET_ADRESS=$(ifconfig $DEV | grep 'inet Adresse' | grep -v 127.0 | cut -d':' -f2 | cut -d' ' -f1)
NET_1=$(echo $NET_ADRESS | cut -d'.' -f1)                                                        
NET_2=$(echo $NET_ADRESS | cut -d'.' -f2)                                                        
NET_3=$(echo $NET_ADRESS | cut -d'.' -f3)                                                        
NET_4=$(echo $NET_ADRESS | cut -d'.' -f4)                                                        
echo "Found IP: $NET_ADRESS"                                                                     
if [[ $NET_ADRESS == "" ]]; then                                                                 
        echo -e "\n\tERROR: No IP-Address found! Connect to a network or specify device!\n"      
        exit 1                                                                                   
fi                                                                                               
 
#same with net mask
NET_MASK=$(ifconfig | grep 'inet Adresse' | grep -v 127.0 | cut -d':' -f4)
MASK_1=$(echo $NET_MASK | cut -d'.' -f1)                                  
MASK_2=$(echo $NET_MASK | cut -d'.' -f2)                                  
MASK_3=$(echo $NET_MASK | cut -d'.' -f3)                                  
MASK_4=$(echo $NET_MASK | cut -d'.' -f4)                                  
echo "Found Netmask: $NET_MASK"                                           
 
#look for the network class
if [[ $MASK_1 != "0" ]]; then
         CLASS=8
         NOM=A
   if [[ $MASK_2 != "0" ]]; then
         CLASS=16
         NOM=B
        if [[ $MASK_3 != "0" ]]; then
         CLASS=24
         NOM=C
        fi
   fi
fi
echo "Seems to be a Class $NOM ($CLASS) network."
 
#build network adress out of IP and netmask
if [[ $CLASS == "8" ]]; then
        NETWORK=$NET_1.0.0.0
elif [[ $CLASS == "16" ]]; then
        NETWORK=$NET_1.$NET_2.0.0
elif [[ $CLASS == "24" ]]; then
        NETWORK=$NET_1.$NET_2.$NET_3.0
fi
 
#finally call nmap to search for running machines, maybe the check can be improved
echo -e "The following hosts in your actual network seems to be online"
echo -e "(Gateways and your own host included!)\n"
nmap -v -sP $NETWORK/$CLASS | grep -v "appears to be down" | grep Host

Seiten-Werkzeuge