From: paulr Date: Mon, 13 Sep 2010 02:57:41 +0000 (+1000) Subject: a semi-functional bit of code... lots of todos and error checking X-Git-Url: http://git.pjr.cc/?a=commitdiff_plain;h=fd8bc263bd1e3923c94f949059d90d93b5faaf76;p=PHPIPManager.git a semi-functional bit of code... lots of todos and error checking --- diff --git a/lib/db.php b/lib/db.php index 05216dc..6c6a4d5 100644 --- a/lib/db.php +++ b/lib/db.php @@ -30,6 +30,7 @@ class db { error_log("db open for init"); $this->init(); } + } public $dbobject = ""; diff --git a/lib/ip.php b/lib/ip.php index a0287f3..585643c 100644 --- a/lib/ip.php +++ b/lib/ip.php @@ -5,6 +5,7 @@ function ipversion($ip) $ipv4 = ereg('^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$', $ip); $ipv6 = ereg("^[0-9a-fA-F:]+$", $ip); + error_log("ipversion, $ip, $ipv4, $ipv6"); if($ipv4 == 1) return 4; if($ipv6 == 1) return 6; return 0; @@ -98,7 +99,7 @@ class ip { if($ver == 4) { if($mask != 0) { - + return $ip; } } } @@ -147,7 +148,7 @@ class ip { if($ver == 4) { if($mask != 0) { - + return $ip; } } } diff --git a/lib/runscan.php b/lib/runscan.php new file mode 100644 index 0000000..ceb648b --- /dev/null +++ b/lib/runscan.php @@ -0,0 +1,70 @@ +connect(); + +$fname = "/var/tmp/.phpipman.".time(); + +if($argc != 2) { + echo "usage: ".$argv[0]." subnet-id"; +} + +$res = $db->dbobject->query("select * from subnet where sn_id='".$argv[1]."'"); +$id = $argv[1]; + +foreach($res as $row) { + //("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT); + $sn_ip = $row["sn_ip"]; + $sn_mask = $row["sn_mask"]; +} + +if(ipversion($sn_ip)!=4) return; + +system("/usr/bin/nmap -oX $fname -sP $sn_ip/$sn_mask > /dev/null 2>&1"); + +//$fname = "/var/tmp/.phpipman.1284344553"; +echo "Output in $fname\n"; + +$sml = simplexml_load_file("$fname"); + +foreach($sml as $row => $key) { + //echo "row: $row\n"; + if($row == "host") { + //print_r($key); + $status = $key->status["state"]; + $ha = $key->address["addr"]; + //echo "host address: $ha\n"; + $hname = ""; + if(isset($key->hostnames->hostname["name"])) { + //echo "was set: ".$key->hostnames->hostname["name"]."\n"; + //exit(0); + $hname = $key->hostnames->hostname["name"]; + } + + //"hosts" ("ho_id" INTEGER PRIMARY KEY AUTOINCREMENT,"ho_sn_id" INTEGER,"ho_ip" TEXT,"ho_name" TEXT,"ho_desc" TEXT);'; + $doadd = false; + if($status == "up") { + $doadd = true; + if($hname == "") $hname = $ha; + echo "up: $ha, $hname\n"; + } else { + if($hname != "") { + $doadd = true; + echo "down: $ha, $hname\n"; + } + } + + if($doadd) { + $sql = "delete from hosts where ho_ip=='$ha'"; + $db->dbobject->query($sql); + $sql = "insert into hosts values (NULL, '$id', '$ha', '$hname', '')"; + $db->dbobject->query($sql); + echo "added, $ha, $hname\n"; + } + } + +} +unlink($fname); + +?> \ No newline at end of file diff --git a/lib/www.php b/lib/www.php index f9648b9..17140f5 100644 --- a/lib/www.php +++ b/lib/www.php @@ -3,12 +3,17 @@ $actionRegister["addsuper"] = "www_ip_addSuperRange"; $actionRegister["allocate"] = "www_ip_allocateRange"; $actionRegister["allocatesub"] = "www_ip_allocateSubRange"; +$actionRegister["deletesub"] = "www_ip_deleteSubRange"; $actionRegister["modsubnet"] = "www_ip_modifySubnet"; $actionRegister["dumpdb"] = "www_db_dumpdb"; $actionRegister["restoredb"] = "www_db_restoredb"; $actionRegister["changesubdetails"] = "www_ip_changesubdetails"; $actionRegister["addhosts"] = "www_ip_addhost"; $actionRegister["deletehost"] = "www_ip_deletehost"; +$actionRegister["modifyhost"] = "www_ip_modifyhost"; +$actionRegister["updatehost"] = "www_ip_updatehost"; +$actionRegister["scansub"] = "www_ip_scansub"; +$actionRegister["scansubreal"] = "www_ip_scansubReal"; class www { function Go() { @@ -74,9 +79,11 @@ Description $res = $db->dbobject->query("select * from supernet"); foreach($res as $row) { //echo "
";
-		//	print_r($row);
-		//	echo "

"; - echo ""; + // print_r($row); + // echo "
"; + if(ipversion($row["sn_ip"]) == 6) $ip6trans = "::0"; + else $ip6trans = ""; + echo "
".$row["sn_name"]."".$row["sn_ip"]."::0/".$row["sn_mask"]."".$row["sn_desc"]."
"; echo ""; echo ""; echo ""; @@ -89,10 +96,15 @@ Description $subnet = $row2["sn_ip"]; $mask = $row2["sn_mask"]; $desc = $row2["sn_desc"]; + + if(ipversion($row2["sn_ip"]) == 6) $ip6trans = "::0"; + else $ip6trans = ""; + echo ""; - echo ""; + echo ""; echo ""; //echo ""; + if(ipversion($row2["sn_ip"]) == 4) echo ""; echo ""; } echo "
".$row["sn_name"]."".$row["sn_ip"]."$ip6trans/".$row["sn_mask"]."".$row["sn_desc"]."Allocate SubnetDelete Supernet
  • $name
  • $subnet::0/$mask$desc
  • $name
  • $subnet$ip6trans/$mask$descDelete SubnetAdd HostScan Subnet

    "; @@ -126,6 +138,13 @@ Description function footer() { + echo "
    ";
    +		//print_r($_SERVER);
    +		
    +		//print_r($_REQUEST);
    +		
    +		//print_r($GLOBALS);
    +		echo "
    "; ?> } } +function www_ip_scansub() +{ + global $db, $wwwConnector; + + $id = $_REQUEST["id"]; + + $res = $db->dbobject->query("select * from subnet where sn_id=='$id'"); + + foreach($res as $row) { + $snip = $row["sn_ip"]; + $snmask = $row["sn_mask"]; + $snname = $row["sn_name"]; + } + $wwwConnector->header(); + $wwwConnector->printError(); + +?> +You are about to scan the subnet how would you like entries added:
    +
  • Replace all default entries (i.e. where the name and the ip address are the same) where hosts are UP (reply to ping) +
  • Replace all entries where hosts are UP (reply to ping) +
  • Add any entries where hosts are UP (reply to ping) and an entry does not already exist in the database +
  • Add any entries where hosts are UP or Down and an entry does not already exist in the database, but the IP has a reverse hostname entry +
  • Delete all entries and replace with hosts that are Up or Down and have reverse hostname entries +footer(); +} + +function www_ip_scansubReal() +{ + global $db, $wwwConnector; + + $id = $_REQUEST["id"]; + + system("/usr/bin/php ../lib/runscan.php $id > /tmp/out.phpipman 2>&1 &"); + error_log("would exec... nmap -sP -oX $fname $sn/$sm"); + + header("Location: index.php"); +} + function www_ip_addSuperRange() { global $db; @@ -185,6 +244,18 @@ function www_ip_allocateRange() $wwwConnector->footer(); } +function www_ip_deleteSubRange() +{ + global $db, $wwwConnector; + + $id = $_REQUEST["id"]; + //("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);'; + $sql = "delete from subnet where sn_id=='$id'"; + $res = $db->dbobject->query($sql); + + header("Location: index.php?notice=Deleted\n"); +} + function www_ip_allocateSubRange() { global $db, $wwwConnector; @@ -232,7 +303,7 @@ Subnet Description:

    Add Host

    Hostname -IP Address "> +IP Address Description @@ -250,7 +321,7 @@ Description $desc = $row["ho_desc"]; $ip = $row["ho_ip"]; $hid = $row["ho_id"]; - echo "$ip$name$descDelete"; + echo "$ip$name$descDelete"; } ?> @@ -262,6 +333,56 @@ Description $wwwConnector->footer(); } +function www_ip_updatehost() +{ + global $db, $wwwConnector; + + $hname = $_REQUEST["hostname"]; + $hip = $_REQUEST["hostip"]; + $hdesc = $_REQUEST["hostdesc"]; + $sid = $_REQUEST["superid"]; + $hid = $_REQUEST["hid"]; + + // ("ho_id" INTEGER PRIMARY KEY AUTOINCREMENT,"ho_sn_id" INTEGER,"ho_ip" TEXT,"ho_name" TEXT,"ho_desc" TEXT) + $sql = "update hosts set ho_name='$hname',ho_ip='$hip',ho_desc='$hdesc' where ho_id='$hid'"; + $db->dbobject->query($sql); + + header("Location: ?action=modsubnet&id=$sid"); +} + +function www_ip_modifyhost() +{ + global $db, $wwwConnector; + + $id = $_REQUEST["hid"]; + + $res = $db->dbobject->query("select * from hosts where ho_id=='$id'"); + + foreach($res as $row) { + $hname = $row["ho_name"]; + $hip = $row["ho_ip"]; + $sid = $row["ho_sn_id"]; + $hdesc = $row["ho_desc"]; + } + + $wwwConnector->header(); + $wwwConnector->printError(); + ?> +
    + + + + + + +
    Host Name
    Host IP
    Description
    +
    +">Back +footer(); +} + function www_ip_deletehost() { global $db, $wwwConnector;