Jump to content

SNMP_UDF for SNMPv1 and SNMPv2c


enaiman
 Share

Recommended Posts

@joecolgee

From what you are saying, I suspect is a matter of your switch not answering an SNMP querry.

Because you said you have changed the IP, I will believe the new IP is correct. But how about community string? You can see that it is set to "private" in my script - you sure you don't have another community string?

If the community string is correct - does your switch accept SNMP queries from your computer's IP?

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

Hi enaiman...

Thanks for the quick reply!!!.. as a test I run this script (see below) from the same computer and it changes the location OID on the cisco everytime ... but because Shellexecute() only returns a success/fail So I cant use it to read location OID and capture that...

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
;Needs set of Net-SNMP tools installed to get working
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("IPTV Decoder Channel Select", 776, 602, 759, 257)
$Group1 = GUICtrlCreateGroup("London", 16, 56, 225, 505)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("Agincourt", 272, 56, 225, 505)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group3 = GUICtrlCreateGroup("Montreal", 528, 56, 225, 505)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Combo1 = GUICtrlCreateCombo("", 32, 88, 161, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
$Combo2 = GUICtrlCreateCombo("", 100, 200, 200, 50, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "Channel1|Channel2|Channel3|Channel4")
GUICtrlSetData($Combo1, "Channel1|Channel2|Channel3|Channel4|Channel5|Channel6|Channel7|Channel8|Channel9|Channel10|Channel11|Channel12")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $Combo1
   $snmpstring = String("-v 2c -c private 192.168.1.2 1.3.6.1.2.1.1.6.0 = " & GUICtrlRead($Combo1))
   ;MsgBox(0, "", $snmpstring)
   $reply = ShellExecute("snmpset", $snmpstring, "","", @SW_HIDE)
   If $reply = 1 Then
    MsgBox(0, "", "Decoder 1 @ Eton Dorney has been set to decoder " & GUICtrlRead($Combo1))
   Else
    MsgBox(0, "", "did not complete")
   EndIf
EndSwitch
WEnd

Thanks again!

Link to comment
Share on other sites

Hmmm - I am confused about what you want to do.

I have modified your script to write your combo content - it's in a hurry , messy and untested.

Have fun

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <String.au3>
#Include <Array.au3>
#Include 'SNMP_UDF_v1.7.2.au3'
;Needs set of Net-SNMP tools installed to get working
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("IPTV Decoder Channel Select", 776, 602, 759, 257)
$Group1 = GUICtrlCreateGroup("London", 16, 56, 225, 505)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("Agincourt", 272, 56, 225, 505)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group3 = GUICtrlCreateGroup("Montreal", 528, 56, 225, 505)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Combo1 = GUICtrlCreateCombo("", 32, 88, 161, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
$Combo2 = GUICtrlCreateCombo("", 100, 200, 200, 50, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "Channel1|Channel2|Channel3|Channel4")
GUICtrlSetData($Combo1, "Channel1|Channel2|Channel3|Channel4|Channel5|Channel6|Channel7|Channel8|Channel9|Channel10|Channel11|Channel12")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
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)
Global $SNMP_OID = "1.2 1.3.6.1.2.1.1.6.0"


While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $Combo1
 $SNMP_Command = _SNMPBuildPacket($SNMP_OID, $SNMP_Community,$SNMP_Version, $SNMP_ReqID, "A3", "32", "04", GUICtrlRead($Combo1))
 UDPSend($Socket, $SNMP_Command)
 $reply = _StartListener()
 sleep (200)
   If $reply = 1 Then
    MsgBox(0, "", "Decoder 1 @ Eton Dorney has been set to decoder " & GUICtrlRead($Combo1))
   Else
    MsgBox(0, "", "did not complete")
   EndIf
EndSwitch
WEnd
Func _StartListener()
If $Start = 1 Then
  $i = 0
  While (1)
   $srcv = UDPRecv($Socket, 2048)
   If ($srcv <> "") Then
    $result = _ShowSNMPReceived ($srcv)
    If @error Then
  Return 0
 EndIf
 ConsoleWrite($srcv [email="&@CRLF"]&@CRLF[/email])
    ExitLoop
   EndIf
   sleep(100)
  WEnd
  Return 1
EndIf
EndFunc
Func OnAutoItExit()
    UDPCloseSocket($Socket)
    UDPShutdown()
EndFunc

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 Enaiman..

the purpose of the script is to change an SNMP setting on a device based on the selection within the combobox.The OID is not the one that will be used but simply a test one. The cisco device Im testing with is also not the end device but all I had on hand, I have since tested with both cisco and the end device.

In my original script, I build the paramater for a external program that I call with Shellexecute.

Hope this makes some sort of sense.

anyhow, I ran your script (errored out on the consolewrite() line 63, so I commented out and got a little further) it error-ed out with this msgbox (See Jpg)

in the console window I have the following

>Running:(3.3.8.1):C:Program Files (x86)AutoIt3autoit3.exe "D:testing scriptsTestingForum.au3"

0x3031020101040561646D696EA3250202000102010002010030193017060B2B0102030601020101060004084368616E6E656C39

Below was on my clipboard

0x3030020101040561646D696EA22402010102011102010130193017060B2B0102030601020101060004084368616E6E656C39

Based on the error I can only assume the case is being

anyhow I really appreciate the help and I dont want to over stay my welcome with the questions...

Edited by joecoolgee
Link to comment
Share on other sites

@joecolgee

If you got something in the clipboard, that means your device has answered your querry but the error number was not an usuaal one.

After a quick look at the result you had in the clipboard the error number returned was x11(hex) = 17 and that error means "Not Writtable" Simply put - you were trying to write something to a read-only OID.

Your issue made me realise that I need to include more error codes in my UDF; I will update it soon.

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

  • 5 weeks later...

@enaiman

Hello and, first of all THANKS a lot pour this useful UDF.

I just started to communication with my snmp switches to collect data.

I just discovered a small bug in the release 1.7 concerning SNMP SET command.

In fact, when using your original UDF for setting the sysname (which is writable), I got a stop of the application without any information of what was wrong.

So I decided to capture the sent frame using wireshark (another good free software) and compare the SET frame sent by the UDF and a SET frame send by a SNMP software.

When setting the string, I got a [Malformed packet] and discovered (by comparison) that for String value, the nb of bytes to write have to be sent before the string to set

in hexa, if I want to SET this value : 4041424344 (hex ascii value of "ABCDE"), i have to send : 054041424344 (5 bytes "ABCDE").

so i changed your code - the switch part on line 194 to :

Switch $p_dTYPE
  Case $SNMP_data_STR  ;(STR)
   $p_dVALUE = Hex(StringLen($p_dVALUE),2) & _StringToHex($p_dVALUE)
  Case $SNMP_data_INT, $SNMP_data_COUNTER, $SNMP_data_GAUGE, $SNMP_data_TIME  ;(INT, COUNTER, GAUGE, TIME)
   $p_dVALUE = _StringToHex($p_dVALUE)
  Case $SNMP_data_NULL             ;NULL
   $p_dVALUE = "00"
  Case $SNMP_data_OID               ;OID
   $p_dVALUE = _TranslateOID($p_dVALUE, "2h")
  Case $SNMP_data_IP                ;IP
   $p_dVALUE = _SNMPEncodeIP($p_dVALUE)
  Case Else
   Return SetError(1)
EndSwitch

With that addition, you can now set strings without errors !

Reading a bit of your UDF, you put a comment that getBulk should not exceed 50.

what do you suggest if I need to read more than 50 values ? have any hint of the change to do in the code ?

(I need to read the forwarding database of a switch which can contain up to 2000 mac addresses)

Best regard and a big "Holla" for your work.

pegaze01

Edited by pegaze01
Link to comment
Share on other sites

@pegaze01

I think you are still using an old version (most current is 1.7.2)

For some reason my browsers fail when attempting to use the full editor so I can't post the updates on the first post.

Try this version and let me know if it works.

Regarding the limit of 50 OIDs, the packet will become too big and it is likely to be split; I am not such a good scripter/programmer to be able to handle communication with multiple packets.

My advice for you: build a loop and go for 32 entries at the time. I don't like it but I don't have an alternative. You can google for an alternative; somebody else might have a SNMP freeware you could use.

I have to say, my UDF is an enthusiast project, I never intended it to be used at any "serious" level and I don't have the required time (and patience) to bring it to that level.

SNMP_UDF_v1.7.2.au3

snmp_packetgen_write.au3

snmp_packetgen_1.7.2.au3

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

enaiman, I've removed the attachments in the first post as you've requested. Could you please try to work out what may be the cause of your issue with the full editor? Can you edit other posts using the full editor? Can you now edit the first post in this thread after I removed the attachments? I would like to see your issue resolved to determine if it is some quirk on your end or if it's a problem with the post and/or forum.

Note that I obviously had no issues editing it using Firefox 12.

Link to comment
Share on other sites

Hi Valik,

Thank you for fixing this for me.

I have no idea what is happening here but I suspect it might be something with my PC or something is blocked/disabled (I'm at work).

No matter which post I try to edit the bloody editor won't start.

I've tested it from home (via Remote Desktop) and it works perfectly, so it is definitely something at my end (work PC or work environment).

Thanks Valik for help.

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

  • 1 month later...
  • 3 weeks later...

Hi enaiman,

i was facing the same OID big number defect few month ago with your 1.7.2 version.

I corrected your library by myself but forget to advise you before.

I was really happy to see this morning that you've corrected the problem (surely better than i can have done ) but this still doesn't work for me.

I use your UDF to query à NetApp filer and OIDs i must work with are far from what it can handle

ie : .1.3.6.1.4.1.789.1.5.10.1.5.177985217.40 or .1.3.6.1.4.1.789.1.5.10.1.3.244893440.1

Here is the modifed UDF i did. Maybe it can help you to improve your library.

snmp_UDF-nob.au3

(PS: I hope you'll understand my code better than my english ;-) )

Best regards,

Nobur

Link to comment
Share on other sites

Hi Nobur,

Thank you very much for your help.

That is a damn big number :) never imagined that would be used for an OID and that will explain why my UDF was working for smaller numbers.

I'll definitely do some tests to see that everything works fine then I'll update the UDF.

Thanks for providing a solution for this bug :)

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,

I am pleased to ear that my contribution helped.

For those like me who look for a SNMPWALK function, i'll join some code that did the job for me ( maybe not the best but working )

I saw someone using 'getBulk' to achieve this but my needs were far from the actual limitation ( > 1000 subitems )

#include-once
#include 'SNMP_UDF.au3'

Func _SnmpWalk($S_srvIP,$S_OID,$S_COM,$S_Ver=2,$S_Port=161)
;~  Init variables
Dim $S_ReqID = Random(10000,20000,1)
Dim $S_socket,$SNMP_Command,$OIDwalk,$OIDwalk_len,$rslt
Dim $resultTab[1][2]
$resultTab[0][0]=0
;~  open Udp socket to target
UDPStartUp()
$S_socket = UDPopen($S_srvIP, $S_Port)
$OIDwalk=$S_OID
$OIDwalk_len=StringLen ( $OIDwalk )
;~  Cycle through Child OID's
While (StringLeft($S_OID,$OIDwalk_len)=$OIDwalk)
  $SNMP_Command = _SNMPBuildPacket($S_OID, $S_COM,$S_Ver, $S_ReqID, "A1")
  UDPSend($S_socket, $SNMP_Command)
  $rslt= _StartListener($S_socket)
;~   if answer didn't come before timeout then exit and return -1
  if $rslt=-1 Then
   $resultab=-1
   ExitLoop
  EndIf
  if StringLeft($rslt [1][0],$OIDwalk_len)<>$OIDwalk Then ExitLoop
  $resultTab[0][0] += 1
  ReDim $resultTab[$resultTab[0][0]+1][2]
  $resultTab[$resultTab[0][0]][0] = StringRight($rslt[1][0],StringLen($rslt[1][0]) - StringInStr($rslt[1][0], ".", 2, -2) + 1)
  $resultTab[$resultTab[0][0]][1]=$rslt[1][1]
  $S_OID = $rslt [1][0]
  $S_ReqID+=1
WEnd
;~  close connection and return results
UDPCloseSocket($S_socket)
    UDPShutdown()
Return $resultTab
EndFunc

Func _StartListener($sock,$timeOut=500)
dim $sleepDelay=30 ; millisecond
Dim $TOCycles=$timeOut/$sleepDelay
$i = 0
While ($i<=$TOCycles)
  $srcv = UDPRecv($sock, 2048)
  If ($srcv <> "") Then
   $result = _ShowSNMPReceived ($srcv)
   ConsoleWrite($result)
   Return $result
   Exit
  EndIf
  Sleep(30)
  $i+=1
WEnd
;~  fallback condition on timeout
return -1
EndFunc

this returns 2D array of value.

1st value ([0][0]) contains the number of answers.

All others entry contains OID suffix (first dimension) and it assigned value (second dimension)

$array[0][0] = number of results

$array[1][0] = oid suffix

$array[1][1] = value

$array[2][0] = oid suffix

$array[2][1] = value

Hope this will be usefull for somebody.

Nobur

Link to comment
Share on other sites

  • 1 month later...

Hi,

I got stuck with this UDF. Some years ago I used it and everything worked well. But today even the sample snmp_packetgen_1.7.3.au3 won't work. I just changed the IP in the sample to match the IP of my switch. Using snmpget from Net-SNMP gives me the correct sysDescr, using the sample results in a waiting application. Using wireshark I see some Wrong value length problem with the packet which is not there while using net-snmp.

Using Autoit 3.3.8.1 and SNMP_UDF_v1.7.3.au3

Got the exact same result using two different machines (one running XP, one running W7x64)

Could anyone give me a hint?

Max

Link to comment
Share on other sites

Hi enaiman and suukit,

i'm not an expert in snmp but i think there is a small mistake in the code.

in "_SNMPBuildPacket" function the data default value is 00 and not null

@suukit :

can you try to apply the following temporary correction:

in SNMP_UDF_v1.7.3.au3 file can you replace line 221 :

$_result_ = $p_dTYPE & $_p_dLen & $p_dVALUE

by following code :

if Number($p_dTYPE)=05 then
    $_result_ = $p_dTYPE & "00"      ;if null send null data type and  null data value
Else
    $_result_ = $p_dTYPE & $_p_dLen & $p_dVALUE    ;if not null, send data type, data lenght and data value
EndIf

@enainam

what i understand:

when data is null, we must just sent a null value and not a size of 1octet followed by a null value

i our case this UDF send a size for a null value: Some device dont care but some do care ( like cisco's switches)

nobur

edit : my code was not optimal : i just correct it.

Edited by nobur
Link to comment
Share on other sites

  • 1 month later...
  • 3 weeks later...

Hi Guys/Gals

Thx for this UDF, it's mint, I've tweaked the code slight to output to a .txt file, and also added in a part in the .ini section for devices to specify which version of SNMP 1 or 2, e.g.

10.10.3.2=ITOFFICE,2,SAMSUNG

as I found that some of the older HP printers wouldn't return anything using just ver 2.

I then did a string split here, which defines your SNMP_Version

$OIDSplit = StringSplit($aPrinter[$i][1], ",")

$SNMP_Version = $OIDSplit[2]

and I also added in a ping to check that the device is actually online and an if..then..else clause and these lines in the else clause

ConsoleWrite(@LF & $IP & " not responding to ping, logged in error log" & @LF)

_FileWriteLog("error.log", $IP & " did not respond to a ping request.")

and then... I set about writing a GUI (work in progress) to allow me to add new devices, MIB OID's and Groups to the .ini file, meaning I don't need to touch the .ini

Posted Image

Ultimately I plan to output the results to an XML file, which in turn will be read by a WEB page, and hey presto... SNMP Devices monitoring

oh and I found a tool to read MIB OID's called Getif, it's free and takes the pain out of getting the correct MIB OID

Regards

RichE

Edited by RichE

RichE

[font="'Arial Narrow';"]Current projects[/font]

[font="'Arial Narrow';"]are on my site [/font]Sellostring

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...