Tracking a dynamic IP address

Posted on 27/10/2009 by

0



Problem:
You have a family member or friend(s) who has a linux box that you administer for them. They are on a dynamic ip service from their isp. Therefore their ip address changes regularly preventing you from easily ssh’ing into their box.

Solution:

You could use a free dynamic ip updating service such as no-ip.com, dyndns.com or dtdns.com. The problem is those services will require updating. meaning you must install some client software on their machines and they must have their P.C turned on on a regular basis.
Can the client software be trusted not to do something nefarious? Also with my friends, they weren’t keeping their PC on enough and the dynamic dns service eventually expired their account for inactivity.
Fortunately there is another alternative if you have a linux server connected to the net.
My server has apache and postfix running so it was pretty simple to hack together a couple of scripts to solve the issue.

On the friends PC make sure wget is installed. (it is usually installed by default on most distro’s) Make a cronjob with the following command…
crontab -e
This edits the users crontab using the editor you or your distro specified. In ubuntu it is nano, on other distros it may be vi.
add a line like this.

00 8,20 * * * wget http://yourserver/friends-ip.html

save and exit the editor
now make sure the changes are saved by displaying the users crontab with the command…
crontab -l
This will fetch the file at 8 am and 8pm. Change the time to one suitable for your friend’s hours.
You may also want to add the wget command to a local startup script if they have their PC on very little.
The name of the file to wget can be anything just remember what it is and make it different for every user.

Now on your server make a script file named ipreport.sh like this one. I keep mine in /root but put yours wherever is convenient for you.

#!/bin/bash
cat /var/log/apache2/access.log |grep friends-ip.html >> /root/ipreport.txt
cat /root/ipreport.txt |mail -s “Fiendsname’s IP” your email address
rm /root/ipreport.txt

Make the saved file executable with chmod o+x ipreport.sh

Now create a cronjob on the server. You can do this several ways but since I use vixiecron which isn’t picky I just edit /etc/crontab and add this line

10 8 * * * root /root/ipreport.sh

Finally make the file that wget is looking for in your apache DocumentRoot. On my machine that is /var/www/

sudo touch /var/www/friends-ip.html

If you like you can edit this file and put an explanation in it to help you remember what it is.
The file also needs to be readable by your webserver.
For me using ubuntu…
sudo chgroup www-data
sudo chmod 740 friends-ip.html

Now I should be emailed daily me with the last ip address of my friend. Note I have it set to happen at 8:10 am giving their computer a little extra time to connect in case their time is off.
The email will have the subject “Friend’s IP”
and the body of the message will look something like this
IP Address – – Access Date and Time “GET /friends-ip.html HTTP/1.0” 200 30 “-” “Wget/1.10.2”

Whenever you want to ssh into their box simply look at the email from today and ssh to the listed IP.

– dgurl –

Advertisements
Posted in: Hacking, Networking