Generate SNMP traps with PHP

杀马特。学长 韩版系。学妹 提交于 2019-12-07 07:39:08

问题


I'm desperately searching for a way to generate SNMP traps from PHP. I know the build in methods to use snmpget but I was not able to figure out how to send SNMP traps.

Does anybody know a class / code snippet for it? Searching the web did not bring up anything other than using exec to call cli tools which is definately no option for me.

I suspect that it would be neccessary to use socket_create and corresponding functionality to generate the UDP package manually...


回答1:


For anybody searching for such a library these days (in 2019), I found https://github.com/FreeDSx/SNMP which supports sending SNMPv1 and SNMPv2 traps (including inform requests).




回答2:


As far as I know, there is no native way for generating traps/informs with php. Even the SNMP extension only permits get and set requests. So the only (quick) way to accomplish this is to call an external tool like net-snmp. The proper command line would be something like

snmptrap -v 1 -c public manager enterprises.spider test-hub 3 0 '' interfaces.iftable.ifentry.ifindex.1 i 1

will send a generic linkUp trap to manager, for interface 1 (taken from the manpage). To execute this from php the net-snmp binaries should be on the path of the system and you could either call exec, shell_exec or proc_open.

Obvisouly you also can send the trap by yourself by encoding it as raw byte array and sending it over an UDP socket, but then you had to implement a BER encoder and a SNMP packet encoder all by yourself which I don't recommend. For your reference, you would need those informations:

  • Basic Encoding Rules
  • ASN.1
  • and some further links found here



回答3:


There are no core SNMP trap libraries. Or even any core libraries that will help you package an SNMP udp packet. I did however find this abandoned project. http://code.google.com/p/php-snmp/ which provides most of what you would need to send a simple trap.

A little more active but a lot more complex seems to be http://www.activexperts.com/network-component/howto/snmpts/php/




回答4:


I know this question is old, but I just came across it via Google, and thought to update it according to my findings in case someone else also lands here. As Jek answered, using net-snmp is the best solution. Although the original post said that he didn't want to use any external components, consider you can now add net-snmp though apt-get (look up package php-snmp) for many Linux distro's, and I'm sure installing on Windows will be equally easy. The great benefit of using it, is as of PHP 5.3.3, PHP inherintly has built-in interface functions to use SNMP, so that you don't have to use exec, shell_exec or proc_open. Everything can be done in a PHP environment. See http://php.net/manual/en/book.snmp.php



来源:https://stackoverflow.com/questions/3825638/generate-snmp-traps-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!