PHP FreeDSx\Snmp walk windows已安装软件软件名称中文和安装时间正确显示方法

<?php

require 'vendor/autoload.php';

function hex2date($hexstring) {
    $date = "";

    $p = unpack("H*", substr($hexstring, 0, 2));  // year (2 byte)
    $date .= hexdec($p[1]) . "-";

    $p = unpack("H*", substr($hexstring, 2, 1));  // month (1 byte)
    $date .= sprintf("%02s", hexdec($p[1])) . "-";

    $p = unpack("H*", substr($hexstring, 3, 1));   // day (1 byte)
    $date .= sprintf("%02s", hexdec($p[1])) . " ";

    $p = unpack("H*", substr($hexstring, 4, 1));  // hour (1 byte)
    $date .= sprintf("%02s", hexdec($p[1])) . ":";

    $p = unpack("H*", substr($hexstring, 5, 1));  // minute (1 byte)
    $date .= sprintf("%02s", hexdec($p[1])) . ":";
    $p = unpack("H*", substr($hexstring, 6, 1));  // second (1 byte)
    $date .= sprintf("%02s", hexdec($p[1]));

    return ($date);
}

$snmp = new FreeDSx\Snmp\SnmpClient([
    'host' => '127.0.0.1',
    'version' => 2,
    'community' => 'public',
        ]);

# Get a specific OID value as a string...
echo $snmp->getValue('1.3.6.1.2.1.25.6.3.1.2.1') . PHP_EOL;

# Get a specific OID as an object...
$oid = $snmp->getOid('1.3.6.1.2.1.25.6.3.1.2.1');
var_dump($oid);

echo sprintf("%s == %s", $oid->getOid(), (string) $oid->getValue()) . PHP_EOL;

# Get multiple OIDs and iterate through them as needed...
$oids = $snmp->get('1.3.6.1.2.1.25.6.3.1.2.1', '1.3.6.1.2.1.25.6.3.1.2.2', '1.3.6.1.2.1.25.6.3.1.2.3');

foreach ($oids as $oid) {
    echo sprintf("%s == %s", $oid->getOid(), (string) $oid->getValue()) . PHP_EOL;
}

# Using the SnmpClient, get the helper class for an SNMP walk...
$walk = $snmp->walk('1.3.6.1.2.1.25.6.3.1');

# Keep the walk going until there are no more OIDs left
while ($walk->hasOids()) {
    try {
        # Get the next OID in the walk
        $oid = $walk->next();
        if (strpos($oid->getOid(), '1.3.6.1.2.1.25.6.3.1.2') !== false) {
            echo sprintf("%s = %s", $oid->getOid(), mb_convert_encoding($oid->getValue(), "utf8", "gbk")) . PHP_EOL;
        }
        if (strpos($oid->getOid(), '1.3.6.1.2.1.25.6.3.1.5') !== false) {
            echo sprintf("%s = %s", $oid->getOid(), hex2date($oid->getValue())) . PHP_EOL;
        }
    } catch (\Exception $e) {
        # If we had an issue, display it here (network timeout, etc)
        echo "Unable to retrieve OID. " . $e->getMessage() . PHP_EOL;
    }
}

echo sprintf("Walked a total of %s OIDs.", $walk->count()) . PHP_EOL;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值