<?php

/*
Script to check the config on Siae 
Will be others added in later version 
Started 15 Jan 2016 
JMcD.

*/


include('netconf/Device.php');

$username = "Ckendellen";
$password = "h2VEF4e2";


function pre($a){
	echo "<pre>";
	print_r($a);
	echo "</pre>";
}


function ping($target){
   $maxresponse = "0";
   $result = array();

   /* Execute Shell Command To Ping Target */

   $cmd_result = shell_exec("/bin/ping -c 6 -w 8 ". $target);

   /* Get Results From Ping */
   $result = explode(",",$cmd_result);

   // temp turn off the results of the ping.
   // pre($result);

   /* Return Server Status */
   if(eregi("0 received", $result[1])){
     return 'offline';
   }
   elseif(eregi("6 received", $result[1])){
     $maxresponse =  $result[3];
     return array('online', $maxresponse);
   }
   else{
   echo "NNN".$result[1]."MMM";
     return 'unknown';
   }
}


// Set of HI Devices:
$hidevices = array(
 "134" => "ASPECT",
 "119" => "BALLS",
 "106" => "BEACON",
 "136" => "BEWLEYS LEOP 136",
 "142" => "BEWLEYS LEOP 142",
 "138" => "BEWLEYS AIR",
 "102" => "BURLO",
 "120" => "CHRLSTWN",
 "137" => "CLARION",
 "117" => "CPBLN",
 "231" => "CRITICALDATA",
 "108" => "DC",
 "140" => "DUB PORT",
 "118" => "DUNL",
 "122" => "EXCHEQ",
 "111" => "GMO",
 "110" => "GSV",
 "105" => "IFC",
 "109" => "KKSF",
 "123" => "MALDRON",
 "124" => "METRO",
 "127" => "MOUNT",
 "132" => "NMS",
 "133" => "OGILVY",
 "115" => "REGUS",
 "113" => "SAGGART",
 "125" => "TC EX",
 "146" => "TC EDGEMX",
 "145" => "TC MPLS MX",
 "126" => "TC MX",
 "103" => "TC SRX3400",
 "121" => "TC SRX550",
 "144" => "TREASURY ACX",
 "141" => "TREASURY EX",
 "114" => "TRSF",
 "112" => "TRTC",
 "143" => "TS ACX",
 "100" => "TS EX",
);


// initial variables 
 $secondoctet = "";

// Must provide an ip 
if(empty($_GET['ip'])){
  echo '
    <!DOCTYPE html>
    <html>
     <head></head>
      <body>
      <center>Please Provide an IP Address</center></body></html>

    ';
  exit;
}



// IP VALIDATEOR - FROM MASSEY
if(isset($_GET['ip']) && !empty($_GET['ip'])){
    
    $ip = $_GET['ip'];
    
    if (filter_var($ip, FILTER_VALIDATE_IP) === false) {
        
    //    echo("$ip is not a valid IP address");

          echo '
               <!DOCTYPE html>
                 <html>
                     <head></head>
                     <body>
                        <center>
          ';

          echo $ip." is not a valid IP address</center>";
          echo "</body></html>";
          exit;
    } 
    else{
        $digit = explode(".", $ip) ;

        $secondoctet = $digit[1]; 

        // if(in_array($digit[0], array(10, 172, 192)) && end($digit)==254){
           if(in_array($digit[0], array(10, 172, 192))) {
            // echo 'Awesome';
            // print the html header
            echo '
               <!DOCTYPE html>
                    <html>
                        <head></head>
                        <body>
                        <center>
            ';
        }
        else{
            // echo 'Check failed';
            echo '
               <!DOCTYPE html>
                  <html>
                     <head></head>
                       <body>
                       <center>
            ';

            echo $ip." is not a valid IP address. IP address should start with 10,172,192 and end with 254</center>";
            echo "</body></html>";
            exit;

        }
    }
}



// }

// print the html tail
$ping = ping($ip);

echo "<B>OK Great the IP you provided is a private:".$ip."</B></center><p>";

// Procedure 1
// Present the user with the Switch name based on the 2nd Octet of the input address:
echo '
 <p>
1. <u>Get the Switch Name</u> <p>
The switch that this ip address is associated with is based on the second octet of the<p>
 ';
 
echo "ip address entered [".$secondoctet."]. This is typically a 3 digit number between 100 and 150.<p>";

// Check 2nd Octet is between 100 and 160.
// And if not quit
$lower_boundary = 100;
$upper_boundary = 150;

if(!(( $secondoctet >= $lower_boundary ) && ( $secondoctet <= $upper_boundary ))){
  echo "Sorry Second Octet of IP [".$secondoctet."] is outside range 100-150.<p>";
  echo "Going to end now<p>";
  echo "</body></html>";
  exit;
}

echo "Switch :". $hidevices[$secondoctet]. "<p>";

//
// CHECK : Ip address is pingable
// 

echo "<p>";

echo "$ip is ".$ping[0] ;

echo "<p>";

echo "MAX RESPONSE".$ping[1];

echo "<p>";


// CHECK : THE SWITCH CONF AND SET UP
// Checking for ... 
// for an interface.
// check for vlan mgm 
// check for vlan trf
// check for ip mgm
// check for ip trf
// check for dhcp on trf vlan
// check for mgm vlan member and vlan trf is just native
// check for ospf 


// Check the switch for a mention of this ip in the vlan interfaces.

echo "<p><hr width=\"100%\"><p>";

echo "2. <u>Check the Switch Configuration for mention of this ip on an vlan interfaces</u><p>";



    // Using Maldron LAB as a test
    // but will have to generate the ip of the swtich 
    $ip = "192.168.181.202";
    $d= new Device($ip, $username, $password);
    $d->connect();

    try
        {
            $inven=$d->get_system_information();
            $description = $inven->to_string();
            $descr_exist = true;

        }
    catch(Exception $e)
        {
            $description =  $e->getMessage();
            $descr_exist = false;
        }



    // $jconfchk1 = $d->run_cli_command("show configuration interfaces vlan | display set");
    // Get the full switch config.
    $jconfchk1 = $d->run_cli_command("show configuration | display set");


    $jconfchk_lines = explode("\n", $jconfchk1);

    // pre($jconfchk_lines);

    $d->close();
    // echo "\r\n HELLO <p>\r\n";
    // will have to update with the coorect str.

    // $searchword = '149.71.161.172';
    // $searchword = '10.123.8.1';
    $searchword =  '10.123.21.1';

    echo "<p>SEARCHWORD: ". $searchword."<p>\r\n";
    $matches = array();
    $vlan_line ="";
    echo "Any matching lines of the \"interface vlan\" will show between the next 2 horizontal lines<p>";
    echo "--------------------<p>";

    $i = 0 ; // counter of number of mentions of lins
    $di = 0; // counter of number of lines with deactivate of dissable
    $sn = 0; // count of number of lines with subnetmask NOT 24
    $searchword2 = $searchword."\/24";
    foreach($jconfchk_lines as $k => $v) {
      if (preg_match("/set interfaces vlan/", $v)) {

          if(preg_match("/\b$searchword\b/i", $v)) {
             $matches[$k] = $v;

             if(preg_match("/disable/i",  $v)) {
               $di = $di + 1;
             }
             if(preg_match("/deactivate/i",  $v)) {
               $di = $di + 1;
             }
             if(!(preg_match("/$seachword2/i",  $v))) {
               $sn = $sn + 1;
             }
        
             // echo "KEY:" . $k . " VALUE:" . $v . "\r\n";
             echo $v . "<p>\r\n";
             $vlan_line = $v;
             $i = $i + 1;
        
         }
      }
    }
    echo "--------------------<p>";
    
    $good1="<td bgcolor=\"#00FF00\">YES</td>"; // ip mentioned in just one line of vlan config
    $good2="<td bgcolor=\"#00FF00\">NO</td>"; // ip mentioned in more than one line of vlan config
    $good3="<td bgcolor=\"#00FF00\">FALSE</td>"; // ip mentioned in NO lines of vlan config
    $good4="<td bgcolor=\"#00FF00\">FALSE</td>"; // ip mentioned in lines of vlan config with disable or deactivate
    $good5="<td bgcolor=\"#00FF00\">FALSE</td>"; // ip mentioned in lines of vlan config with a subnet mask not 24
    if ($i == 1) { 
      $good1="<td bgcolor=\"#00FF00\">YES</td>";
    }
    else {
       $good1="<td bgcolor=\"#FF0000\">NO</td>";
    }
    if ($i > 1) {
      $good2="<td bgcolor=\"#FF0000\">YES (".$i.")</td>";
    } 
    if ($i == 0) {
      $good3="<td bgcolor=\"#FF0000\">TRUE</td>";
    }
    if ($di > 0) {
      $good4="<td bgcolor=\"#FF0000\">TRUE</td>";
    }
    if ($sn != 0) {
      $good5="<td bgcolor=\"#FFFF00\">SUSPICOUS</td>";
    }
    echo "<p><table with=\"80%\" border=\"1\">";
    echo "<tr><td>IP Mentioned on just one line of vlan config</td>";
    echo $good1."</tr>";
    echo "<tr><td>IP Mentioned on more than one line of vlan config</td>";
    echo $good2."</tr>";  
    echo "<tr><td>IP Mentioned on NO lines of vlan config</td>";
    echo $good3."</tr>";
    echo "<tr><td>IP Mentioned on lines of vlan config with disabled or deactivate</td>";
    echo $good4."</tr>";
    echo "<tr><td>IP Mentioned on lines of vlan config with a subnet not \"/24\"</td>";
    echo $good5."</tr>";
    echo "</table>";


    // Failed on this TEST Going to Exit Now
    if (($i != 1) || ($di != 0) ) {
      echo "<p>Failed this test - Quit Checking now<p>";
      echo "</center></body></html>";
      exit;
   }

// Check the switch for a mention if the vlan number and the interface number with the ip

echo "<p><hr width=\"100%\"><p>";

echo "3. <u>Check the Switch Configuration for the Interface that has the vlan (that has the management IP)</u><p>";

   // echo "<p>VLAN LINE :". $vlan_line."<p>";
   // set interfaces vlan unit 4008 family inet address 10.123.8.1/24
   $vlanchk_element = explode(" ", $vlan_line);
   // echo "<p>VLAN ID: ".$vlanchk_element[4]."<br>";

   // Seach the config file for the vlan name by using its vlan-id 
   foreach($jconfchk_lines as $k => $v) {
      if (preg_match("/ vlan-id $vlanchk_element[4]\b/i", $v)) {

             // echo "KEY:" . $k . " VALUE:" . $v . "\r\n";
             // echo "GG:". $v . "<p>\r\n";
             $vlan_id_line = $v;
             // $i = $i + 1;

      }
    }
    $vlan_id_element =  explode(" ", $vlan_id_line);
    // set vlans ge21_MGMT vlan-id 4021
    // echo "VLAN NAME : ".$vlan_id_element[2]."<br>";
    // Now have the name of the vlan so have to get the Interface.
    // echo "<p>The config lines for the relevant physical interface should show between the next 2 horizontal lines<p>";
    // echo "--------------------<p>";

   foreach($jconfchk_lines as $k => $v) {
      if (preg_match("/ ethernet-switching vlan members $vlan_id_element[2]\b/i", $v)) {

             // echo "KEY:" . $k . " VALUE:" . $v . "\r\n";
             // echo "<p>HH:". $v . "<p>\r\n";
             $iface_line = $v;
             // $i = $i + 1;

      }
    }
    $iface_element =  explode(" ", $iface_line);
    // echo "<p>IFACE:". $iface_element[2]."<p>";
    $iface_esc = str_replace('/', '\/', $iface_element[2]);
    // set interfaces ge-0/0/21 unit 0 family ethernet-switching vlan members ge21_MGMT

    echo "The config lines for the relevant physical interface should show between the next 2 horizontal lines<p>";
    echo "--------------------<p>";

   $dib = 0; // counter for disaled or deactivated
   $tc = 0;  // counter for check there is mention of trunk for the interface.
   $fi = 0;  // family inet check counter 
   $nv = 0;  // counter for native vlan
   $natvlan_line = "";
   $mc = 0;  // counter of lines with  vlan members should be 1
   foreach($jconfchk_lines as $k => $v) {
      if (preg_match("/ interfaces $iface_esc\b/i", $v)) {

             if(preg_match("/disable/i",  $v)) {
               $dib = $dib + 1;
             }
             if(preg_match("/deactivate/i",  $v)) {
               $dib = $dib + 1;
             }
             if(preg_match("/ trunk/i",  $v)) {
               $tc = $tc + 1;
             }
             if(preg_match("/ family inet/i",  $v)) {
               $fi = $fi + 1;
             }
             if(preg_match("/ vlan members /i",  $v)) {
               $mc = $mc + 1;
             }
             if(preg_match("/ native-vlan-id /i",  $v)) {
               $nv = $nv + 1;
               // echo "<p>JJJJ<p>";
               $natvlan_line = $v;
             }
             // echo "KEY:" . $k . " VALUE:" . $v . "\r\n";
             echo "<p>". $v . "<p>\r\n";
             // $iface_line = $v;
             // $i = $i + 1;

      }
    }

    echo "--------------------<p>";

    $natvlanid_element = explode(" ", $natvlan_line);
    //$vlanchk_element[4]  $natvlanid_element[8]

    // check the MGT vlan does not equal the native vlan.
    
    echo "<p> NATIVE VLAN: ".$natvlanid_element[8]."<br>";
    echo "MGMT VLAN: ".$vlanchk_element[4]."<br>";
    echo "IFACE:". $iface_element[2]."<p>";
    $good1="<td bgcolor=\"#00FF00\">YES</td>"; // interface does not mention disabled or deactivate
    $good2="<td bgcolor=\"#00FF00\">YES</td>"; // interface does mention trunk.
    $good3="<td bgcolor=\"#00FF00\">YES</td>"; // interface does NOT mention inet
    $good4="<td bgcolor=\"#00FF00\">YES</td>"; // interface just one vlan member 
    $good5="<td bgcolor=\"#00FF00\">YES</td>"; // there is a native vlan
    $good6="<td bgcolor=\"#00FF00\">YES</td>"; // there is just one native vlan
    $good7="<td bgcolor=\"#00FF00\">YES</td>"; // native vlan is not same as member

    if ($dib > 0) {
      $good1="<td bgcolor=\"#FF0000\">NO</td>";
    }
    
    if ($tc != 1) {
      $good2="<td bgcolor=\"#FF0000\">NO</td>";
    }
    if ($fi != 0) {
      $good3="<td bgcolor=\"#FF0000\">NO</td>";
    }
    if ($mc != 1) {
      $good4="<td bgcolor=\"#FF0000\">NO</td>";
    }
    if ($nc == 1) {
      $good5="<td bgcolor=\"#FFFF00\">NO</td>";
    }
    if ($nc > 1) {
      $good6="<td bgcolor=\"#FFFF00\">NO</td>";
    }
    if ($vlanchk_element[4] == $natvlanid_element[8]) {
       $good7="<td bgcolor=\"#FFFF00\">NO</td>";
    }
    echo "<p><table with=\"80%\" border=\"1\">";
    echo "<tr><td>Interface ".$iface_element[2]." Mentioned in Config with  with disabled or deactivate</td>";
    echo $good1."</tr>";
    echo "<tr><td>Interface ".$iface_element[2]." Mentioned with trunk in Config just once</td>";
    echo $good2."</tr>";
    echo "<tr><td>Interface ".$iface_element[2]." Mentioned with \" inet \" config</td>";
    echo $good3."</tr>";
    echo "<tr><td>Interface ".$iface_element[2]." Mentioned with just one vlan member</td>";
    echo $good4."</tr>";
    echo "<tr><td>Interface ".$iface_element[2]." Mentioned with a Native vlan</td>";
    echo $good5."</tr>";
    echo "<tr><td>Interface ".$iface_element[2]." Mentioned with just one Native vlan</td>";
    echo $good6."</tr>";
    echo "<tr><td>Interface ".$iface_element[2]." Has Member vlan (MGMT ".$vlanchk_element[4].") different from Native vlan (Traffic ".$natvlanid_element[8]." </td>";
    echo $good7."</tr>";
    echo "</table>";

// Do a run of the config file for the management vlan
// set interfaces ge-0/0/21 unit 0 family ethernet-switching vlan members ge21_MGMT
// set vlans ge21_MGMT vlan-id 4021
// set vlans ge21_MGMT l3-interface vlan.4021
// check that it matchs the vlan-id and NOT the traffic one.


    echo "<p><hr width=\"100%\"><p>";

    echo "4. <u>Check the Switch Configuration for the interface the Mgmt vlan is tied to</u><p>";

    // VLAN NAME : $vlan_id_element[2]

   // echo "<p>VLAN LINE :". $vlan_line."<p>";
   // set interfaces vlan unit 4008 family inet address 10.123.8.1/24
   // $vlanchk_element = explode(" ", $vlan_line);
   // echo "<p>VLAN ID: ".$vlanchk_element[4]."<br>";

   // Seach the config file for l3 iface of the MGMT vlan.

   $vlan_iface_line ="";
   $vic = 0; // counter for layer3 lines mgmt
   foreach($jconfchk_lines as $k => $v) {
      if (preg_match("/set vlans $vlan_id_element[2]/i", $v)) {

          if (preg_match("/l3-interface/i", $v)) {

             // echo "KEY:" . $k . " VALUE:" . $v . "\r\n";
             // echo "GG:". $v . "<p>\r\n";
             $vlan_iface_line = $v;
             $vic = $vic + 1;
          }
      }
    }
    $vlan_iface_element =  explode(".", $vlan_iface_line);

    echo $vlan_iface_line."<p>";

    echo "VLAN IFACE :". $vlan_iface_element[1]."<p>";

    $osm = 0; // OSFP line counter
    $vie = trim($vlan_iface_element[1]);
    foreach($jconfchk_lines as $k => $v) {
      if (preg_match("/set protocols ospf area /i", $v)) {

          // echo "KKKK ".$v."<p>";
          // echo "PPP interface vlan.".$vie."<p>";

          if (preg_match("/interface vlan.$vie/i", $v)) {

             // echo "KEY:" . $k . " VALUE:" . $v . "\r\n";
             echo $v . "<p>\r\n";
             $ospf_mgmt_line = $v;
             $osm = $osm + 1;
          }
      }
    }

    $good1="<td bgcolor=\"#00FF00\">YES</td>"; // interface mgmt vlan have a l3 interface
    $good2="<td bgcolor=\"#00FF00\">YES</td>"; // does this match with the vlan number
    $good3="<td bgcolor=\"#00FF00\">YES</td>"; // does it differ with TRF vlan number
    $good4="<td bgcolor=\"#00FF00\">YES</td>"; // is this interface in OSPF
    $good5="<td bgcolor=\"#00FF00\">YES</td>"; // does it have the keyword passive


    if ($vic == 0) {
      $good1="<td bgcolor=\"#FF0000\">NO</td>";
    }
    if (trim($vlanchk_element[4]) != trim($vlan_iface_element[1])) {
      $good2="<td bgcolor=\"#FF0000\">NO V:".$vlanchk_element[4]." X:".$vlan_iface_element[1]."</td>";
    }
    if ($natvlanid_element[8] == $vlan_iface_element[1]) {
      $good3="<td bgcolor=\"#FF0000\">NO</td>";
    }
    if ($osm != 1) {
      $good4="<td bgcolor=\"#FF0000\">NO".$osm."</td>";
    }
    if (!(preg_match('/passive/',$ospf_mgmt_line))) {
      $good5="<td bgcolor=\"#FF0000\">NO</td>";
    }

    echo "<p><table with=\"80%\" border=\"1\">";
    echo "<tr><td>Interface vlan".$vlan_iface_element[1]." has a layer3 interface</td>";
    echo $good1."</tr>";
    echo "<tr><td>Interface vlan".$vlan_iface_element[1]." does the interface vlan number match the Mgmt vlan number <br>(usual it does but strictly it does not have to)</td>";
    echo $good2."</tr>";
    echo "<tr><td>Interface vlan".$vlan_iface_element[1]." does the interface vlan number DIFFER from the TRF vlan (".$natvlanid_element[8].")</td>";
    echo $good3."</tr>";
    echo "<tr><td>Interface ".$iface_element[2]." Is the interface vlan put into OSPF<br>".$ospf_mgmt_line."</td>";
    echo $good4."</tr>";
    echo "<tr><td>Interface ".$iface_element[2]." does OSPF have the passive keyword</td>";
    echo $good5."</tr>"; 

    
    // echo "<p>END HELLO<p>";
echo '

</center>
</body>
</html>

 ';



?>


