Home
California Web Design
eMarketing Solutions
Search Optimization
Multimedia & Graphics
Clients
Consultations
News / Content
Contact SJScott
SEARCH
 
Business Information
  SJScott News and Information
IP Address Tracking in Perl PDF Print E-mail
Written by LIQID   
Thursday, 14 April 2005
Here is a little support tool for keeping tracking of an ever changing IP addressTracking of client's public IP Address for support or maintenance.

I have been challenged by my ISP, who by character tends to not assign me an IP address for any length of time. I love to get my system online over VNC or some other desktop sharing app when I am at work so I can keep up on my mail with out having to move my 8 addresses to all the places that I go. I am sure that I could just sync them to a handheld or whatever. Also my family and other clients want me to support them and at any time their lousy dial up has switched between any number of addresses. So here is what I came up with for a solution. I have a simple script that will hit any website and that site will use a cgi script to record the IP.

I have needed a tool like this so I will be posting this here in hopes that someone else will get some benefit from it. I am also hoping to add as much to my blog about support tricks that I have found so us geeks out there can better support our non-geek friends.

First you will need a client script running any language you want, but just for convenience I have included a perl flavor so you will have to install perl or write a simple app to hit the URL in the perl scripts below. It only requires for you to hit or open the site with a system and and optional comments. Second you will need to have this perl script running on the server. That is if you have a standard apache install pushing your websites.

I hope this will be useful to you in the future.
Regards,
LIQID



The File to place on your server: ipreport.cgi

#!/usr/bin/perl -wT
# ipreport.cgi- this will set on the host
#######################################################################
# your local app will his this CGI script with a couple of possible 
# attributes 1 = the system name and 2 = some extra text about whatever
# if there are no elements in the URL then all of the ip addresses so far 
# will be printed to the screen

$| = 1; # auto flush

print ("Content-Type: text/htmlnn");
use strict;
use CGI;
use CGI (":all");
use DBI;

my $q = CGI->new();
my $ip = $ENV{'REMOTE_ADDR'} ;


my $serverName = "localhost";
my $serverPort = "3306";
my $serverUser = "account";
my $serverPass = "password";
my $serverDb   = "database";
my $serverTabl = "thetable";

my $compName   = $q->param("name");
my $extra      = $q->param("extra");
my $ret        = '';

# check to see if there is a computername in the URL
if($compName) {
	# if there is a comp name then check to see if that name already exists
	if (checkRecord()) {
	  $ret = update_ip();
	} else {
	# if it doesnt exist then we will need to insert a new reord
	  $ret = insert_ip();
    }
    print $ret;
} else {
  # this should probably be changes since there is going to be a list of machines with 
  # open ports available to anyone who runs this app, but for simplicity I have stuck this in here
  print start_html("IP Registry");
  print h1("View All IPs Listed here");
  print "Remember you can't add anything unless you have a **sysname** attribute in your url",br;
  print "Latest Logs",hr;
  showall();
  print end_html;
}


sub insert_ip () {
	 # register the variabes
     my ($dbo, $success, $str, $rso);
     
     # initialize the database object with a url to you server
     $dbo = DBI->connect("DBI:mysql:database=$serverDb;host=$serverName",$serverUser,$serverPass) 
     	|| die ('cant connect to the DB');
     	
     # our insert string
	 $str = ("INSERT INTO $serverTabl ( Name, IP, Extra )
               VALUES( '$compName', '$ip', '$extra' ) ");

	 # execute the query
     $success = $dbo->do($str);
     $dbo->disconnect;
     
     if($success != 1) {
          return "Sorry, the database was unable to add your entry. n Please try again later.";
     } else {
          return;
     }
}

sub update_ip () {
     my ($dbo, $success, $str, $rso);
     
     $dbo = DBI->connect("DBI:mysql:database=$serverDb;host=$serverName",$serverUser,$serverPass) 
     	|| die ('cant connect to the DB');
     	
	 $str = ("UPDATE $serverTabl SET IP = '$ip', Extra = '$extra', hits = hits+1
	 	WHERE Name = '$compName' ");

     $success = $dbo->do($str);
     $dbo->disconnect;
     
     if($success != 1) {
          return "Sorry, the database was unable to update the DB for $compName. n Please try again later.";
     } else {
          return;
     }
}


sub checkRecord() {
     my ($dbo, $rso, $str);
     
     $dbo = DBI->connect("DBI:mysql:database=$serverDb;host=$serverName",$serverUser,$serverPass) 
     	|| die ('cant connect to the DB');
     	
	 $str = ("SELECT id FROM $serverTabl WHERE Name = '$compName' ");
     $rso = $dbo->prepare($str);
     $rso->execute;
     
     if($rso->rows == 0) {
          return 0;
     } else {
          return 1;
     }

}

sub showall() {
	
     my ($dbo, $rso, @row);

     $dbo = DBI->connect("DBI:mysql:database=$serverDb;host=$serverName",$serverUser,$serverPass) 
      || die ('can not get into the db');
      
     # here we are going to ask for the record set back from MySQL database but we will add a little
     # spice in here and use some of the great built in functionality from the database to format the
     # date and time into a readable format and thus keeping us from having to do string manipulation
     # on the raw returned value
     $rso = $dbo->prepare("SELECT id,Name,DATE_FORMAT(Report_Date,'%W %m/%d/%Y %H:%i'),IP,Extra,hits
     		FROM $serverTabl ORDER BY Report_Date ");
     $rso->execute;
     while(@row = $rso->fetchrow_array) {
          print "id: ", $row[0], br, "n";
          print "System Identity: ", $row[1], br, "n";
          print "Last Update: ", $row[2], br, "n";
          print "IP: ", $row[3], br, "n";
          print "Extra Comments: ", $row[4], br, "n";
          print "Times Executed: ", $row[5], br, "n";
          print hr;
     }
     $rso->finish;
     $dbo->disconnect;
}



And below is the ultra simple perl script for the client system.
#!/usr/bin/perl -w
# ipget.pl- the script that addresses the host that will save your IP for you. Run this on the box 
# whose IP you want to figure out. 
# you will need to have the system name declared here and any extra comments that you want to post
use LWP::Simple;
get("http://your.host.com/ipreport.cgi?name=YourSystemName&extra=Any_Extra_Comments");
Last Updated ( Friday, 22 April 2005 )
< Prev   Next >
 
CASE STUDIES
 
BUSINESS SOLUTIONS
   
   
 
xhtml compliant Get Firefox! Get RSSOwl
SJScott Enterprise
Simi Valley, CA. 93063
(805)501-9286
Webmaster@SJScott.net