enaiman

SNMP_UDF for SNMPv1 and SNMPv2c

155 posts in this topic

#1 ·  Posted (edited)

NOTE: Since I am not around here too much - please feel free to fix/improve the code if you want to - it is available to everyone so absolutely no problem. Even giving credit is optional :)

 

Last Updated 17/10/2012

So far it works only for SNMPv1 and SNMP v2c ... I will try to add support for SNMPv3 in the future

If you want to test this UDF please download the au3 file. Because of some long lines the code display will truncate many of the lines and that will show on the script if you use copy-paste.

A long time ago I've made a "promise" to ptrex that I will share my findings on SNMP. Now I do have some findings smile.gif

I did a little work on this UDF; I know there is more to SNMP than included in this UDF; there are still parts to be added and information.
I provide this UDF "as is"; I will work to improve it in the future and I will try to help others as much as possible.

A big credit for this goes to @ptrex; I've used some of his work in writing this UDF and the example.
Credit goes also to @nobur for fixing version 1.7.3

I know that there might be some bugs but only the feedback can help me discover them.


[03/10/2008] Finally I've got the code to work 100% correct on all my tests. I have redone quite a significant bit of the code and added a couple more functions.

[30/12/2008] UDF code rewritten. ... not easy but I had to do it in order to add support for Multiple OIDs and packet lenghts of 256 and over.
Still compatible with SNMP v1 and SNMP v2c ; maybe in the future I will be able to work a little more on it.
UDF and example script updated.

[10/03/2009] Important changes again:
- added support for GetBulk requests (get a large amount of OID contents returned in the same packet) The amount of OIDs returned can be specified as a parameter. It can work with a large amount of OIDs but I recommend using it for 50-60 OIDs (some OID can be very long and it might reach and go over all array limits in the udf); anyway - you can play with the global variables (array dimension) in the udf and see the results.
For 50 OIDs it is safe smile.gif
- Changed the return of function _ShowSNMPReceived -> the $SNMP_Received array is still there (it has an "educational" value and it is still built but it is never returned) but the function now returns another array $SNMP_Util which has only OID and clear values inside -> easier to work with this array.
- Updated example script: 4 different requests in the same script

[30/03/2009]
Bug fix: OID numbers above 16384 were incorrectly decoded - changed "_SNMPExtractOID" function to correct this bug. (thanks Sjee)

[02/04/2009]
Bug fix: Fixed the encoding of OID numbers over 128

[26/08/2010]
SNMP_UDF v 1.4 released:
Fixed: SetRequest is now working - by error I have assumed that it works :> and it wasn't - thanks GSM
Still no hope for SNMP v3 (sorry)

[31/08/2010]
SNMP_UDF v 1.4.1 released:
Bug Fix: wrong value returned for OID -> thanks GSM again smile.gif

[09/09/2010]
It seems that I forgot a debugging MsgBox inside the UDF - fixed that smile.gif

[01/10/2010]
SNMP_UDF v 1.5 released:
Completely re-written the UDF. Minor change in $SNMP_Util structure (error code is now held in $SNMP_Util[0][1])
- fixed many issues with SetRequest and Bulk querries
- I didn't have much time to test it so there might be errors (please let me know)

[04/10/2010]
Fixed an error (a function was missing from UDF) - thanks @AleksanderWalesa

SNMP_UDF v 1.5:
- SNMP version supported: v1, v2c
- Data types returned (in clear): counter, gauge, string, integer, IP Address, OID, Time Ticks
- returns an array with the following: Request Id, Error Index, Error, Data Type, Data Length, Data Value and Data Value in "clear" form
- the returned array has many more information; you can see effectively the structure of SMNP packet
- Multiple OIDs support; you can build packets using up to 8 OIDs by passing an array of OIDs as parameter (one request for 8 OIDs) and decode packets having even more OIDs
- support for "long" packets
- support for GetBulk requests
- support for SetRequest

[01/04/2011]
Removed support for OID Arrays; there wasn't enough value in this feature - it was only complicating the UDF and debugging. Packets are generated fast enough so the performance loss will be minimum.
SNMP_UDF v 1.5.1:
- removed support for OID arrays

[29/08/2011]
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.
SNMP_UDF v 1.6:
- added a new element in $SNMP_Received

[30/08/2011]
Minor bug fix - v1.6 was giving errors ... bad array handling :|
Fixed that in 1.6.1
I cannot get the "full editor" to work so - check the last post for the new updated version.

I will remove the link to 1.6 for now.

[02/11/2011]
New version 1.6.2
Added Error control - when the querry returns an error - it will display an associated error message and will exit - no more crashes.

[07/11/2011] - New version SNMP_UDF v 1.7
Completely rewritten - no more arrays used to build/decode SNMP packets - now using strings only. I guess a dramatical speed increase can be noticed.
I didn't test it fully so if there are any bugs - please let me know and I will fix them.

[13/01/2012] - New (minor) version SNMP_UDF v 1.7.1
I had to change it a little bit to make it work with AutoIt 3.3.8.0
StringLen was buggy and I had to fix all script lines involving this function.


[16/01/2012] - New (minor) version SNMP_UDF v 1.7.2
I can't remember what I have fixed in this but it was definitely fixing something smile.gif

[27/06/2012] - Another New (minor) version SNMP_UDF v 1.7.3
A bug in the _SysObjIDToHexString function was affecting encoding big numbers and another one affecting OIDs returned.

[17/10/2012] - Another New (minor) version SNMP_UDF v 1.7.4
A bug about null value corrected by @nobur


@nobur posted a SNMP Walk function


Thanks nobur :)

To DO:
- change the way the packet is decoded - at the moment, there are lots of _ArrayDelete functions and these might slow the script - I'll try to make the UDF faster
- add support for SNMP v3

Latest versions attached.

Attached files:

snmp_UDF-v1.7.4.au3
snmp_packetgen_1.7.4.au3
snmp_packetgen_write_1.7.4.au3

Edited by enaiman
1 person likes this

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 :)

Share this post


Link to post
Share on other sites



Haven't tested it yet but this will be a very useful addition for sysadmins to be about to monitor the status of their devices on their networks.

I would propose to have it added to the standard UDF's when proven to be working and stable.

Jos :)


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

@enaiman

Good to see that you polished my rough draft into a UDF.

Unfortunately I could not get any results yet using it on an HP Printer as a test.

The string that worked for me is the top one the second one is from your UDF.

0x302B02010004067075626C6963A01E0202014402010002010030123010060C2B060104010B0204030102000500

0x302B02010204067075626C6963A01E0202000002010002010030123010060C2B060104010B0204030102000500

There are still some differences, and of course because these no result is coming back from the printer.

[Edit : Forgot to mention the command string I used.]

$cmd =  _SNMPBuildPacket("1.3.6.1.4.1.11.2.4.3.1.2.0", "public", 3, 0, "A0")oÝ÷ Ù*&[.¬¶*'°(ºW@uß©àzØ^P1]yö®Û-¢Ø^±æ®¶­sdgVæ2õ4äÕ'VÆE6¶WBb33c·6æ×ôBÂb33c·6æ×4ôÔÒÒgV÷CµV&Æ2gV÷C²Âb33c·6æ×dU"Ò2Âb33c·6æ×&WBÒÂb33cµEUGRÒgV÷C´gV÷C²

Hopefuly some more people can do some testing.

Great job so far !!

Regards,

Ptrex

Edited by ptrex

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thank you Jos and ptrex,

I will definitely improve it once I can get some feedback.

@ptrex

The only difference I see between the 2 strings is in the RequestID - your string shows you have used RequestID = 1 and you have used my UDF to generate a string with RequestID=0; I wonder if changing the RequestID to 1 won't get it working ... can you test that for me please?

Another difference is the bits coming right after the RequestID (44 in your string and 00 in mine) - I couldn't figure yet what these bits are and I've been testing many values in that position; so far any value in that position had no effect. I will try to find about that.

But I guess the most important difference is the SNMP version you tried to use: SMNP v3 is veeery different from SNMPv1 and v2, maybe the packet structure is not so different but the setup at "both ends" is; I remember we had to configure profiles on both ends and choice of encryptions ... anyway, this UDF won't work on v3 right now. I will add support first for v2 and sometime later for v3 (if I can) but now, it only works for SNMP v1.

I've mentioned that in the UDF but I guess I wasn't placed it in a good enough place.

About changes in the Parameters default values: thanks for suggestions, I will implement that :)

EDIT: I have corrected a minor glitch which caused the first character to be missed when using "A0" requests - I will update the UDF and code...

Also it looks like any try to edit my first post is messing it up (maybe it's too long because my example gets messed) so I will put the example only as an attached document. Sorry for inconvenience.

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 :)

Share this post


Link to post
Share on other sites

@enaiman

We are nearly there.

The UDF seems to work now indead, if I use SNMP v 1.

But now I don't any return from your "_ShowSNMPReceived" function ?

The following return should give "READY TO PRINT"

0x303902010104067075626C6963A22C020201440201000201003020301E060C2B060104010B020403010200040E72656164

7920746F207072696E74

If you can fix the return Function than I can do more tests.

By the way the "(44 in your string and 00 in mine) "related to the SNMP Error code

Regards,

ptrex

Share this post


Link to post
Share on other sites

didn't test it but that's a really good addition.

keep up the good work !

and thx for that :)


-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

@ptrex

Thank you very much for testing it.

I have done my homework today and I've changed quite a bit of code - it ended up working in 100% of my tests. I can't say it's bug free but I hope to find all "bugs". :)

Waiting for test results >_<

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 :)

Share this post


Link to post
Share on other sites

@enaiman

Sorry no output yet for me ?

Input UDF give slitely diffeferent Binary string :

Correct string

0x302B02010004067075626C6963A01E0202014402010002010030123010060C2B060104010B0204030102000500

Your UDF output :

0x302B02010004067075626C6963A01E0202000002010002010030123010060C2B060104010B0204030102000500

Output UDF does not return data for me.

Correct string

0x303902010104067075626C6963A22C020201440201000201003020301E060C2B060104010B020403010200040E72656164

7920746F207072696E74

SNMP response on your input UDF :

0x303802010004067075626C6963A22B0201000201000201003020301E060C2B060104010B020403010200040E7265616479

20746F207072696E74

I have tested my string against 5 different HP printers and not problem so far.

In case I use your UDF I can't get it to run.

More test results would be appreciated from other to get a good picture of the current status.

I think you are not far off.

Regards,

ptrex

Share this post


Link to post
Share on other sites

@ptrex

Thanks again for further testing it.

I had a close look at these 2 strings:

Correct string

0x302B02010004067075626C6963A01E0202014402010002010030123010060C2B060104010B0204030102000500

Your UDF output :

0x302B02010004067075626C6963A01E0202000002010002010030123010060C2B060104010B0204030102000500

and the only difference is the request ID - I don't know why it is Request ID 0 on my string - it should be 1 if you have chosen so. Can you try Request ID = 1 and see what happens?

Please help me understand: the next 2 strings are the strings you have received when sending the above strings?

the only difference between them is here:

Correct string

0x303902010104067075626C6963A22C020201440201000201003020301E060C2B060104010B020403010200040E72656164

7920746F207072696E74

SNMP response on your input UDF :

0x303802010004067075626C6963A22B0201000201000201003020301E060C2B060104010B020403010200040E7265616479

20746F207072696E74

because the 2nd one assumes that the request ID length is 1 not 2.

Also - what you can try - is change this line in the UDF: $index = SetItemValue("00", $index) ;========= reason Unknown =================

with: $index = SetItemValue("44", $index) ;========= reason Unknown =================

and see what happens. When you use ReqID=1 and you change the above value to 44 the string generated by my UDF is exactly the same as yours.

Please, try these 3 cases and let me know about results:

RequestID = 1 and above value = 00

RequestID = 1 and above value = 44 --> both strings here will be identical

RequestID = 0 and above value = 44

I'm kinda confused right now ... all my tests on a network switch were successful and I've got an answer anytime.

I do have a couple other equipments I can test this on (but unfortunately no HP printer ...)

Thanks again,


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 :)

Share this post


Link to post
Share on other sites

@enaiman

Hold on.

I think it is working using your last version you released !!

(I had not noticed you updated the first post :) )

My mistake I was still running the previous version of you UDF and Exmple.

I will try to connect to some swithes too and see that I can get a result.

It looks promissing for now.

I'll post the results later on.

Regards,

ptrex

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

@ptrex

I don't know how can I give you more credit for your help than I already did :P

Thank you again for all your work :(

I'm owing you a biiig favour!


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 :)

Share this post


Link to post
Share on other sites

Great script here

One thing I have been trying to make it do though is send multiple OID's. Having a little trouble with it but should be able to figure it out here. Nice program can't wait til v2 is done. Ran this on 3,000 devices and got all s/n's :P would like multiple OIDS so I can collect all their information. Got an ETA on snmp v2?

Share this post


Link to post
Share on other sites

The SNMP v2 is almost ready; I can only support SNMPv2c at the moment because the packet structure is the same as SNMPv1. Actually you can use it right now if you change the SNMP version to 2; however the new data (counter64) won't be shown in clear (but in hex).

About sending multiple OID requests in the same packet: I have considered doing that but for now I'm quite busy to work on it (I'm in a full "moving" process to a new house :P ).

I will work on it in the future and I will post the updated script.


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 :)

Share this post


Link to post
Share on other sites

@ptrex

Thank you :P

I will continue the work on this during next month, so far I've got sample packets to help me figure out the multiple OID. I'm sure I can solve that.

Until then ... I have a house to move :(


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 :)

Share this post


Link to post
Share on other sites

@enaiman

Thank you for all your hard work!

I'm autoit new guy.

Could you give me some help for Cisco switch snmp?

Thank you very much!

Share this post


Link to post
Share on other sites

@leo

I don't have access to any Cisco switches but I might be able to help.

Post your question and ... who knows? maybe someone can 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 :)

Share this post


Link to post
Share on other sites

@enaiman

Thanks for your reply.

My question:

I can generate SNMP packets use your function, but I can't encode the response packets.

I want to get the data as below.

OID: .1.3.6.1.2.1.1.1.0

Value: Cisco Internetwork Operating System Software

IOS C3750 Software (C3750-I9-M), Version 12.1(19)EA1d, RELEASE SOFTWARE (fc1)

Copyright © 1986-2004 by cisco Systems, Inc.

Compiled Mon 05-Apr-04 22:40 by antonino

or

OID: .1.3.6.1.2.1.1.3.0

Value: 234 hours 18 minutes 54 seconds

Now, I only get the character string.

Please kinldy help me.

Share this post


Link to post
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