Working with PYSNMP con’t – SNMP simple SETs


So inspired by @KirkByers blog on SNMP which I wrote about here using Python 3, I decided that I wanted to go past just reading SNMP OIDs and see how PYSNMP could be used to set SNMP OIDs as well.  As with anything, it’s best to start with the docs and apply the KISS principle.

Why SNMP?

Although RESTful APIs are definitely an easier, more human readable, way to get data in and out of a network device, not all network devices support modern APIs. For a lot of the devices out there, you’re still stuck with good ol’ SNMP.

Tools

SNMP is a horrible human readable language. We have MIBS to translate those nasty strings of digits into something that our mushy brains can make sense of. Thats known as a MIB Browser. There are a lot of MIB browsers out there, which one you use doesn’t matter much, but I would HIGHLY recommend that you get one if you’re going to start playing with SNMP.

http://www.ireasoning.com has a free version that works great on Windows and supports up to 10 MIBs loaded at the same time.

The Goal

There are a lot of powerful actions available through the SNMP interface, but I wanted to keep it simple.  For this project, I wanted to go after something simple. For this project, I wanted to use SNMP to change the SYSCONTACT and SYSLOCATION fields.

For those of you who are used to a CLI, the section of the config I’m looking after resembles this.

 snmp-agent sys-info contact contact

 snmp-agent sys-info location location

 

The Research

So I know what I want to change, but I need to know how I access those values though SNMP. For this, I’ll use the MIB Browser

 

Using the MIB Browser, I was able to find the SYSLOCATION MIB which is identified as .1.3.6.1.2.1.1.6.0

Screen Shot 2014 11 28 at 1 37 36 PM

I was also able to find the SYSCONTACT MIB which is identified as .1.3.6.1.2.1.1.4.0

Screen Shot 2014 11 28 at 1 37 14 PM

 

So now I’ve got the two OIDs that I’m looking for.

The Code

Looking through the PYSNMP documentation, there’s an example there of how to do an  SNMP SET. But they threw in a couple of options there that I didn’t want, specifically, I didn’t want to use the lookupNames option. So I changed the lookupNames option to False and then I was able to use the OIDs above directly without having to find the names of the MIBs.

So looking through the code below, you can see that I’ve created a function which will take an input syscontact and use it as the variable to set the MIB object  .1.3.6.1.2.1.1.4.0 which corresponds to the

SNMP-AGENT SYSCONTACT …  in the configuration of the device.

from pysnmp.entity.rfc3413.oneliner import Camden

cmdGen = cmdgen.CommandGenerator()

#using PYSNMP library to set the network devices SYSCONTACT field using SNMP
def SNMP_SET_SYSCONTACT(syscontact):
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd(cmdgen.CommunityData(‘private’),cmdgen.UdpTransportTarget((‘10.101.0.221’, 161)),(cmdgen.MibVariable(‘.1.3.6.1.2.1.1.4.0’), syscontact), lookupNames=False, lookupValues=True)
# Check for errors and print out results
if errorIndication:
print(errorIndication)
else:
if errorStatus:
print(‘%s at %s’ % (errorStatus.prettyPrint(),errorIndex and varBinds[int(errorIndex)-1] or ‘?’))
else:
for name, val in varBinds:
print(‘%s = %s’ % (name.prettyPrint(), val.prettyPrint()))

 

 Running the Code

Now we run the code

>>>
>>> SNMP_SET_SYSCONTACT(‘test@lab.local’)
1.3.6.1.2.1.1.4.0 = test@lab.local
>>>

Looking back at the MIB Browser we can see that the SYSCONTACT location has been changed to the new value above.

Screen Shot 2014 11 28 at 2 24 50 PM

And when we log back into the network device

 snmp-agent sys-info contact test@lab.local

 snmp-agent sys-info location location

 

Wrap Up

This is just a small proof-of-contact code that shows, although RESTful APIs are definitely sweeter to work with, they are not the only way to programatically interface with network devices.

Comments or Questions?  Feel free to comment below

 

@netmanchris

 

 

 

Advertisement

2 thoughts on “Working with PYSNMP con’t – SNMP simple SETs

  1. from pysnmp.entity.rfc3413.oneliner import cmdgen

    cmdGen = cmdgen.CommandGenerator()

    #using pysnmp library to set the network devices syscontact field usung snmp

    def snmp_set_syscontact(syscontact):
    errorIndication, errorStatus, errorIndex, varBinds = \
    cmdGen.setCmd(cmdgen.CommunityData(‘public’),
    cmdgen.UdpTransportTarget(‘104.236.166.95’,161),
    (cmdgen.MibVariable(‘.1.3.6.1.2.1.1.5.0’),syscontact),
    lookupNames=False,
    lookupValues=True)
    #check for errors
    if errorIndication:
    print(errorIndication)
    else:
    if errorStatus:
    print(‘%s at %s’ % (errorStatus.prettyPrint(),
    errorIndex and varBinds[int(errorIndex)-1] or ‘?’))
    else:
    for name, val in varBinds:
    print(‘%s = %s’ % (name.prettyPrint(), val.prettyPrint()))

    ——————OUTPUT
    ss(‘test@kurwa.pl’)
    Traceback (most recent call last):
    File “C:\Users\rsuda\PycharmProjects\untitled2\venv\lib\site-packages\pysnmp\hlapi\asyncore\transport.py”, line 60, in _resolveAddr
    socket.IPPROTO_UDP)[0][4][:2]
    File “C:\Users\rsuda\AppData\Local\Programs\Python\Python37-32\lib\socket.py”, line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    socket.gaierror: [Errno 11001] getaddrinfo failed
    During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
    File “”, line 1, in
    File “C:\Users\rsuda\PycharmProjects\untitled2\test1.py”, line 10, in snmp_set_syscontact
    cmdgen.UdpTransportTarget((‘104.236.166.95’,161)),
    File “C:\Users\rsuda\PycharmProjects\untitled2\venv\lib\site-packages\pysnmp\hlapi\transport.py”, line 19, in __init__
    self.transportAddr = self._resolveAddr(transportAddr)
    File “C:\Users\rsuda\PycharmProjects\untitled2\venv\lib\site-packages\pysnmp\hlapi\asyncore\transport.py”, line 63, in _resolveAddr
    ‘@’.join([str(x) for x in transportAddr]), sys.exc_info()[1]))
    pysnmp.error.PySnmpError: Bad IPv4/UDP transport address 1@0@4@.@2@3@6@.@1@6@6@.@9@5: [Errno 11001] getaddrinfo failedcaused by : [Errno 11001] getaddrinfo failed

    whats wrong? please help… Ive tried some mods but Im lost

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s