Header Shadow Image


Dynamic IP’s: How to find external IP of your Router / Linux machine and ‘ssh’ to it.

Working remotely from various places, I needed to have access to my Linux machine at home. Unfortunately for me, my DSL IP was dynamic and at the time my ISP would change this IP several times a day. That was good for maybe only a few hours and then it would change to something completely different and there I was not being able to login remotely to my Linux box again. I knew I could find my external IP from my routers ’status’ page and started looking into ways of getting this information remotely without actually having to pay for a static IP.  I knew of software I could purchase but didn’t want to go into an expensive trial and error campaign with software that eventually might not have something I really needed.  Additionally, as you may find out or already know, having a static IP has some disadvantages when it comes to security and tracability.  Simplicity at no cost is what I wanted: ssh.

INTRO

There’s a couple of ways of gaining access to your remote system without spending a dime. The first way that came to mind was to email myself the external IP from within my Linux distribution whenever it changed. This is probably the least expensive solution since there are many available web mail clients out there that you can sign up with to where you could send your IP. An alternate would be to send your own IP to a location you already know such as an FTP account you have or your own web account / web page somewhere.

SOURCE CODE

Eventually I settled on the email option since it was relatively easy to implement.  The full script below (routerip.sh):

#!/bin/bash

# — OLD FIRMWARE —
#lwp-request http://”<NAME>”:”<PASS>@192.168.0.1/status.xyz|sed -n ‘/IP Address/{n;p;}’|awk -F\< ‘{ print $4 }’|awk -F\> ‘{ print $2 }’ > /tmp/latest-ip-check.txt
# — NEW FIRMWARE —
#lwp-request http://”<NAME>”:”<PASS>”@192.168.0.1/status.xyz|sed -n ‘/share.ipaddr/{n;p;}’|awk -F\< ‘{ print $4 }’|awk -F\> ‘{ print $2 }’ > /tmp/latest-ip-check.txt
# — SSL SECURED —
lwp-request https://”<NAME>”:”<PASS>”@192.168.0.1/status.xyz|sed -n ‘/share.ipaddr/{n;p;}’|awk -F\< ‘{ print $4 }’|awk -F\> ‘{ print $2 }’ > /tmp/latest-ip-check.txt

diff /tmp/latest-ip-check.txt /YOUR FOLDER/last_ip.txt >/dev/null 1<&2;
if [ $? == 1 ]; then
      echo “Sending new IP ….”;
      cat /tmp/latest-ip-check.txt > /YOUR FOLDER/last_ip.txt;
      echo “/YOUR FOLDER/routerip.sh: Sending mail to mrjoe@joesfreeemail.xyz with new IP.” >> /var/log/messages;
      mail -s “Your new Router IP” mrjoe@joesfreeemail.xyz < /YOUR FOLDER/last_ip.txt >/dev/null 1<&2;
      if [ $? == 1 ]; then
            echo “/YOUR FOLDER/routerip.sh: Sending of IP failed!” >> /var/log/messages; > /YOUR FOLDER/last_ip.txt;
      fi
fi
rm -f /tmp/latest-ip-check.txt

 

HOW DOES IT WORK

Let’s look at the first set of lines above that start with ‘lwp-request‘.  These lines are probably the most important as they extract the IP from your router.  To do this, first you need to know where does your router keep your external IP address. To figure this out, login to your router  using your favorite browser and browse to your status page that contains your router information. Note down the link in your browser. This could be something like ‘https://192.168.0.1/status.xyz‘. Once you have this site, you need to extract the IP from it using command line tools.  This is where lwp-request comes in. This is a linux command line utility for testing web servers used mostly by admins. For our purposes, we need it to extract the HTML code from the routers status page above.

$lwp-request https://192.168.0.1/status.xyz

In my case the command produced nearly 600 lines of HTML code with my IP being burried in the middle. For most purposes, this would be sufficient and you could put this command into a small script and send this resultant page off to some email address you have using:

*/5 * * * * nice -n 10 /path_to_your_script/routerip.sh

somewhere in your crontab as a job ever so often.  This would work, but there’s a couple of problems with that. First, since you’re not checking if your IP changed, you need to keep emailing yourself. That’s alot of mail and your ISP and the remote free email service admins might not like that. Second, you’re reading messy output and since you have so much mail to go through, you have to read ALOT of messy output.

Let’s refine this a bit. First check how often your IP appears in the file and the surrounding code it appears in.

$ lwp-request https://” “:”<YOUR PASS>”@<192.168.0.1/status.xyz|grep <YOUR CURRENT EXTERNAL IP>|wc -l then

$ lwp-request https://”<R. LOGIN NAME>”:”<R. PASS>”@<192.168.0.1/status.xyz|grep <YOUR CURR. EXT. IP> -A5 -B5

(NOTE: The HTTPS and the router NAME and PASSWORD. I’ve taken steps to secure my router better then using the default security settings my router came shipped with. For you, the command will be different. You’ll have to experiment how to get your page using the commands in this article. )

Of course, you’ll need to replace the above place holders with your own information for your particular router. In my case I noted a unique string the external IP always came listed under: share.ipaddr. Here’s the line to extract the first line below the line containing ‘share.ipaddr‘:

$ lwp-request https://”<R. LOGIN NAME>”:”<R. PASS>”@192.168.0.1/status.xyz|sed -n ‘/share.ipaddr/{n;p;}’ <TD><FONT style=”FONT-SIZE: 11pt”><B>123.123.123.123</B></FONT></TD> $

The sed is a powerfull search and replace utility.  It’s very extensive and beyond the scope of this topic to include all details on it.  For the time being, all you need to know is that below line extract the line after ‘share.ipaddr:

sed -n ‘/share.ipaddr/{n;p;}’

Let’s refine this further however. We can use awk command to refine the results a bit more:

$ lwp-request https://”<NAME>”:”<PASS>”@192.168.0.1/status.xyz|sed -n ‘/share.ipaddr/{n;p;}’|awk -F\< ‘{ print $4 }’ B>123.123.123.12
$

That’s better. The awk command extract the 4th field from the previous result and uses the ‘<‘ character as the delimeter: -F\< .  We still have ‘B>‘ in there.  We can get rid of it by addint another call to awk again using the ‘>‘ as the delimeter this time to produce just the IP:

$ lwp-request https://”<NAME>”:”<PASS>”@192.168.0.1/status.xyz|sed -n ‘/share.ipaddr/{n;p;}’|awk -F\< ‘{ print $4 }’|awk -F\> ‘{ print $2 }’
123.123.123.12
$

Great!  Now we have one line that extracts the external IP for us. We now have clean, easily readable output. For the next part we need to check if this IP has changed (Don’t want to send ourselves the same IP over and over.). The way to do this is to save the latest IP to a file and compare subsequent checks against that IP. To do so, pipe the output of the above command to a temporary file:

$ lwp-request https:// ….. rint $2 }’ > /tmp/latest-ip-check.txt

and use the diff command to check if the current IP is different from the last one:

diff /tmp/latest-ip-check.txt /<YOUR FOLDER>/last_ip.txt >/dev/null 1<&2;
if [ $? == 1 ]; then # There is a difference. .
cat /tmp/latest-ip-check.txt > /YOUR FOLDER/last_ip.txt > /dev/null 1<&2; .
mail -s “Your new Router IP” mrjoe@joesfreeemail.xyz < /root/run/currentip.txt >/dev/null 1<&2;
….
fi

The way above works is like this.  If the current check shows that the IP is different then save the current IP to a folder and file of your choice then send it to your email from that file.  The same file would then keep your current IP on subsequent comparisons when it changes. You can put all of these commands into a bash / ksh script and run it at a specified interval. Notice the script will only send if there is a difference between what the IP used to be and what it is now.  To setup the cronjob, type:

$ crontab -e

to begin editing the current crontab file (‘-e’ standing in for edit) to have your script run periodically:

$ crontab -l
*/5 * * * * nice -n 10 /YOUR FOLDER/getmyexternalip.sh
$

In my case I have the WRT54GL router and the script is coded against it.  In your case, as mentioned above, you’ll have to experiment with the first set of ‘lwp-request‘ lines in your code to extract your IP from the config page of your router.

And there you have it. Everytime your IP changes at home, you get a notice in the email.  Omissions, suggestions, success stories?  Feel free to submit a comment on this.  smiley

2 Responses to “Dynamic IP’s: How to find external IP of your Router / Linux machine and ‘ssh’ to it.”

  1. isnt there an easier way? im with my colleague behind a dhcp client which gives us each morning a fresh ip, just varying slightly, but varying. somehow i cant get samba to work so i thought of trying ssh, but for that i need to know my own ip address when i login..

  2. Great info, just what i was looking for!

Leave a Reply

You must be logged in to post a comment.


     
  Copyright © 2003 - 2013 Tom Kacperski (microdevsys.com). All rights reserved.

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License