[TriLUG] dhclient and iptables
Greg Brown
gregbrown at mindspring.com
Sat Jun 14 22:01:50 EDT 2003
I've got a script that does that. It monitors eth0 by default. Just
copy the ip address that you are currently using to a file named
"currentIP" and change /home/ghome/scripts to wherever you want to
store the script currentIP and the script and you're good to go.
Here's the script:
#!/bin/bash
###############################################################
#
# testIP.sh
# version 1.0
# Greg Brown
#
# watches the external interface on my cable modem attached
# box (eth0) and compaires the IP address to what is stored
# in the flat file /home/ghome/scripts/currentIP. If a change
# has been detected it sends an e-mail to me at work and
# at home so I can change my dynamic hostname resolution.
#
# June 12, 2003
#
# version 1.1
#
# Added a test to make sure that the results caputred in
# $IPADDRESS actually contained something. If the results
# in $IPADDRESS are null we now exit the script without
# processesing further.
#
# June 13, 2003
#
###############################################################
# get the current ip address on the system
IPADDRESS=`/sbin/ifconfig eth0 | grep inet | tr ':' ' ' | awk '{ print
$3 }'`
# test line
# echo "current ip $IPADDRESS"
# now use test to see if $IPADDRESS has a value stored in it
/usr/bin/test $IPADDRESS
# now check the results of $? which is were the results of the above
# test are stored. If the result is 1 then it is null, if the result
# is 0 then it is NOT null
if /usr/bin/test $? = 1
then
# debug line
# echo "we don't have a IP address stored in $IPADDRESS"
# we don't have an ip address so exit the script
# we can assume the interface is down and since there is no
# point in sending an e-mail - we can't without the inteface
# being up - well, just exit.
exit
fi
# debug line
# echo "we made it past the first test"
# now get the last known IP address
LASTIP=`/bin/cat /home/ghome/scripts/currentIP`
# echo "lastip $LASTIP"
# now test to see if the IP addresses match
if /usr/bin/test "$IPADDRESS" = "$LASTIP"
then
# echo "we have the same ip address"
# echo "last: $LASTIP current: $IPADDRESS"
# do nothing, we have matching IP addresses
# just exit
exit
else
# echo "we do not have matching IPs"
# echo "last: $LASTIP current: $IPADDRESS"
# we do NOT have matching IP addresses so
# we have to overwrite the currentIP file
echo $IPADDRESS >/home/ghome/scripts/currentIP
# get the hostname for the e-mail
HOSTN=`/bin/hostname`
# and get the time as well
DANDT=`/bin/date`
# and we have to notify Greg what has happened
# create a file with the info we need to send
# the email
echo "$DANDT" >/tmp/$$
echo "$HOSTN has changed IP addresses" >>/tmp/$$
echo "IP was $LASTIP" >>/tmp/$$
echo "IP is now $IPADDRESS" >>/tmp/$$
# now mail the file
/bin/mail -s "$HOSTN has changed IP Addresses"
gregbrown at mindspring.com </tmp/$$
# now remove /tmp/$$
/bin/rm /tmp/$$
# and we're done
exit
fi
More information about the TriLUG
mailing list