Jump to content

SNMP_UDF for SNMPv1 and SNMPv2c


enaiman
 Share

Recommended Posts

New Update: 29/08/2011

Thanks to @Shredplayer

This update is very useful if you have ever used $SNMP_Received array (declared to have "educational purpose") or if you will work with enterprise-specific OIDs; if you didn't, you won't notice any difference.

This UDF works 100% on non-enterprise specific OID's - when it comes to these, some other rules may apply - this is the purpose of this update.

Added a new element in $SNMP_Received array - the new element is a string with all PDU's elements delimited by "|" (data type|length|OID|data type|length|util data)

This was necessary to offer more flexibility because some enterprise-specific have different decoding rules. Having the raw data in a delimited string will help you "customize" how it is decoded

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Thanks for the update however ive got error using GetBulk

060D2B060104018121130303011A0004818B446967506C61743B31312E5266506C61743B302E426F6172645265763B322E4672657142616E643B312E41646A506F7765723B542E45787452616E67653B462E457874416E743B462E506F6C61723B302E4C6974653B462E4650474153697A653B302E4D6F643B302E504C44576F726B3B462E504C445665723B302E0A4C6F7743757272656E743B542E0A

C:\Program Files\AutoIt3\Include\SNMP_UDF_v1.6.au3 (637) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$ArrRet[$idx][0] = $val0

reverted back to 1.5 and No error.

Link to comment
Share on other sites

For some reason - the new forum does not work for me very well - I have no access to the full editor, so I can't delete the attached files to my 1st post.

I have fixed the error from last version (1.6) - it was bad array handling - sorry about that.

Fixed version number 1.6.1

SNMP_UDF_v1.6.1.au3

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

  • 2 months later...

New - minor - version update, now 1.6.2

I have added error control for the case when the SNMP querry returned errors - now it will show an error message then exit.

Sometime, I might work a little bit on optimization ...

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

New version: 1.7

Completely rewritten. Not using arrays anymore to handle packets. In the previous code I had a lot of _ArrayAdd, _ArrayDelete and some other and that was slow.

Now using strings, it definitely looks much faster now.

Because of this massive change, it is highly possible to have some bugs - if you find any, please let me know and I will fix them.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

  • 2 months later...

I think i found a bug?

When calling Func _SNMPBuildPacket($snmpOID, $snmpCOMM = "public", $snmpVER = 1, $snmpReqID = 1, $PDUType = "A1", $GetBulk = "32", $dataTYPE = "05", $dataVALUE = "00")

With $dataTYPE = $SNMP_data_INT and $dataValue to 0 or "00", the packet will contain "30" and "3030" at the end. This is the ascii value of the character "0"

One would think that it would be "00" that is be added.

Another thing i noticed is that when i send the same write snmp with another snmp program, i get different output.

The last bytes of the packet

your code:

02 00

other program:

02 01 00

the first byte is the type (INTEGER) the third byte is the data it self (0). If the second packet is opened with the sniffer program, the thrid byte is selected as data byte. If i load you packet, when i select the value info, no bytes are selected in the packet. So that way i supose that the packet is incorrect.

When I check your code, i think both errors (if these are errors) can be found in the _Build_Varbind() function.

One more question.

How should one save the write byte in dataVALUE?

dataVALUE="1384"

dataVALUE="568"

dataVALUE=1384

dataVALUE=0x568

kind regards,

and thanks for this great UDF!

Link to comment
Share on other sites

@SilentButeo2

Thank you for taking your time to report this.

What are you saying does make sense that the "length" of the data should be there. I didn't notice that because I haven't got any errors so far; but on the other hand, I don't use integers too often.

Regarding your second question: the value is always in decimal format. I don't think it does make a difference if using 1384 or "1384". Hex format? - nah, I didn't implement that.

Question: did you get any errors or you haven't got an answer back because of the "missing length"?

I'll have a look at the uDF and I'll fix that (maybe even today).

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

New minor update: had to change all StringLen to Int(StringLen) - now it works again (in 3.3.8.0)

Couldn't attach the files to first post.

snmp_packetgen_write.au3

snmp_packetgen_1.7.1.au3

SNMP_UDF_v1.7.1.au3

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

@enaiman:

1. The fix you had to release to work with 3.3.80, is probably a bug in the 3.3.80 version. I opened a bug report for that. (http://www.autoitscript.com/trac/autoit/ticket/2094)

2. I adjusted your code to make the INT work. In short i changed next lines:

Case $SNMP_data_INT, $SNMP_data_COUNTER, $SNMP_data_GAUGE, $SNMP_data_TIME, $SNMP_data_STR  ;(INT, COUNTER, GAUGE, TIME, STR)
   $p_dVALUE = _StringToHex($p_dVALUE)

into

Case $SNMP_data_INT ;(INT)
   $p_dVALUE =Hex(Int($p_dVALUE), 8)
  Case $SNMP_data_COUNTER, $SNMP_data_GAUGE, $SNMP_data_TIME, $SNMP_data_STR  ;(COUNTER, GAUGE, TIME, STR)
   $p_dVALUE = _StringToHex($p_dVALUE)

Now the output is correct if I view the created packet in WireShark. The correct data length is filled (thanks) and the correct data is added.

Optimizations could be done.

- Only add the number of bytes that are needed. Now there are always 4 bytes added

- don't know if the fix for INTEGERS should also be done for COUNTER, GAUGE, TIME?

Other smaller fixes i had to do to run without warnings/errors.

Add

AutoItSetOption ( "MustDeclareVars", 1 )

at the beginning of your file to check these warnings/errors.

Added my patched UDF file as a reference.

SNMP_UDF_testing.au3

Link to comment
Share on other sites

Thanks again for pointing out this behaviour. I'll fix the UDF as soon as possible; definitely: gauge, counter, time and Int needs to be converted not as strings but as numbers.

EDIT: Fixed the INT behaviour; I've decided to use 8 digits for conversion then strip pairs of 0 from the left.

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

big thanks on this UDF... now i would like to create a desktop version of what i have already done php.

i just need to send an snmp trap ... anyone have an example of this using this UDF? :) greatly appreciate it.

i see i can read packets with SNMPBuildPacket... but can i generate a SNMP packet and send it to a backend server?

here is my config with OID's.

$source_ip= "xx.xx.xx.xx";
$dest_Address = "xx.xx.xx.xx";
$enterprise="1.3.6.1.4.1.13568";
$GenericType=6;
$SpecificType=3;
$OID1="1.3.6.1.4.1.13568.1.0";
$OID2="1.3.6.1.4.1.13568.2.0";
$OID3="1.3.6.1.4.1.13568.3.0";
$OID4="1.3.6.1.4.1.13568.4.0";
$OID5="1.3.6.1.4.1.13568.5.0";
$OID6="1.3.6.1.4.1.13568.6.0";
$OID7="1.3.6.1.4.1.13568.7.0";

and here is how i am currently sending trap via PHP on my Linux/Apache build. just uses sendtrap command...

exec("/opt/EMPsysedge/bin/sendtrap -v 1 -f ".$source_ip." -h ".$dest_Address." -c public ".$enterprise." ".$GenericType." ".$SpecificType." ".$OID1." -s ".'"'.$email.'"'." ".$OID2." -s ".'"'.$filter.'"'." ".$OID3." -s ".'"'.$startTime.'"'." ".$OID4." -s ".'"'.$endTime.'"'." ".$OID5." -s ".strtoupper($_POST['actionType'])." ".$OID6." -s ".'"'.$_SESSION['SERVICE'].'"'." ".$OID7." -s ".'"'.$reason.'"');
Edited by zEwt
Link to comment
Share on other sites

@zEwt

I'm afraid you're out of luck here. My UDF can't build trap packets because the packet format is different.

I was thinking at some point to add that functionality but haven't got the time to do it.

I will definitely add that at some point but I can't promise anything; might be months or more. The most difficult part of this is getting the packet structure and the algorythm to use to build the packet - it does require a fair bit of research and testing and my time is quite limited.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

  • 2 months later...

Good day..

I'd like to start by saying, im new to AutoIt and last programming i did was C++ about 10 years ago... trying to shake the rust off and its not getting any better...

I've made a couple of small programs to get started and need to tackle SNMP sets and gets...

Currently I use NET-SNMP and use Shellexecute() to set OIDs in a Cisco switch...

I'd love to implement a simple set or get via your UDF but am having a really hard time getting any of the functions to work, even the generic one snmp_packetgen_write.au3

The only value I changed was $dest_IP to that of the switch and I get stuck with a bunch of hex at the bottom of the page...

any help would be appreciated!!!

#Include <String.au3>
#Include <Array.au3>
#Include 'SNMP_UDF_v1.7.2.au3'
; ----- CAUTION -------------------------------------------------------------
; This script will modify the value found at sysLocation (1.3.6.1.2.1.1.6.0)
; it is your job to write back whatever was there previously :)
;----------------------------------------------------------------------------
;In order to have a succesful script run you will need to be sure that:
; - you are using the "write" community string
; - the OID where you want to write is NOT Read-Only
; - the value type you want to write is according to that OID (if you try to write text in an integer type OID it will fail)

Global $dest_IP = "192.168.1.2"    ; Destination Address (change it)
Global $Port = 161      ; UDP 161  = SNMP port
Global $SNMP_Version = 2     ; SNMP v2c (1 for SNMP v1)
Global $SNMP_Community = "private"   ; SNMPString(Community) - you need the "write" community string to test this.
Global $SNMP_ReqID = 1
Global $SNMP_Command
Global $Start = 1
Global $result
UDPStartUp()
$Socket = UDPopen($dest_IP, $Port)
;Read value at sysLocation
Global $SNMP_OID = "1.3.6.1.2.1.1.6.0"
$SNMP_Command = _SNMPBuildPacket($SNMP_OID, $SNMP_Community,$SNMP_Version, $SNMP_ReqID, "A0")
UDPSend($Socket, $SNMP_Command)
_StartListener()
sleep (200)
_ArrayDisplay($SNMP_Util, "GetRequest 1x OID")
;Write value at sysLocation
Global $SNMP_OID = "1.3.6.1.2.1.1.6.0"
$SNMP_Command = _SNMPBuildPacket($SNMP_OID, $SNMP_Community,$SNMP_Version, $SNMP_ReqID, "A3", "32", "04", "My_TEST")
UDPSend($Socket, $SNMP_Command)
_StartListener()
sleep (200)
_ArrayDisplay($SNMP_Util, "Write 1x OID")
;Read new value at sysLocation
Global $SNMP_OID = "1.3.6.1.2.1.1.6.0"
$SNMP_Command = _SNMPBuildPacket($SNMP_OID, $SNMP_Community,$SNMP_Version, $SNMP_ReqID, "A0")
UDPSend($Socket, $SNMP_Command)
_StartListener()
sleep (200)
_ArrayDisplay($SNMP_Util, "GetRequest 1x OID")

Func _StartListener()
If $Start = 1 Then
  $i = 0
  While (1)
   $srcv = UDPRecv($Socket, 2048)
   If ($srcv <> "") Then
    $result = _ShowSNMPReceived ($srcv)
    ConsoleWrite($srcv &@CRLF)
    ;_ArrayDisplay($result)
    ExitLoop
   EndIf
   sleep(100)
  WEnd
EndIf
EndFunc
Func OnAutoItExit()
    UDPCloseSocket($Socket)
    UDPShutdown()
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...