Jump to content

Enaiman wrote SNMP_UDF: I have difficulties to use it.


rudi
 Share

Recommended Posts

Hi.

To read the counters of a subset of our printers I wrote this code, using this UDF of enaiman:

for the first value it's working fine, for the 2nd vaule the UDP:161 packet is never sent. I've already tried to do a UDPStartup() and UDPShutdown() for each and every value inside the "$x = 1 to ..." loop, no change.

xerox.au3

#include <array.au3>
#include <date.au3>
#include 'SNMP_UDF_v1.7.3.au3'


Global $Port = 161 ; UDP 161 = SNMP port
Global $SNMP_Version = 2 ; SNMP v2c (1 for SNMP v1)
Global $SNMP_Community = "public" ; SNMPString(Community) (change it)
Global $SNMP_ReqID = 1
Global $SNMP_Command
Global $Start = 1
Global $result



If Not @Compiled Then Opt("trayicondebug", 1)

$ini = StringTrimRight(@ScriptName, 3) & "ini"
$sect = "Drucker"

$aDrucker = IniReadSection($ini, $sect)
$Ergebnis = "Zählerstände der Xerox Drucker von: " & _NowCalc() & @CRLF


; UDPStartup() --> moved inside "for $x = 1 to ..." loop: no change

For $i = 1 To $aDrucker[0][0]
$IP = $aDrucker[$i][0]
ConsoleWrite($IP & @LF)
ConsoleWrite($aDrucker[$i][1] & @LF)
$Ergebnis &= $aDrucker[$i][1]
$OIDsect = $aDrucker[$i][1]
$OIDsect = StringTrimLeft($OIDsect, (StringInStr($OIDsect, " ", 0, -1)))
$aOID = IniReadSection($ini, $OIDsect)
For $x = 1 To $aOID[0][0]
UDPStartup()
$Socket = UDPOpen($aDrucker[$i][0], $Port)
$SNMP_Command = _SNMPBuildPacket($aOID[$x][1], $SNMP_Community, $SNMP_Version, $SNMP_ReqID, "A0")
UDPSend($Socket, $SNMP_Command)
_StartListener()
Sleep(200)
$Ergebnis &= @TAB & $aOID[$x][0] & @TAB & $SNMP_Util[1][1] & @CRLF
_ArrayDisplay($SNMP_Util, $aDrucker & ", " & $x)
ConsoleWrite($aOID[$x][0] & ", " & $aOID[$x][1] & " = " & $SNMP_Util[1][1] & @LF)
UDPCloseSocket($Socket)
UDPShutdown()
Sleep(200)
Next
Next

; UDPShutdown()

ConsoleWrite($Ergebnis & @LF)



Func _StartListener()
If $Start = 1 Then
$i = 0
While (1)
$srcv = UDPRecv($Socket, 2048)
If ($srcv <> "") Then
$result = _ShowSNMPReceived($srcv)
ConsoleWrite("received: " & $srcv & @CRLF)
;_ArrayDisplay($result)
ExitLoop
EndIf
Sleep(100)
WEnd
EndIf
EndFunc ;==>_StartListener

Func OnAutoItExit()
UDPCloseSocket($Socket)
UDPShutdown()
EndFunc ;==>OnAutoItExit

the ini used:

xerox.ini

[drucker]
10.27.50.34=Doku Xerox-7525
10.27.50.38=AV Xerox-7232
10.27.50.39=Vertieb1 Xerox-7232
10.27.50.40=TBE Xerox-7232
10.27.50.41=Verwaltung Xerox-7232
10.27.50.43=AV Xerox-7435
10.27.50.44=TBE Xerox-7435
; ....


[Xerox-7232]
Total=1.3.6.1.4.1.253.8.53.13.2.1.6.1.20.1
bw=1.3.6.1.4.1.253.8.53.13.2.1.6.1.20.34
color=1.3.6.1.4.1.253.8.53.13.2.1.6.1.20.33

[Xerox-7435]
total=1.3.6.1.4.1.253.8.53.13.2.1.6.1.20.1
bw=1.3.6.1.4.1.253.8.53.13.2.1.6.1.20.34
color=1.3.6.1.4.1.253.8.53.13.2.1.6.1.20.33
oversize total=1.3.6.1.4.1.253.8.53.13.2.1.6.1.20.47
oversize BW=1.3.6.1.4.1.253.8.53.13.2.1.6.1.20.44
oversize color=1.3.6.1.4.1.253.8.53.13.2.1.6.1.20.43

; .....

Wireshark traces show, that the SNMP request for the 2nd value is never sent.

console output:

10.27.50.34
Doku Xerox-7525
0x303002010104067075626C6963A023020200010201000201003017301506102B06010401817D08350D020106011401050100
received: 0x303002010104067075626C6963A2230201010201000201003018301606102B06010401817D08350D020106011401020211C1
Gesamt, 1.3.6.1.4.1.253.8.53.13.2.1.6.1.20.1 = 4545
0x303002010104067075626C6963A023020200010201000201003017301506102B06010401817D08350D020106011422050100

What do I miss?

Regards, Rudi.

<edit: topic tags, added author's name of the UDF (enaiman)>

<edit2: topic text>

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

  • 2 weeks later...

I've found why your script was not advancing - it is my fault because I have an $i = 0 instruction left in the example which caused the loop to reset everytime.

I did a little bit of cleaning and your script should work properly now.

#include <array.au3>
#include <date.au3>
#include 'SNMP_UDF_v1.7.3.au3'


Global $Port = 161 ; UDP 161 = SNMP port
Global $SNMP_Version = 2 ; SNMP v2c (1 for SNMP v1)
Global $SNMP_Community = "public" ; SNMPString(Community) (change it)
Global $SNMP_ReqID = 1
Global $SNMP_Command
Global $Start = 1
Global $result
Global $Timeout_msec = 2500


If Not @Compiled Then Opt("trayicondebug", 1)

$ini = StringTrimRight(@ScriptName, 3) & "ini"
$sect = "Drucker"

$aDrucker = IniReadSection($ini, $sect)
$Ergebnis = "Zählerstände der Xerox Drucker von: " & _NowCalc() & @CRLF


UDPStartup()


For $i = 1 To $aDrucker[0][0]
$IP = $aDrucker[$i][0]
$Socket = UDPOpen($aDrucker[$i][0], $Port)
ConsoleWrite($IP & @LF)
ConsoleWrite($aDrucker[$i][1] & @LF)
$Ergebnis &= $aDrucker[$i][1]
$OIDsect = $aDrucker[$i][1]
$OIDsect = StringTrimLeft($OIDsect, (StringInStr($OIDsect, " ", 0, -1)))
$aOID = IniReadSection($ini, $OIDsect)
For $x = 1 To $aOID[0][0]
$SNMP_ReqID += 1
$SNMP_Command = _SNMPBuildPacket($aOID[$x][1], $SNMP_Community, $SNMP_Version, $SNMP_ReqID, "A0")
UDPSend($Socket, $SNMP_Command)
_StartListener()
Sleep(200)
$Ergebnis &= @TAB & $aOID[$x][0] & @TAB & $SNMP_Util[1][1] & @CRLF
_ArrayDisplay($SNMP_Util, $aDrucker & ", " & $x)
ConsoleWrite($aOID[$x][0] & ", " & $aOID[$x][1] & " = " & $SNMP_Util[1][1] & @LF)
Next
UDPCloseSocket($Socket)
Next

UDPShutdown()
Sleep(200)

ConsoleWrite($Ergebnis & @LF)



Func _StartListener()
If $Start = 1 Then
$Timeout = TimerInit()
While (1)
$srcv = UDPRecv($Socket, 2048)
If ($srcv <> "") Then
$result = _ShowSNMPReceived($srcv)
ConsoleWrite("received: " & $srcv & @CRLF)
;_ArrayDisplay($result)
ExitLoop
EndIf
Sleep(100)
If TimerDiff($Timeout) > $Timeout_msec Then
ExitLoop
EndIf
WEnd
EndIf
EndFunc ;==>_StartListener

Func OnAutoItExit()
UDPCloseSocket($Socket)
UDPShutdown()
EndFunc ;==>OnAutoItExit

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

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