Find and Post My IP

2009/09/16

I use and love DynDNS but for some reason one of my hosts isn’t updating like it should.

While I work on a solution, I need to make sure that I can get at the machine via ssh, which means I need to know its IP address.

There are a lot of sites which offer to show you your IP but when I tried a few of them via scripts, they either blocked lynx, or required that the search be done interactively. Not to mention the ads and other crap/

I can see their point (they don’t want to be inundated with a bunch of automated requests) and this isn’t a tricky problem to solve, assuming you have access to a webserver where you can run PHP. Create a small file named something like ’showmyip.php’:

Step 1: Make a PHP file on your server

<?php

$ip=$_SERVER['REMOTE_ADDR'];
echo "<html><body>$ip</body></html>";

?>

You could skip the actual HTML markup, but it’s shown here as an example.

Step 2: Create a script on the computer you want to be able to find via IP

Then you just need a script which will check the IP, and then upload it to the same server. So, an example of how this might work here at luo.ma

#!/bin/zsh -f

# your $PATH may be different
PATH=//usr/local/bin:/usr/local/sbin:/opt/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

MYIP=`lynx -dump -nomargins "http://luo.ma/showmyip.php" | egrep '[0-9]'`

if [ "$MYIP" != "" ]
then

    ssh luo.ma "(echo $MYIP > public_html/$HOST.ip.txt)" 

fi

Note: http://luo.ma/showmyip.php is not the actual path. My goal here is not to setup a free service for you, but show you how to setup your own.

This assumes that you’ve already setup password-less ssh between the $HOST machine and the server. If you’re operating at this level of geekdom, I’m sure you can figure out how to make that work…

Save that “/usr/local/bin/uploadmyip.zsh” (or whatever… be sure to make it executable!) and add it to cron:

Step 3: Add script from step 2 to cron

*/15 * * * *  /usr/local/bin/uploadmyip.zsh

That tells cron to update it every 15 minutes.

Notes:

Be sure to customize the script to your setup:

  1. Change http://luo.ma/showmyip.php to the path on your server.
  2. Change ssh luo.ma to ssh your.server.name.here
  3. Change public_html/ to wherever you want the file stored relative to your $HOME on the remote server.

The end result is that I could go to http://luo.ma/imac.ip.txt and always know that I was getting the current IP address for my iMac, from any computer.

blog comments powered by Disqus

Previous post: QuickTime X’s UI (or lack thereof) is already frustrating

Next post: How to get the MPEG-2 Component for Quicktime