Koozali.org: home of the SME Server

Script to log WAN IP in server-only mode

Offline mmccarn

  • *
  • 2,627
  • +10/-0
Script to log WAN IP in server-only mode
« on: July 14, 2017, 12:27:01 PM »
I run my SME in server-only mode, and I wanted to keep a record of my WAN IP for various reasons.

I wrote a script that outputs the date, time and WAN IP either once each day or if the WAN IP changes.

Here's the script:

/usr/local/bin/wanip_log.sh:
Code: [Select]
#!/bin/bash
mylog=/var/log/wanip.log
mydate=$(date +"%F")
mywanip=$(dig +short myip.opendns.com @resolver1.opendns.com)

if [ -f $mylog ]; then
  myoldip=$(tail -1 /var/log/wanip.log |cut -f3 -d' ')
  mylastdate=$(tail -1 /var/log/wanip.log |cut -f1 -d' ')
else
  touch $mylog
  myoldip=a.b.c.d
  mylastdate=1970-01-01
fi

if [ "$mywanip" == "$myoldip" ]; then
  if [ "$mydate" != "$mylastdate" ]; then
    echo $(date +"%F %T") $mywanip >> $mylog
  fi
else
    echo $(date +"%F %T") $mywanip >> $mylog
fi

And here's what i put into crontab:
Code: [Select]
*/5 * * * * /usr/local/bin/wanip_log.sh