#!/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;
}
|