#!/bin/bash # the most basic "host discover" networking script using ping ### input format: # $1 = SUBNET e.g. 192.168.0 # $2 = RANGE e.g. 255 ### output format: # ip (mac) (hostname) (real/vm) description & howto SUBNET=192.168.1 RANGE=255 LOGFILE="scan_network_device_simple_ping.$SUBNET.log" CURRENT_MAC="" CURRENT_IP="" CURRENT_HOSTNAME="" echo "===== scan_network_device_simple_ping v1 =====" echo "=== starting new log file $LOGFILE ===" echo "ip (mac) (hostname) (real/vm) description & howto" > $LOGFILE; echo "ping scanning SUBNET: "$SUBNET; echo "ping scanning RANGE: "$RANGE; echo "=== start scan ===" for ((n=1; n<=$RANGE; n++)) do # assemble ip CURRENT_IP=$SUBNET.$n; echo "=== test if host is alive (3x pings) ===" ping -c1 $CURRENT_IP echo "=== test if a mac address was discovered ===" CURRENT_MAC=$(arp -n -a|grep $CURRENT_IP) sleep 1; # chance to Ctrl+C cancel the process if [[ $CURRENT_MAC == *"incomplete"* ]]; then CURRENT_MAC="unkown"; fi echo "=== test if hostname can be resolved ===" CURRENT_HOSTNAME_TEMP=$(host $CURRENT_IP) if [[ $CURRENT_HOSTNAME_TEMP == *"not found"* ]]; then CURRENT_HOSTNAME="unkown"; else CURRENT_HOSTNAME=$(echo $CURRENT_HOSTNAME_TEMP|grep -oE '[^ ]+$') fi # ip (mac) (hostname) (real/vm) description & howto echo "$CURRENT_IP ($CURRENT_MAC) ($CURRENT_HOSTNAME) (real/vm?) description & howto" >> $LOGFILE; done