I have installed Linux on the computers of relatives with very, very limited computer skills. With Linux, they are safe from Windows malware and almost always, I can support them (including doing remote backups) without leaving home and without any effort on their side. In practice though, this only happens if, whenever Uncle Franco asks for help, I can get the current IP address of his computer without asking him (if you don’t know what such addresses are, see the text box at right)!

That’s a problem, because all these people have low cost, residential Internet accounts with dynamic IPv4 addresses. Some of the involved ISPs randomly reassign the addresses every few hours, even if the customer did not disconnect. To make things worse, I often receive these help requests when we’re all away from our computers, maybe at somebody else’s place. I must be able to answer, “Sure, Uncle, next time you’ll turn on the computer (which may be 2 days later…) I’ll take care of that, don’t worry about it anymore”.

To handle these situations, I need to receive the IP addresses of these computers, every time they change or the computers are turned on, because:

  • All the involved computers (including mine) have dynamic IP addresses. Yes, theoretically, I could have set up some dynamic DNS service, but why do that if there is a simpler way?
  • None of those computers really is under my control; some are laptops that are moved around every day. I feel better if these machines cannot connect to each other without someone manually typing a password.
  • Above all, this is not only about backups or automatic, unattended tasks. Me, I use the trick below to perform different support work every time. But knowing the IP address of a friend’s computer automatically makes many other activities a bit easier, from gaming to working together online, without using third party services

How can you get those addresses?

In practice, I set up every computer I need to assist with:

    1. a new email address, just for this purpose (“uncle@example.com” in the code below), for each “customer”
    2. a corresponding configuration file for the Mutt email client.
    3. a script that, every few minutes, finds what the current IP address is, and sends it to me by email

      Step 1 is needed to not interfere with people’s real email, and to avoid problems if they change the corresponding passwords. Step 2 means preparing a Mutt configuration file ($RCFILE in the script) containing just what is necessary to send email from the command line, with the right email address:

        set smtp_url="smtp://uncle.joe@smtp.example.com/"
        set smtp_pass="the_password_for_that_account"
        set realname="The IP Address monitor of Uncle Joe"
        set from="uncle.joe@example.com"
        set envelope_from = yes
        set copy = yes
        set record = /tmp/mutt_send_ip_address.`/bin/date +%Y.%m`
        set postponed = /tmp/mutt_ip_postponed
        unset use_domain
        set hostname = example.com

      Please note that the URL in smtp_url must be whatever Uncle Joe’ ISP declares as “outgoing email” server! The “record” option, instead, sets the Mutt archive mailbox, which may be useful for debugging. All the options are thoroughly explained in the Mutt Manual, so I won’t get in more detail. Just remember, after you’ve replaced the right values in the file above, to try to send an email with it, to be sure it works!

      This, instead, is the shell script that does the real job:

             1  #! /bin/bash
             3  EMAIL=YOU@example.com
             4  IPFILE=/tmp/my_ip_address
             5  RCFILE="$HOME/Documents/mutt_send_ip/muttrc_ip"
             6  $LOG=/tmp/send_ip_log
             7  if [ ! -f "$IPFILE" ];
             8  then
             9      touch $IPFILE
            10  fi
            12  PREVIOUS_IP=`cat $IPFILE`
            13  CURRENT_IP=`w3m -no-cookie -dump http://whatismyip.com | sed -n 's/^\([0-9\.]\+\)$/\1/p'`
            15  if [ "$PREVIOUS_IP" != "$CURRENT_IP" ]
            16      then
            17          echo -n `date` >> $LOG
            18          echo "IP address changed from $PREVIOUS_IP TO $CURRENT_IP" >> $LOG
            19          rm -f $IPFILE
            20          echo $CURRENT_IP > $IPFILE
            21          mutt -F $RCFILE -s 'New IP Address is '$CURRENT_IP $EMAIL < $IPFILE
            22      fi

      Line 3 is the email address that must receive the notifications. Line 13 does a bit of Web Scraping to get the current IP address. Basically, it queries with the text browser w3m a Web service that provides this information. Then, using the sed program, it extracts from that Web page only the line that consists of four numbers separated by dots. To see how it works in detail, open http://whatismyip.com in your browser then run w3m -no-cookie -dump http://whatismyip.com | sed -n ‘s/^\([0-9\.]\+\)$/\1/p’ at a command prompt.

      If $CURRENT_IP is different from the content of $IPFILE, the script rewrites that file and logs the event to $LOG. Then it tells Mutt (line 21) to send an email with the subject “New IP Address is $CURRENT_IP” to $EMAIL, using the configuration stored in $RCFILE. A cron job like;

      */2 * * * * /absolute/path/to/the/script

      will run the script every two minutes, thus informing $EMAIL quickly whenever the address change.

      Now, I’m going to be mean…

      As is, the script has two issues. One is technical, one is not. Which issues, you ask? Well, I leave finding them as… an exercise for the reader, to check if you were paying attention (but don’t worry, I’ll post them in the comments anyway!).