xref: /PHP-8.3/ext/snmp/tests/snmp-object.phpt (revision 10f102d7)
1--TEST--
2OO API
3--CREDITS--
4Boris Lytochkin
5--EXTENSIONS--
6snmp
7--SKIPIF--
8<?php
9require_once(__DIR__.'/skipif.inc');
10?>
11--FILE--
12<?php
13require_once(__DIR__.'/snmp_include.inc');
14
15//EXPECTF format is quickprint OFF
16snmp_set_enum_print(false);
17snmp_set_quick_print(false);
18snmp_set_valueretrieval(SNMP_VALUE_PLAIN);
19snmp_set_oid_output_format(SNMP_OID_OUTPUT_FULL);
20
21echo "SNMPv1\n";
22$session = new SNMP(SNMP::VERSION_1, $hostname, $community, $timeout, $retries);
23$session->valueretrieval = SNMP_VALUE_LIBRARY;
24var_dump($session->get('.1.3.6.1.2.1.1.1.0'));
25var_dump($session->get(array('.1.3.6.1.2.1.1.1.0', '.1.3.6.1.2.1.1.3.0')));
26var_dump($session->getnext('.1.3.6.1.2.1.1.1.0'));
27var_dump($session->close());
28
29echo "SNMPv2\n";
30$session = new SNMP(SNMP::VERSION_2c, $hostname, $community, $timeout, $retries);
31var_dump($session->get('.1.3.6.1.2.1.1.1.0'));
32var_dump($session->getnext('.1.3.6.1.2.1.1.1.0'));
33var_dump($session->close());
34
35echo "GET with preserving original OID names\n";
36$session = new SNMP(SNMP::VERSION_2c, $hostname, $community, $timeout, $retries);
37$orig = array('.1.3.6.1.2.1.1.1.0', '.1.3.6.1.2.1.1.5.0');
38$result = $session->get($orig, TRUE);
39foreach($orig as $oid){
40    var_dump($result[$oid]);
41}
42var_dump($session->close());
43
44echo "WALK multiple on single OID\n";
45$session = new SNMP(SNMP::VERSION_2c, $hostname, $community, $timeout, $retries);
46$z = $session->walk('.1.3.6.1.2.1.1');
47var_dump(gettype($z));
48var_dump(count($z));
49var_dump(key($z));
50var_dump(array_shift($z));
51var_dump($session->close());
52
53echo "WALK multiple on single OID, max_repetitions set to 30\n";
54$session = new SNMP(SNMP::VERSION_2c, $hostname, $community, $timeout, $retries);
55$z = $session->walk('.1.3.6.1.2.1.1', FALSE, 30);
56var_dump(gettype($z));
57var_dump(count($z));
58var_dump(key($z));
59var_dump(array_shift($z));
60var_dump($session->close());
61
62echo "WALK multiple on single OID, max_repetitions set to 30, non_repeaters set to 0\n";
63$session = new SNMP(SNMP::VERSION_2c, $hostname, $community, $timeout, $retries);
64$z = $session->walk('.1.3.6.1.2.1.1', FALSE, 30, 0);
65var_dump(gettype($z));
66var_dump(count($z));
67var_dump(key($z));
68var_dump(array_shift($z));
69var_dump($session->close());
70
71echo "WALK multiple on single OID, max_oids set to 30\n";
72$session = new SNMP(SNMP::VERSION_2c, $hostname, $community, $timeout, $retries);
73$session->max_oids = 30;
74$z = $session->walk('.1.3.6.1.2.1.1');
75var_dump(gettype($z));
76var_dump(count($z));
77var_dump(key($z));
78var_dump(array_shift($z));
79var_dump($session->close());
80
81echo "WALK multiple on single OID with OID suffix as keys\n";
82$session = new SNMP(SNMP::VERSION_2c, $hostname, $community, $timeout, $retries);
83$z = $session->walk('.1.3.6.1.2.1.1', TRUE);
84var_dump(gettype($z));
85var_dump(count($z));
86var_dump(key($z));
87var_dump(array_shift($z));
88var_dump(key($z));
89array_shift($z);
90var_dump(key($z));
91array_shift($z);
92var_dump(key($z));
93array_shift($z);
94var_dump(key($z));
95var_dump($session->close());
96
97echo "SNMPv3 (default security settings)\n";
98$session = new SNMP(SNMP::VERSION_3, $hostname, $user_noauth, $timeout, $retries);
99#$session->setSecurity($user_noauth, 'noAuthNoPriv', '', '', '', '', '', '');
100var_dump($session->get('.1.3.6.1.2.1.1.1.0'));
101var_dump($session->getnext('.1.3.6.1.2.1.1.1.0'));
102var_dump($session->close());
103
104echo "SNMPv3 (noAuthNoPriv)\n";
105$session = new SNMP(SNMP::VERSION_3, $hostname, $user_noauth, $timeout, $retries);
106$session->setSecurity('noAuthNoPriv');
107var_dump($session->get('.1.3.6.1.2.1.1.1.0'));
108var_dump($session->getnext('.1.3.6.1.2.1.1.1.0'));
109var_dump($session->close());
110
111echo "SNMPv3 (authPriv)\n";
112$session = new SNMP(SNMP::VERSION_3, $hostname, $rwuser, $timeout, $retries);
113$session->setSecurity('authPriv', 'MD5', $auth_pass, 'AES', $priv_pass);
114var_dump($session->get('.1.3.6.1.2.1.1.1.0'));
115var_dump($session->getnext('.1.3.6.1.2.1.1.1.0'));
116var_dump($session->walk('.1.3.6.1.2.1.1.1.0'));
117var_dump($session->close());
118
119echo "SET single OID\n";
120$session = new SNMP(SNMP::VERSION_3, $hostname, $rwuser, $timeout, $retries);
121$session->setSecurity('authPriv', 'MD5', $auth_pass, 'AES', $priv_pass);
122$oid1 = 'SNMPv2-MIB::sysContact.0';
123$oldvalue1 = $session->get($oid1);
124$newvalue1 = $oldvalue1 . '0';
125
126$z = $session->set($oid1, 's', $newvalue1);
127var_dump($z);
128var_dump(($session->get($oid1) === $newvalue1));
129$z = $session->set($oid1, 's', $oldvalue1);
130var_dump($z);
131var_dump(($session->get($oid1) === $oldvalue1));
132var_dump($session->close());
133
134echo "Multiple OID with max_oids = 1\n";
135$oid2 = 'SNMPv2-MIB::sysLocation.0';
136$session = new SNMP(SNMP::VERSION_3, $hostname, $rwuser, $timeout, $retries);
137$session->setSecurity('authPriv', 'MD5', $auth_pass, 'AES', $priv_pass);
138$session->max_oids = 1;
139$oldvalue2 = $session->get($oid2);
140$newvalue2 = $oldvalue2 . '0';
141$z = $session->set(array($oid1, $oid2), array('s','s'), array($newvalue1, $newvalue2));
142var_dump($z);
143var_dump(($session->get($oid1) === $newvalue1));
144var_dump(($session->get($oid2) === $newvalue2));
145$z = $session->set(array($oid1, $oid2), array('s','s'), array($oldvalue1, $oldvalue2));
146var_dump($z);
147var_dump(($session->get($oid1) === $oldvalue1));
148var_dump(($session->get($oid2) === $oldvalue2));
149var_dump($session->close());
150
151echo "SNMPv3, setting contextEngineID (authPriv)\n";
152$session = new SNMP(SNMP::VERSION_3, $hostname, $rwuser, $timeout, $retries);
153$session->setSecurity('authPriv', 'MD5', $auth_pass, 'AES', $priv_pass, '', 'aeeeff');
154var_dump($session->get('.1.3.6.1.2.1.1.1.0'));
155var_dump($session->close());
156
157?>
158--EXPECTF--
159SNMPv1
160string(%d) "%S"
161array(2) {
162  ["%s"]=>
163  string(%d) "%S"
164  ["%s"]=>
165  string(%d) "%S"
166}
167string(%d) "%S"
168bool(true)
169SNMPv2
170string(%d) "%S"
171string(%d) "%S"
172bool(true)
173GET with preserving original OID names
174string(%d) "%s"
175string(%d) "%s"
176bool(true)
177WALK multiple on single OID
178string(5) "array"
179int(%d)
180string(%d) "%S"
181string(%d) "%S"
182bool(true)
183WALK multiple on single OID, max_repetitions set to 30
184string(5) "array"
185int(%d)
186string(%d) "%S"
187string(%d) "%S"
188bool(true)
189WALK multiple on single OID, max_repetitions set to 30, non_repeaters set to 0
190string(5) "array"
191int(%d)
192string(%d) "%S"
193string(%d) "%S"
194bool(true)
195WALK multiple on single OID, max_oids set to 30
196string(5) "array"
197int(%d)
198string(%d) "%S"
199string(%d) "%S"
200bool(true)
201WALK multiple on single OID with OID suffix as keys
202string(5) "array"
203int(%d)
204string(3) "1.0"
205string(%d) "%s"
206string(3) "2.0"
207string(3) "3.0"
208string(3) "4.0"
209string(3) "5.0"
210bool(true)
211SNMPv3 (default security settings)
212string(%d) "%S"
213string(%d) "%S"
214bool(true)
215SNMPv3 (noAuthNoPriv)
216string(%d) "%S"
217string(%d) "%S"
218bool(true)
219SNMPv3 (authPriv)
220string(%d) "%S"
221string(%d) "%S"
222array(1) {
223  [%s]=>
224  string(%d) "%S"
225}
226bool(true)
227SET single OID
228bool(true)
229bool(true)
230bool(true)
231bool(true)
232bool(true)
233Multiple OID with max_oids = 1
234
235Warning: SNMP::set(): Cannot fit all OIDs for SET query into one packet, using multiple queries in %s on line %d
236bool(true)
237bool(true)
238bool(true)
239
240Warning: SNMP::set(): Cannot fit all OIDs for SET query into one packet, using multiple queries in %s on line %d
241bool(true)
242bool(true)
243bool(true)
244bool(true)
245SNMPv3, setting contextEngineID (authPriv)
246string(%d) "%S"
247bool(true)
248