PHP snmpwalk

snmpwalk

(PHP 4, PHP 5, PHP 7)

snmpwalk — 从代理返回所有的 SNMP 对象

说明

snmpwalk ( string $hostname , string $community , string $object_id [, int $timeout [, int$retries ]] ) : array

返回由 object_id 作为根的 SNMP 对象值所组成的数组,错误则返回 FALSE

snmpwalk() 函数是用来读取所有由 hostname 指定的 SNMP 代理的值。Community 指定该代理中具有读权限的域。一个为 NULL 的 object_id 将被看作 SNMP 对象树的根,而在此树下的所有对象将作为一个数组被返回。如果指定了 object_id,则返回所有在 object_id 下的 SNMP 对象。

<?php
$a = snmpwalk("127.0.0.1", "public", ""); 
?>

上边的函数调用将从运行于本机的 SNMP 代理那里返回所有的 SNMP 对象。可使用循环遍历这些值。

<?php
foreach ($a as $val) {
    echo "$val\n";
}
?>

 

Timeout is in MICRO seconds.
1,000,000 &micros = 1 s

 /**
  * Do snmpwalk
  *
  * @param unknown_type $rootOID
  * @return array Array of values
  */
 public function GetTree($rootOID = null)
 {
     try {
         $retval = @snmpwalk($this->Connection, $this->Community, $rootOID, $this->Timeout);
     } catch (Exception $e) {
         $this->RaiseWarning("Cannot walk through {$this->Connection}/{$this->Community}/{$rootOID}" . $e->__toString());
     }
     return $retval;
 }
 <tr>
        <td colspan = "1">
        <a href='index.php'>HOME</td> 
            	    
            	    
            	   
            	    	
        <td style="height:600px;width:2000px;vertical-align:top;">
        <center><br>
        <form action = "a3n3.php" method = "POST">  
          <?php 
$IP = $_POST["IP"];
$PORT = $_POST["PORT"];
$COMMUNITY = $_POST["COMMUNITY"];
$int = snmpwalk("{$IP}:{$PORT}", "{$COMMUNITY}", '1.3.6.1.2.1.2.2.1.1', 100000, 5);
$device = "{$COMMUNITY}-{$IP}-{$PORT}";
if ($int) {
    echo "The interfaces of the selected device {$COMMUNITY}, {$IP} and {$PORT} are:";
    echo "<br><table border = '1'>";
    foreach ($int as $x) {
        $i = explode(' ', $x);
        echo "<tr><td><input type = 'checkbox' name ='list[]' value = {$i['1']},{$device}> {$i['1']}</td></tr>";
    }
} else {
    echo "<br>The device is down. Cannot add<br>";
}
?>
     
       </table><br>
private function _sGetCpuInfo($sIp)
 {
     $cpuinfo = array();
     $ret = snmpwalk($sIp, $this->_aConf['community'], 'hrDeviceEntry', 300000);
     if (is_array($ret)) {
         $len = 0;
         foreach ($ret as $k => $v) {
             list($t, $s) = explode(':', $v);
             if ('INTEGER' !== $t) {
                 $len = $k;
                 break;
             }
         }
         for ($i = 0; $i < $len; $i++) {
             list($t, $s) = explode(':', $ret[$len + $i], 2);
             if (' HOST-RESOURCES-TYPES::hrDeviceProcessor' == $s) {
                 list($t, $s) = explode(':', $ret[2 * $len + $i], 2);
                 $cpuinfo[trim($s)]++;
             }
         }
     }
     $ret = array();
     foreach ($cpuinfo as $k => $v) {
         $ret[] = $v . '个逻辑CPU,' . $k;
     }
     return implode(' || ', $ret);
 }

 

 function my_snmp_walk($ip, $credentials, $oid)
 {
     $timeout = '30000000';
     $retries = '0';
     switch ($credentials->credentials->version) {
         case '1':
             $array = @snmpwalk($ip, $credentials->credentials->community, $oid, $timeout, $retries);
             break;
         case '2':
             $array = @snmp2_walk($ip, $credentials->credentials->community, $oid, $timeout, $retries);
             break;
         case '3':
             $sec_name = $credentials->credentials->security_name ?: '';
             $sec_level = $credentials->credentials->security_level ?: '';
             $auth_protocol = $credentials->credentials->authentication_protocol ?: '';
             $auth_passphrase = $credentials->credentials->authentication_passphrase ?: '';
             $priv_protocol = $credentials->credentials->privacy_protocol ?: '';
             $priv_passphrase = $credentials->credentials->privacy_passphrase ?: '';
             $array = @snmp3_walk($ip, $sec_name, $sec_level, $auth_protocol, $auth_passphrase, $priv_protocol, $priv_passphrase, $oid, $timeout, $retries);
             break;
         default:
             return false;
             break;
     }
     if (!is_array($array)) {
         return false;
     }
     foreach ($array as $i => $value) {
         $array[$i] = trim($array[$i]);
         if ($array[$i] == '""') {
             $array[$i] = '';
         }
         if (strpos($array[$i], '.') === 0) {
             $array[$i] = substr($array[$i], 1);
         }
         // remove the first and last characters if they are "
         if (substr($array[$i], 0, 1) == "\"") {
             $array[$i] = substr($array[$i], 1, strlen($array[$i]));
         }
         if (substr($array[$i], -1, 1) == "\"") {
             $array[$i] = substr($array[$i], 0, strlen($array[$i]) - 1);
         }
         // remove some return strings
         if (strpos(strtolower($array[$i]), '/etc/snmp') !== false or strpos(strtolower($array[$i]), 'no such instance') !== false or strpos(strtolower($array[$i]), 'no such object') !== false or strpos(strtolower($array[$i]), 'not set') !== false or strpos(strtolower($array[$i]), 'unknown value type') !== false) {
             $array[$i] = '';
         }
         // remove any quotation marks
         $array[$i] = str_replace('"', ' ', $array[$i]);
         // replace any line breaks with spaces
         $array[$i] = str_replace(array("\r", "\n"), " ", $array[$i]);
     }
     return $array;
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值