freedsx / snmp Goto Github PK
View Code? Open in Web Editor NEWA Pure PHP SNMP Library.
License: MIT License
A Pure PHP SNMP Library.
License: MIT License
I have begun working on using this class, and it works great. However I am running into trouble when invalid information is passed to the class.
I specifically am testing an invalid IP address, as well as invalid community names. Currently I am getting fatal errors in PHP for both. This is against both PHP7.4, as well as PHP8.2 and am having a hell of a time getting the try catch block to work as you designed.
I have been attempting to alter the throw so it does not spew these errors but have not been having any luck there or in making a __construct() within the ConnectionException to attempt to grab the error. I do see the error information when I do a print_r and it returns the object, but suspect that I am not getting all the way to the \Exception class for some reason..
Any suggestions or guidance on how to fix this would be appreciated.. ( Still learning the more complex areas of PHP, and it is biting me in the rear end)
chubbard@lnms01:/opt/nmsApi/testing/freedsx$ /usr/bin/php7.4 ./testDisc.php
PHP Fatal error: Uncaught FreeDSx\Snmp\Exception\ConnectionException in /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php:152
Stack trace:
#0 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php(122): FreeDSx\Snmp\Protocol\ClientProtocolHandler->sendRequestGetResponse()
#1 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(233): FreeDSx\Snmp\Protocol\ClientProtocolHandler->handle()
#2 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(113): FreeDSx\Snmp\SnmpClient->send()
#3 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(126): FreeDSx\Snmp\SnmpClient->get()
#4 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(139): FreeDSx\Snmp\SnmpClient->getOid()
#5 /opt/nmsApi/testing/freedsx/testDisc.php(15): FreeDSx\Snmp\SnmpClient->getValue()
#6 {main}
thrown in /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php on line 152
chubbard@nms01:/opt/nmsApi/testing/freedsx$ /usr/bin/php8.2 ./testDisc.php
PHP Fatal error: Uncaught FreeDSx\Snmp\Exception\ConnectionException in /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php:152
Stack trace:
#0 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php(122): FreeDSx\Snmp\Protocol\ClientProtocolHandler->sendRequestGetResponse()
#1 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(233): FreeDSx\Snmp\Protocol\ClientProtocolHandler->handle()
#2 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(113): FreeDSx\Snmp\SnmpClient->send()
#3 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(126): FreeDSx\Snmp\SnmpClient->get()
#4 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(139): FreeDSx\Snmp\SnmpClient->getOid()
#5 /opt/nmsApi/testing/freedsx/testDisc.php(15): FreeDSx\Snmp\SnmpClient->getValue()
#6 {main}
thrown in /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php on line 152
This is what I am attempting to run:
cat testDisc.php
<?php
require __DIR__ . '/../../vendor/autoload.php';
use \FreeDSx\Snmp\SnmpClient;
$snmp = new SnmpClient([
'host' => '192.168.15.589',
'version' => 2,
'community' => 'public2',
'timeout_connect' => 1,
'timeout_read' => 1,
]);
# Get a specific OID value as a string...
$data= $snmp->getValue('1.3.6.1.2.1.1.1.0');
echo $data . "\n\n";
?>
Hi, what is the reason for using spread operator eg. here
SNMP/src/FreeDSx/Snmp/OidList.php
Line 36 in 0157012
Is it just type safety or something else? Thanks!
Hello @ChadSikorra,
I am taking look at this libary and I would like to use it in my project. But, how to use SnmpWalk ?
For example I wan to read values of oid 1.3.6.1.2.1.2.2.1.1
. With classic snmpwalk I can get values with:
snmpwalk -v2c -c ${COMMUNITY) ${HOST_IP} 1.3.6.1.2.1.2.2.1.1
IF-MIB::ifIndex.1 = INTEGER: 1
IF-MIB::ifIndex.2 = INTEGER: 2
IF-MIB::ifIndex.3 = INTEGER: 3
IF-MIB::ifIndex.4 = INTEGER: 4
IF-MIB::ifIndex.5 = INTEGER: 5
IF-MIB::ifIndex.6 = INTEGER: 6
IF-MIB::ifIndex.7 = INTEGER: 7
IF-MIB::ifIndex.8 = INTEGER: 8
IF-MIB::ifIndex.9 = INTEGER: 9
IF-MIB::ifIndex.10 = INTEGER: 10
IF-MIB::ifIndex.11 = INTEGER: 11
IF-MIB::ifIndex.12 = INTEGER: 12
IF-MIB::ifIndex.13 = INTEGER: 13
IF-MIB::ifIndex.14 = INTEGER: 14
IF-MIB::ifIndex.15 = INTEGER: 15
IF-MIB::ifIndex.16 = INTEGER: 16
IF-MIB::ifIndex.17 = INTEGER: 17
IF-MIB::ifIndex.18 = INTEGER: 18
IF-MIB::ifIndex.19 = INTEGER: 19
IF-MIB::ifIndex.20 = INTEGER: 20
IF-MIB::ifIndex.21 = INTEGER: 21
IF-MIB::ifIndex.22 = INTEGER: 22
IF-MIB::ifIndex.23 = INTEGER: 23
IF-MIB::ifIndex.24 = INTEGER: 24
IF-MIB::ifIndex.25 = INTEGER: 25
IF-MIB::ifIndex.26 = INTEGER: 26
IF-MIB::ifIndex.27 = INTEGER: 27
IF-MIB::ifIndex.28 = INTEGER: 28
but when I am trying to get values with SnmpWalk function from this libaray I can't get values for example:
public function get()
{
$snmp = new SnmpClient([
'host' => ${HOST_IP},
'version' => 2,
'community' => ${COMMUNITY},
]);
$walk = $snmp->walk('1.3.6.1.2.1.2.2.1.1');
return $walk;
}
I expect that count can't be 0
@ChadSikorra can you help me what I am doing wrong ?
Thank you.
Hi,
I'm trying to connect to Dell Switch using following code:
$snmp = new SnmpClient([
'host' => '1.2.3.4',
'version' => 3,
'user' => 'user',
'use_auth' => true,
'auth_pwd' => 'PWD',
'auth_mech' => 'md5',
'use_priv' => true,
'priv_pwd' => 'PWD',
'priv_mech' => 'des',
]);
$uptime = $snmp->getValue('1.3.6.1.2.1.1.3.0');
But I get the following error:
Authentication was requested, but the received header has none specified.
The when I'm using the native php method all is ok:
snmp3_get('1.2.3.4', 'user', 'authPriv', 'MD5', 'PWD', 'DES', 'PWD', '1.3.6.1.2.1.1.3.0');
Could you please help?
hey! your lib seems to be in the right direction! what do you think to add reactPHP support?
Is there a way, how to get sub tree for lets say 1.3.6.1.2.1.47.1.1.1.1.2
? I studied the docs but couldn't find a way how to get array of sub tree results. Thanks!
Hello.
Do I need to close SnmpClient
after usage? For example if I have several instances of this class.
I don't see any corresponding method in SnmpClient.php.
Hello, I constantly get the error of the first attached image. Is this is caused by CPU usage of the device, connection, or what? And how to avoid that? Is there any change on the device that I should do?
More details:
I have begun testing with your example, and the walks are going really well, now that I installed GMP and it can handle the 64 bit counters correctly. However I am currently seeing issues where a return is a Hex-STRING value, and the returns get mangled.
snmpwalk -v2c -c noyb 192.168.15.58 1.3.6.1.2.1.92.1.3.1.1.3.7.100.101.102.97.117.108.116.1
iso.3.6.1.2.1.92.1.3.1.1.3.7.100.101.102.97.117.108.116.1 = Hex-STRING: 07 E5 0C 11 0A 19 36 00 2D 08 00
From the walk example:
1.3.6.1.2.1.92.1.3.1.1.3.7.100.101.102.97.117.108.116.1 = �
6-
Another using the example code:
1.3.6.1.2.1.55.1.5.1.8.2 = 8�G��}
manually:
snmpwalk -v2c -c noyb 192.168.15.58 1.3.6.1.2.1.55.1.5.1.8.2
iso.3.6.1.2.1.55.1.5.1.8.2 = Hex-STRING: 38 D5 47 DF DA 7D
Is there additional information that I can provide to assist in debugging this?
Ubuntu 20.04
PHP 7.4.26
Hi, happy afternoon friend. i am trying to get the name of a computer. using a snmp search engine, it tells me that its oid is <<sysName.0>>, but when i put that value like that i never get the result.
How can I get the value by passing that string?
please ask me, where is mibs data ? i have add my mibs.
My MIBs location /usr/share/snmp/mibs, but laravel not see.
The PHP max int is limited to the 64 bit signed integer value of 9,223,372,036,854,775,807. However, the Counter64 value of SNMP can be the maximum value of a 64 bit unsigned integer, which is 18,446,744,073,709,551,615.
This presents a real issue with integer type hinting across FreeDSx libraries. This will need to be solved in the ASN.1 library than decide how to handle it elsewhere.
I have gone exploring internals of this lib. I'd like to ask about this line
$this->startAt = $startAt ?? '1.3.6.1.2.1';
Shouldn't it rather throw exception when no startAt is provided? Or just type string $startAt
? I don't get the reason for default '1.3.6.1.2.1'. Thanks!
This library should have some sort of general SNMP server component for replying to SNMP client requests. There is already a Trap Sink component capable of handling incoming client requests. Some prerequisite work is need:
Related work in progress in the ASN.1 library, needed for MIB support: FreeDSx/ASN1#3
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.