Monday, December 10, 2007

Resolving a hostname from the DI-624 DHCP

Its an edit of the getIp script I wrote earlier.. this time it checks the DHCP page. Provide a hostname & it ll return you the IP (if its in there..)


# Variables
## Host
host="HOSTNAME"

## Location & filename for temporary files
map="/tmp/"
file="adres"

## Webpage with IP info page
IPpage=http://192.168.0.1/h_dhcp.html
IPu=username
IPp=password



## Do not edit beyond this point ;)

if [[ $map == "" || $map == "/" || $file == "" ]]
then
echo "Thank god I didnt trust you to fill these out wisely."
echo " (dont get over confident tough, this is an EXTREMELY basic test!!)"
echo "At least One of the following errors was found!"
echo
echo "* the map and file variable can NOT be empty!"
echo "* the map can NOT be / (the root)."
echo "Since we re doing rm -f, these settings might cost you your OS!"
echo " ;)"
echo
echo $map$file
exit 1
fi

# make sure there s no files that could screw the process ;)
rm $map$file* -f

# Get IP
wget -q --user $IPu --password $IPp $IPpage -O $map$file.router


# check for file existance
if [[ -e $map$file.router ]]
then
grep $host $map$file.router | grep dDat | grep -oE [0-9]+\.[0-9]+\.[0-9]+\.[0-9]\+ > $map$file

cat $map$file

fi

# remove residual files
rm $map$file* -f


I needed this to have synergy connect to the right dynamic IP.. It still wont work all of the time, but at least, now it ll work more!

Next up is a script to check that page & fill in the /etc/hosts file for my linux boxes.. I might make it more secure then, since it can at this time be abused since it uses temp files..

No comments: