I finally managed to generate a Get Command to my proprietary MIB with the following python script:
from pysnmp.entity.rfc3413.oneliner import cmdgen
errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(
cmdgen.CommunityData('10.0.1.134', 'admin', 0),
cmdgen.UdpTransportTarget(('10.0.1.134', 161)),
(1,3,6,1,4,1,4515,1,8,1,1,1,8,1295360,1295360)
)
print (varBinds)
print (varBinds[0])
print (varBinds[0][0])
print (varBinds[0][1])
The varBinds print in 4 different ways was just for the learning process. Now I have tried to imitate the same script while turning it to a Set Command (without any success). There are lots of examples to get but non fitted. So, I have tried the following:
from pysnmp.entity.rfc3413.oneliner import cmdgen
errorIndication, errorStatus, errorIndex, rspVarBinds = cmdgen.CommandGenerator().setCmd(
cmdgen.CommunityData('10.0.1.134', 'admin', 0),
cmdgen.UdpTransportTarget(('10.0.1.134', 161)),
(1,3,6,1,4,1,4515,1,8,1,1,1,8,1295360,1295360),
(1),
)
I didn't succeed in generating the Set Command as you can see. And i don't see how can I get it to work. By the way, the OID that I am setting is a read-write, Integer (32 bit) with the following possible values: up(1), down(2) and standby(3).
What do i need to change in order to succeed in Setting the OID with any of the possible values ?
I have tried your suggested script and failed:
from pysnmp.hlapi import SnmpEngine, setCmd, CommunityData, UdpTransportTarget, ContextData, ObjectType, ObjectIdentity
from pysnmp.proto.api.v2c import Integer32
setCmd(SnmpEngine(),
CommunityData('public', mpModel=0),
UdpTransportTarget('10.0.1.134', 161),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.4.1.4515.1.8.1.1.1.8.1295360.1295360'), Integer32(1)))
This is the error messages that I got:
**Traceback (most recent call last): File "C:\Program Files\Python35\lib\site-packages\pysnmp-4.3.2-py3.5.egg\pysnmp\hlapi\asyncore\transport.py", line 56, in _resolveAddr socket.IPPROTO_UDP)[0][4][:2] File "C:\Program Files\Python35\lib\socket.py", line 732, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11004] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "", line 3, in File "C:\Program Files\Python35\lib\site-packages\pysnmp-4.3.2-py3.5.egg\pysnmp\hlapi\transport.py", line 17, in init self.transportAddr = self._resolveAddr(transportAddr) File "C:\Program Files\Python35\lib\site-packages\pysnmp-4.3.2-py3.5.egg\pysnmp\hlapi\asyncore\transport.py", line 58, in _resolveAddr raise error.PySnmpError('Bad IPv4/UDP transport address %s: %s' % ('@'.join([str(x) for x in transportAddr]), sys.exc_info()[1])) pysnmp.error.PySnmpError: Bad IPv4/UDP transport address 1@0@.@0@.@1@.@1@3@4: [Errno 11004] getaddrinfo failed**
What do I need to do in order to fix this ?
You have to wrap your OID-value pair(s) into ObjectType
object(s):
setCmd(SnmpEngine(),
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.pysnmp.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.2.1.1.9.0'), Integer32(1)))
Code above does not really use any MIBs. If you want to operate on a more human friendly terms, you can replace OID with MIB module+symbol and integer with label (as described in the MIB):
setCmd(SnmpEngine(),
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.pysnmp.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('MY-CUSTOM-MIB', 'myCustomVariable', 0), 'up'))
Many examples are on https://docs.lextudio.com/pysnmp/.