Marco Fioretti shares a little tip and script for an easy way to have the IP addresses of computers you support automatically sent to you.
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)!
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:
- a new email address, just for this purpose ("firstname.lastname@example.org" in the code below), for each "customer"
- a corresponding configuration file for the Mutt email client.
- 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 realname="The IP Address monitor of Uncle Joe"
set envelope_from = yes
set copy = yes
set record = /tmp/mutt_send_ip_address.`/bin/date +%Y.%m`
set postponed = /tmp/mutt_ip_postponed
set hostname = example.comPlease 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
7 if [ ! -f "$IPFILE" ];
9 touch $IPFILE
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" ]
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
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!).