OpenWRT Router IP Exposure Script

If you have an OpenWRT router (any router that can run the software can be converted), you can do a little quasi-dynamic-DNS trick. This is useful if you don't really care enough to set up Dyn DNS, and you have a web server setup.

The main disadvantage is that you don't have a DNS record. The main advantage is that the updates don't need to propagate through DNS, so if you have an app that relies on talking to your LAN, you can quickly detect any changes.

First, save this as /usr/bin/checkmyip

#!/bin/sh

past_ip="first"
[ -f /tmp/myip ] &current_ip=`ifconfig | grep P-t-P | tr ':' ' ' | awk '{print $3;}'`

if [ ${past_ip} != ${current_ip} ] ; then
        exec wget -q -O /tmp/myip.html http://yourdomain.com/networkname/wrt.php?ip=${current_ip} 2>&1 &
        echo ${current_ip} > /tmp/myip
        rm /tmp/myip.html
fi

Then add it to your cron via crontab -e. Run it every 15 minutes, or more often.

Add this to your web server, in its own directory, as wrt.php:

<?php
$ip = $_GET['ip'];
$current_ip = escapeshellcmd("$ip");
system("echo $current_ip > current_ip");
system("echo Changed on: `date` >> current_ip");
?>

Yes, not that efficient.