-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtree.php
More file actions
executable file
Β·78 lines (63 loc) Β· 2.02 KB
/
Copy pathtree.php
File metadata and controls
executable file
Β·78 lines (63 loc) Β· 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env php
<?php
declare(strict_types=1);
/*
* This file contains a simple PHP source code to print the OID tree in console
*
* Usage:
* ./tree.php 'SNMPv2-MIB::system'
* or
* php ./tree.php 'SNMPv2-MIB::system'
*/
use NoFlash\MibParser\Factory\NetSnmpBridgeFactory;
use NoFlash\MibParser\Snmp\TreeBuilder;
use NoFlash\MibParser\Snmp\TreeNode;
require __DIR__ . '/../vendor/autoload.php';
if ($argc !== 2) {
printf("Usage: %s oid\n", $argv[0]);
exit(2);
}
$oid = \trim($argv[1]);
if (\strlen($oid) === 0) {
print("OID cannot be empty!\n");
exit(1);
}
$bridge = NetSnmpBridgeFactory::createBridge();
$treeBuilder = new TreeBuilder($bridge);
printNodeRecursively($treeBuilder->buildFromOid($oid));
function printNodeRecursively(TreeNode $node, int $level = 0): void
{
echo \str_repeat(' ', $level); //Padding for every level
$extraInfo = [];
$type = $node->getNodeType();
if ($type->isContainer()) { //Checking if it may have kids (not if it does)
\printf('π ');
} else {
\printf("π \e[2m(%s | %s)\e[0m ", $type->asString(), $type->getPhpTypeName());
$access = $node->getNodeAccess();
$extraInfo[] = \sprintf('r/w: %s/%s',
($access->isReadable() ? 'β
' : 'β'),
($access->isWritable() ? 'β
' : 'β')
);
}
\printf(" \e[1m%s\e[0m @ %d", $node->label, $node->subId); //Make label bold
$idx = $node->getIndexes();
if (\count($idx) > 0) {
$extraInfo[] = \sprintf('π {%s}', \implode(', ', $idx));
}
$ranges = $node->ranges;
if (\count($ranges) > 0) {
$extraInfo[] = 'π’' . \implode('/', $ranges);
}
if ($node->txtConvention !== null) {
$extraInfo[] = 'π' . $node->txtConvention;
}
$extraInfo[] = 'NT=' . $node->getType();
if (isset($extraInfo[0])) {
printf(" \e[3m(%s)\e[0m", \implode(', ', $extraInfo)); //Make extras italic
}
echo "\n";
foreach ($node->children as $child) {
printNodeRecursively($child, $level+1);
}
}