Dec 15

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.

Dec 15

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. Since I donlt have a static IP address visible to the Internet I needed to work out a way to make sure that the monitoring configuration gets to use the current IP address of my monitoring endpoint despite the ISP changing the IP address. I use Dynamic DNS service. More on this in a subsequent article.

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 attempts to resolve the domain name of the host to its I address as means of obtaining the IP address which is visible form the Internet.

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 resolve the externally-visible host name of my externally-visible host, compare it to the last IP address that was resolved and, if they are different, update the last resolved IP Address and log a change to the system log and the private log created for the purpose.

Set prerequisites (manually create myip_last.log for the first time):

mkdir -pv ~/ipaddresses
# seed for the first time
myip=$(nslookup somehostname.dynu.net 208.67.222.222 | grep Address | tail -n 1 | cut -c10-)
echo "$(date -jR) - ${myip}" > ~/ipaddresses/myip_last.log

Create the script:

> ~/log_ip_change.sh # create empoty script file
chmod u+x ~/log_ip_change.sh # set permissions to allow script execution

cat <<-'EODECK' > ~/log_ip_change.sh # create the script via a HERE-document
#!/bin/sh
myip=$(nslookup somehostname.dynu.net 208.67.222.222 | grep Address | tail -n 1 | cut -c10-)
olip=$(cat ~/ipaddresses/myip_last.log|cut -d'-' -f2|tr -d ' ')

if [ $myip != $olip ]; then
    echo "$(date -jR) - ${myip}" > ~/ipaddresses/myip_last.log
    cat ~/ipaddresses/myip_last.log >> ~/ipaddresses/myip.log
    logger -p user.crit "IP Address change: $(cat ~/ipaddresses/myip_last.log)"
fi

EODECK

Add this script to crontab and run it every minute

crontab -eAdd the following line
*/1   *    *    *    *     /root/log_ip_change.sh 2>/root/ipaddresses/log_ip_change.err

Give it 1 minute and see what the logs say

cat ~/ipaddresses/myip_last.log
cat ~/ipaddresses/myip.log

Summary

In this article I present a script which watches and records my externally-visible IP addresses, as changed bymy ISP form time to time, by resolving my externally-visible host name to the IP address – call me curious 🙂

preload preload preload