Monday, December 21, 2009

Script to record the IP address of your computers

I like to keep a simple script on all of my machines that routinely checks in with a server and records the current IP of the machine. It's great for ssh'ing into a machine that has a constantly changing IP address (like a laptop) and in case of theft, it could help track down the stolen machine.

I like to have the clients check in with a webserver, instead of a machine I own. Basic websites are cheap (like GoDaddy) and have high availability.

There will be three files that this project will use:
-CheckIn.php will be on the server and will record the IP.
-CheckIn.txt will store the IP addresses.
-CheckIn will be on the client. It will use wget to contact the server.

1. Here is CheckIn.php:

<?php
//Begin Setup - Change these variables
$filename = "CheckIn.txt"; //The name of the text file to save the IP addresses to.
$my_pass = "password"; //The password to see the IP addresses.
$max_lines = 100; //The maximum number of IP addresses to store. 0 for no limit.
//End Setup

if (!function_exists('file_put_contents')) {
function file_put_contents($filename, $data) {
$f = @fopen($filename, 'w');
if (!$f) {
return false;
} else {
$bytes = fwrite($f, $data);
fclose($f);
return $bytes;
}
}
}

$computer = htmlentities($_GET['user']);
$password = htmlentities($_GET['password']);
$contents = file_get_contents($filename);
if(!$computer){
if($password==$my_pass){
echo "<table border=1 cellpadding=2 cellspacing=2>";
echo $contents;
echo "</table>";
}
} else {
$output = "<tr>";
$output = $output."<td>".$computer."</td>";
$output = $output."<td>".$_SERVER['REMOTE_ADDR']."</td>";
$output = $output."<td>".date("ymd H:i D")."</td>";
$output = $output."<td>".time()."</td>";
$output = $output."<td>".$_SERVER['HTTP_REFERER']."</td>";
$output = $output."<td>".$_SERVER['HTTP_USER_AGENT']."</td>";
$output = $output."</tr>\n".$contents;
$counter = fopen("Stats", 'w');
fwrite($counter, $output);
fclose($counter);
if($max_lines!=0){
$array = file($filename);
for($i=sizeof($array); $i>=$max_lines;$i--){
unset($array[$i]);
}
$array=array_values($array);
file_put_contents($filename,implode($array));
}
}

?>


This code should work on all machines running PHP v4.3 and later. Change the filename, password, and max_lines at the beginning of the file to the values you want to use.

2. CheckIn.txt is an empty file to start with:

touch CheckIn.txt


3. Upload Checkin.php and Checkin.txt to your webserver. Setup the permissions so that Checkin.php is accessible, but Checkin.txt is not:

chmod 644 CheckIn.php
chmod 600 CheckIn.txt



4a. Add an entry to CheckIn.txt whenever there is a new Internet connection. This only works if you are using NetworkManager. As root, create the file /etc/NetworkManager/dispatcher.d/CheckIn:

#!/bin/sh

if [ "$2" = "up" ]; then
wget http://URL_TO_CHECKIN.PHP?user=COMPUTER -O /dev/null > /dev/null 2> /dev/null
fi


and make it executable:

chmod +x /etc/NetworkManager/dispatcher.d/CheckIn



4b. Add an entry to CheckIn.txt every 10 minutes. Either as a regular user, or as root, create a crontab:

export EDITOR=`which pico`
crontab -e


The choice of editor is completely up to you. Put the following in your crontab:

*/10 * * * * sh /root/script.sh > /dev/null 2> /dev/null


where the "*/10" indicates that the command should be run every 10 minutes. Wikipedia has an great page on the format of crontabs.

Finally, check to make sure that the crontab was installed correctly:

crontab -l

No comments:

Post a Comment