Context
My Internet Service Provider changes my externally-visible IP Address from time to time without notice. I am having issues with the ISP’s service and I decided to keep a watch on my Internet connectivity unsing external monitoring services. As I work on refining my connectivity monitoring infrastructure, I detect anomalies which need watching. One of these anomalies is the ISP-induced IP address change frequency.
In this article I present a simple Unix script which keeps track of my changing external IP addresses so that I can look and see what they were at different points in time and when they were changed, to within 2 minutes. This script is ISP Router-specific. It applies to Sagemcom F@ST3864 provided by Optus.
Script
I have a pfSense firewall (https://www.pfsense.org/), which is based on the FreeBSD OS. The script being discussed runs on this firewall.
The purpose of the script is to discover the externally-visible host name of my externally-visible host, compare it to the last IP address that was scraped from the ISP-provided Router’s management UI and, if they are different, update the last scraped IP Address and log a change to the system log and to the private log created for the purpose.
Set prerequisites (manually create myip_last.log for the first time):
mkdir -pv ~/ipaddresses
# find externally-visible IP address of the gateway gatewayIP=xxx.xxx.xxx.xxx export CURLOPT_USERPWD="routeruiusername:routeruipassword" extip=$(curl --user ${CURLOPT_USERPWD} -ss http://${gatewayIP}/info.html | grep -A1 'WAN IPv4 Address:' | tail -n 1 | sed 's|^[ ].*<td>||;s|</td>$||') echo "$(date -jR) - ${extip}" > ~/ipaddresses/extip_last.log
Create the script:
> ~/extip_log_change.sh chmod u+x ~/extip_log_change.sh cat <<-'EODECK' > ~/extip_log_change.sh #!/bin/sh gatewayIP=xxx.xxx.xxx.xxx export CURLOPT_USERPWD="routeruiusername:routeruipassword" extip=$(curl --user ${CURLOPT_USERPWD} -ss http://${gatewayIP}/info.html | grep -A1 'WAN IPv4 Address:' | tail -n 1 | sed 's|^[ ].*<td>||;s|</td>$||') oldip=$(cat ~/ipaddresses/extip_last.log|cut -d'-' -f2|tr -d ' ') if [ $extip != $oldip ]; then echo "$(date -jR) - ${extip}" > ~/ipaddresses/extip_last.log cat ~/ipaddresses/extip_last.log >> ~/ipaddresses/extip.log logger -p user.crit "IP Address change (at GW): $(cat ~/ipaddresses/extip_last.log)" fi EODECK
Add this script to crontab and run it every minute
crontab -e
*/1 * * * * /root/extip_log_change.sh 2>/root/ipaddresses/extip_log_change.err
Give it a minute and see what the logs say. Likely they will say nothing new unless it so happened that the IP address was changed between the time you first logged it manually and the time you run the script.
When you detect connectivity issues based on notifications from the monitors check the logs.
cat ~/ipaddresses/extip_last.log cat ~/ipaddresses/extip.log
Summary
In this article I present a simple Unix script which keeps track of my changing external IP addresses so that I can see what they were at different points in time and when they were changed. This script is ISP Router-specific. It applies to Sagemcom F@ST3864 provided by Optus.