Get list of hosts from DHCP

The premise is you need to know the name and ip of every computer on the WAN. One approach is to as your DHCP server by looking at it's logs

have a log file like so:

Nov 15 23:59:59 132.235.51.127 dhcpd[22299]: DHCPREQUEST for 132.235.26.241 from 00:1a:a0:b4:b0:fb (scaliaprojectB) via eth2
Nov 15 23:59:59 132.235.51.127 dhcpd[22299]: DHCPACK on 132.235.26.241 to 00:1a:a0:b4:b0:fb (scaliaprojectB) via eth2 relay eth2 lease-duration 15300
...
...

So you tie together a bunch of tools like so:

#!/bin/bash

if [ "$1" = "" ]
then
        echo "No path specified. Try looking in /data/log/ib2.cns.ohio.edu/local1<something>"
else
#       Get only lines with DHCP Requests
#       that have a (hostname in them
#       Remove the sometimes extra (IP)
#       Get just the name, ip and mac of the client
#       Get only lines with 132.235 in them
#       Remove the () around the name
#       Sort by the mac address in field 2 (some pc's have the same name)
#       Remove duplicates by skipping field 1 and comparing mac addr
#       Resort by IP
#       Clean up the output by inserting a tab after the IP

        cat $1 | grep DHCPREQUEST | \
        egrep '\([a-zA-Z]' | \
        sed 's/([1-9]*\.[1-9]*\.[1-9]*\.[1-9]*) //' | \
        cut -d " " -f 8,10,11 | \
        egrep '132\.235' | \
        sed "s/(//" | sed "s/)//" | \
        sort -k 2 | \
        uniq -f 1 | \
        sort | \
        sed 's/ /       /'
fi

#  command to sawp in case needed
# sed "s/^\([^ ]*\) \([^ ]*\)/\2 \1/"


Comments