External IP Reporting from CENTOS

Much like my PHP example of external IP reporting, the following script performs exactly the same task, but in Centos. This combined with creating a custom system service should provide dynamic IP resolution even if the PC reboots.


#!/bin/bash
i=1
while [ $i == 1 ]
do
datestring=$(date –utc +%FT%TZ)
ipfile=”myip.txt”
ipdir=”/var/manage/”
logfile=/var/manage/myiplog.txt
oldip=$(cat $ipdir$ipfile)
echo “Getting IP… ”
wanip=$(dig +short EXTERNAL IP RESOLVER ADDRESS)
#if [ 1 -ne 1 ]
if [ $oldip != $wanip ]
then
echo My IP has changed! Saving change to file…
echo $wanip > $ipdir$ipfile
echo Connecting to FTP server…
lftp -u FTP_USERNAME,FTP_PASSWORD FTP_SITE -e ” lcd $ipdir; put $i$
echo Writing log…
echo “$datestring IP changed to $wanip” >> $logfile
echo Log written!
else
echo My IP is the same!
fi
echo Sleeping for 5 mins…
sleep 300s
done

Leave a Reply

Your e-mail address will not be published. Required fields are marked *