Gheek.net

February 14, 2008

bridgeimportip_php.txt

Filed under: ipplan, php, switchmap — Tags: , , — lancevermilion @ 2:24 pm
GetIPDetails($baseindex, $ipaddr)) {
//                // log the entire row to the auditlog for history - field
//                // is too small!
//                // probably better to rather just insert into a duplicate
//                // ipaddr table, or to have a trigger event do something
//                $row["event"]=130;
//                $row["user"]="Switchmap Bridge";
//                $row["action"]="log old row contents";
//                $row["baseindex"]=$baseindex;
//                $row["ip"]=inet_ntoa($ipaddr);
//                $this->AuditLog($row);
//
//                $this->ds->RowLock("ipaddradd", "baseindex=$baseindex AND ipaddr=$ipaddr");
//            }
//            $this->AddIPaddradd($ipaddr, $baseindex, $info);
//            //$this->AuditLog(array("event"=>131, "action"=>"modify ip details", "ip"=>inet_ntoa($ipaddr),
//            //        "user"=>getAuthUsername(), "ipaddr"=>$ipaddr, "baseindex"=>$baseindex,
//            //        "userinf"=>$user, "location"=>$location, "telno"=>$telno, "macaddr"=>$macaddr,
//            //        "descrip"=>$descrip, "hname"=>$hname));
//        }
//        else
//        {
//          //echo "ModifyIPaddradd: ERROR \$baseindex ($baseindex) is empty. Can not update template\n";
//          //echo "ModifyIPaddradd: ERROR \$ipaddr ($ipaddr) is empty. Can not update template\n";
//          //echo "ModifyIPaddradd: ERROR \$info ($info)is empty. Can not update template\n";
//        }
//    }
//
//// update an existing IP record - only updates relevant field
//    function UpdateIPaddrbridge($ipaddr, $baseindex, $field, $value) {
//
//        $userid = "Switchmap Bridge";
//
//        $result = &$this->ds->Execute("UPDATE ipaddr
//                           SET $field=".$this->ds->qstr($value).",
//                              lastmod=".$this->ds->DBTimeStamp(time()).",
//                              userid=".$this->ds->qstr($userid)."
//                           WHERE baseindex=$baseindex AND ipaddr=$ipaddr");
//        // record does not exist, error
//        return $this->ds->Affected_Rows();
//    }

//****END ADD TO class.dbflib.php****

require_once("../ipplanlib.php");
require_once("../adodb/adodb.inc.php");
require_once("../class.dbflib.php");
require_once("../class.templib.php");
require_once("../auth.php");
require_once("../xmllib.php");

$filename = $_SERVER["argv"][1];
$cust = "1";
$format = "text";

$ds=new IPplanDbf() or "Could not connect to database\n";
$rowcnt=0;
$ds->DbfTransactionStart();
if ($format=="text") {
    // open uploaded file for read
    $fp = @fopen ($filename, "r");
    // no real portable way to check if file was uploaded - php version
    // dependent
    if (!$fp) {
        echo "File could not be opened.\n";
    }

    // can we read the template?
    $template=FALSE;
    $template=new IPplanIPTemplate("iptemplate", $cust);
    if ($template->is_error() == TRUE) {
        echo "Template could not be opened.\n";
        $template=FALSE;   // use is_object later
    }

    //Clear out all previous configs from Switchmap
    $ds->WipeMacInfo();

    while ($data = fgetcsv ($fp, 4098, FIELDS_TERMINATED_BY)) {
        $rowcnt++;
        //echo "Importing row: $rowcnt\n";
        ProcessRow($ds, $cust, $w, $p, $data, $template);
    }
    fclose ($fp);
}
$ds->DbfTransactionEnd();


// $data is number indexed array that has format of ip, user, location,
// description, telephone
function ProcessRow($ds, $cust, &$w, &$p, $data, $template) {

    global $format;

    $num = count ($data);

    // blank row
    if (empty($data[0])) {
        echo "Row is blank - ignoring\n";
        return;
    }
    // bogus row
    if ($numDbfTransactionEnd();
        echo "Row not the correct format\n";
    }

    if (testIP($data[0])) {
        echo "Invalid IP Address\n";
        return;
    }

    $ip=inet_aton($data[0]);
    $macaddr=substr($data[1],0,12);
    $info="";
    if (is_object($template)) {
        // all columns over 6 are considered for adding to template fields
        $cnt=2;
        $userfld=array();
        foreach($template->userfld as $key=>$value) {
            // set fields in template only if field in import file exists, else make blank
            $userfld[$key]=isset($data[$cnt]) ? $data[$cnt] : "";
            $cnt++;
        }
        $template->Merge($userfld);
        $err=$template->Verify($w);
        if ($err) {
            // ok to save what has been imported already
            $ds->DbfTransactionEnd();
            echo "Row failed template verify.\n";
        }

        if ($template->is_blank() == FALSE) {
            $info=$template->encode();
        }
    }

    // NOTE: Test ip address
    $result=$ds->GetBaseFromIP($ip, $cust);
    if (!$row = $result->FetchRow()) {
        // ok to save what has been imported already
        $ds->DbfTransactionEnd();
        echo "Subnet could not be found for IP address $data[0]\n";
    }

    $baseindex=$row["baseindex"];
    $baseaddr=$row["baseaddr"];
    $subnetsize=$row["subnetsize"];

    if ($ds->UpdateIPaddrbridge($ip, $baseindex, "macaddr", $macaddr) == 0) {
        #echo " - IP address details could not be modified [macaddr = $macaddr]\n";
    }
    else {
        #echo " - Updating IP address details [macaddr = $macaddr]\n";
    }

    if ($ds->ModifyIPaddradd($baseindex, $ip, $info) == 0) {
        #echo " - IP address details could not be modified [info = $info]\n";
    }
    else {
        #echo " - Updating IP address details [info = $info]\n";
    }
}
?>
Advertisements

February 8, 2008

ipplansnmppoll_php.txt

Filed under: ipplan, php, snmp — Tags: , , — lancevermilion @ 12:56 pm
 'someNetworkcommunity', 'windows' => 'someNTcommunity', 'unix' => 'someUnixcommuity', 'na' => 'public' );
$oid = '.1.3.6.1.2.1.1.5.0'; //system.sysName.0
$hosts = array();
//$ips = array();
$ips = file('li.txt');
foreach ($ips as $ip)
{
  $ip = preg_replace("/\n|\r/", '', $ip);
  for ($count = 1; $count <= 254; $count++) {
  $jip = $ip . '.' . $count;
  array_push($hosts, $jip);
  }

$pids = array();

pcntl_signal(SIGCHLD,  "sig_handler");
echo "Forking " . sizeof($hosts) . " times\n";
for($i = 0; $i  $devcomm)
    {

      $output = snmpget($hosts[$i], $devcomm, $oid) . " :: $dns :: $devcomm";
      if (!$output)
      {
        $output = ":: Unknown :: $dns :: Timeout";
      }
      else
      {
        if ( preg_match("/^\s+::/", $output) )
        {
          $output = ":: Unknown :: $dns :: Timeout";
        }
        else
        {
          $output = ":: $output";
          break;
        }
      }
    }
//    $ping  = `ping -c 1 $hosts[$i] | grep loss | awk '{print $6}' | sed 's/%//'`;
//    if ( $ping > 0 )
//    {
//      if ($ping == 100)
//      {
//        $status = 'DOWN';
//      }
//      else
//      {
//        $status = 'UP - Packet loss';
//      }
//    }
//    else
//    {
//      $status = 'UP';
//    }
    $output = preg_replace('/\n/', '', $output);
    $output = preg_replace('/\r/', '', $output);
    //echo "$pids[$i] :: $hosts[$i] $output :: $status\n";
    echo "$hosts[$i] $output :: $status\n";
    //echo "$hosts[$i] $output\n";

    exit;
  }
}

for($i = 0; $i < sizeof($hosts); $i++) {
  pcntl_waitpid($pids[$i], $status, WUNTRACED);
}

// complete parent processing now all children have finished
while ( $totretchild 

January 28, 2008

IP Plan xml template

Filed under: ipplan, switchmap, xml — Tags: , , , , — lancevermilion @ 12:47 am

This is a xml template that is used with IP Plan to allow you to have custom fields. The fields are filled by a script that impoprt information from a program called Switchmap.

  
- 
- 
  
  
- 
  
  
- 
  
  
- 
  
  
- 
  
  
  

Blog at WordPress.com.