Location: PHPKode > projects > PortWatcher > PortWatcher/switch_man.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
	<link rel="stylesheet" href="index.css" type="text/css" />
	<title>Lanifex PortWatcher</title>
	<script src="validator.js"></script>
</head>
<body>
	<div class="header">
	<a href="http://www.lanifex.com"><img src="img/logo.jpg" alt=""></a>
	</div>
	<div class="menu">
			<a href="index.html"><span>Home</span></a>
			<a href="MACUpdate.php"><span>SNMP scan</span></a>
			<a href="MACTools.php"><span>DB review</span></a>
			<a href="switch_man.php"><span>Switch management</span></a>
	</div>
	<div class="content">
		<h2>Switch Management</h2>
		<?
			function checkValidIP($ip)
			{
			   if(!eregi("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$", $ip)) $return = FALSE;
			   else $return = TRUE;
			     
			   $tmp = explode(".", $ip);
			   if($return == TRUE){
			      foreach($tmp AS $sub){
			              $sub = $sub * 1;
			                if($sub<0 || $sub>256) $return = FALSE;
			      }
			   }
			   return $return;
			}
			
			function checkValidMAC($mac)
			{
				$ValidChars = "0123456789abcdef";
				$mac = strtolower($mac);
				
				if (strlen($mac) != 12) return false;
				
				for ($i = 0; $i <12;  $i++)
				{
					if (strpos($ValidChars, $mac[$i]) === false )	return false;
				}
				return true;
			}
	
			function CleanArray($array) 
			{
				foreach ($array as $key => $value) 
				{
					$value = str_replace(";", "", $value); 
					$value = str_replace("=", "", $value); 
					$array1[$key] = mysql_real_escape_string($value);
				}
				return $array1;
			} 
			
	$message = $_GET['message'];
	$error_msg = $_GET['error_msg'];
	include("config.php");
	$db = mysql_connect($dbserver, $dbuser,$dbpasswd);
	mysql_select_db($dbname,$db);

			if ($_POST['test_s'])
			{
					$IPres = mysql_query("SELECT ipaddress FROM switches WHERE switch_ID=\"$_POST[switches]\"");
					$IP = mysql_fetch_row($IPres);
					//$line = exec('ping -c5 -i0.2 -f '.$IP[0], $bla,  $ping_result);
					$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
					socket_set_nonblock($sock);
					socket_connect($sock, $IP[0], 23);
					socket_set_block($sock);
					switch(socket_select($r = array($sock), $w = array($sock), $f = array($sock), 5))
					{
					        case 2:
					                $ping_result = 1;
					                break;
					        case 1:
					                $ping_result = 0;
					                break;
					        case 0:
					                $ping_result = 1;
					                break;
					}
					if ($ping_result == 0)
					{
						$message = "<p id=\"message_y\">The switch is on and is reachable.</p>";
					}
					else
					{
						$message = "<p id=\"message_n\">The switch is off or is not reachable.</p>";
					}
			}
			if ($_POST['add_s'])
			{
				$valid_s = true;
				$valid_i = true;
				$valid_m = true;
				
				if ($_POST[sensip] != "")
				{
					$valid_s = checkValidIP($_POST[sensip]);
				}
				if ($_POST[ip] != "")
				{
					$valid_i = checkValidIP($_POST[ip]);
				}
				if ($_POST[mac] != "")
				{
					$valid_m = checkValidMAC($_POST[mac]);
				}
				if ($valid_s && $valid_i && $valid_m)
				{
					$ins = CleanArray($_POST);
					$insertResult = mysql_query("INSERT INTO switches (location, model, serialno, mac, comment, ipaddress, function, dnsname, status, vlan, OS, portfilter, sensorIP, community_string, snmp_version) VALUES (\"$ins[location]\", \"$ins[model]\", \"$ins[snumber]\", \"$ins[mac]\", \"$ins[comments]\", \"$ins[ip]\", \"$ins[func]\", \"$ins[dnsname]\", \"$ins[status]\", \"$ins[vlans]\", \"$ins[os]\", \"$ins[pfilter]\", \"$ins[sensip]\", \"$ins[comstring]\", \"$ins[snmpver]\")",$db);
					$error_msg = "<font color=green>The switch was succesfully added to the database.</font><br/>"; 
		            header("Location: switch_man.php?error_msg=$error_msg");
				}
				else
				{
						$error_msg = "";
						if (!$valid_s)
							$error_msg .= "<font color=red>Sensor IP Address was entered incorrectly</font><br/>"; 
						if (!$valid_i)
							$error_msg .= "<font color=red>IP Address was entered incorrectly</font><br/>"; 
						if (!$valid_m)
							$error_msg .= "<font color=red>MAC Address was entered incorrectly</font><br/>"; 
		                header("Location: switch_man.php?error_msg=$error_msg"); 
				}
			}
			else
			{
				echo $message;
				echo $error_msg;
			?>
				<form method='post' action='switch_man.php'>
					<fieldset class="general">
						<legend>Test switch</legend>
						<p><label for="switches">Select switch:</label><select name='switches' id='switches'>
						<?
							$stacksResult = mysql_query("SELECT switch_ID,dnsname FROM switches ORDER BY dnsname");
						    while ($stacksrow = mysql_fetch_array($stacksResult)) 
							{
								$bsname = $stacksrow['dnsname'];
								$id = $stacksrow['switch_ID'];
						        echo "<option value = '$id' >$bsname</option>\n";
						    }
						?>
						</select>
						<input type='Hidden' name='test_s' value='1'>
						<span  class="submit"><input type="submit" value="Test" /></span></p>
					</fieldset>
				</form>
				<form method='post' action='edit.php'>
					<fieldset class="general">
						<legend>Edit switch</legend>
						<p><label for="switches">Select switch:</label><select name='switches' id='switches'>
						<?
							$stacksResult = mysql_query("SELECT switch_ID,dnsname FROM switches ORDER BY dnsname");
						    while ($stacksrow = mysql_fetch_array($stacksResult)) 
							{
								$bsname = $stacksrow['dnsname'];
								$id = $stacksrow['switch_ID'];
						        echo "<option value = '$id' >$bsname</option>\n";
						    }
						?>
						</select>
						<span  class="submit"><input type="submit" value="Edit" /></span></p>
					</fieldset>
				</form>
				<form method='post' action='switch_man.php' onsubmit='return submitIt(this)'>
					<fieldset class="general">
						<legend>Add new switch</legend>
						<p><label for="dnsname">DNS Name:</label> <input type="text" name='dnsname' value='' id="dnsname" /></p>
						<p><label for="model">Model:</label> <input type="text" name='model' value='' id="model" /></p>
						<p><label for="func">Function:</label><select name='func' id='func'>
							<option value = 'S' >Switch</option>
							<option value = 'R' >Router</option>
						</select></p>
						<p><label for="snumber">Serial Number:</label> <input type="text" name='snumber' value='' id="snumber" /></p>
						<p><label for="location">Location:</label> <input type="text" name='location' value='' id="location" /></p>
						<p><label for="mac">MAC Address:</label> <input type="text" name='mac' value='' id="mac" /></p>
						<p><label for="ip">IP Address:</label> <input type="text" name='ip' value='' id="ip" /></p>
						<p><label for="vlans">VLAN(s):</label> <input type="text" name='vlans' value='' id="vlans" /> If multiple - separate by spaces.</p>
						<p><label for="status">Status:</label><select name='status' id='status'>
							<option value = '1' >ON</option>
							<option value = '0' >OFF</option>
						</select></p>
						<p><label for="os">OS:</label> <input type="text" name='os' value='' id="os" /></p>
						<p><label for="sensip">Sensor IP:</label> <input type="text" name='sensip' value='' id="sensip" /></p>
						<p><label for="comstring">Community String:</label> <input type="text" name='comstring' value='' id="comstring" /></p>
						<p><label for="snmpver">SNMP version:</label>
						<select name='snmpver' id='snmpver'>
							<option value = '1' >1</option>
							<option value = '2' >2</option>
							<option value = '2c' >2c</option>
							<option value = '3' >3</option>
						</select></p>
						<p><label for="pfilter">Ignore port(s):</label> <input type="text" name='pfilter' value='' id="pfilter" /> If multiple - separate by spaces.</p>
						<p><label for="comments">Comments:</label><textarea name="comments", rows='10', cols='16'></textarea></p>
						<input type='Hidden' name='add_s' value='1'>
						<span  class="submit"><input type="submit" name="submit" value=" Add " /></span><span  class="submit"><input type="reset" name="reset" value=" Reset " /></span>
					</fieldset>
				</form>
			<?
			}
			?>
		
		
		<div class="clear"></div>
	</div>
	<div class="footer"></div>
</body>
</html>
Return current item: PortWatcher