Mar-01-2018, 04:34 PM
I am just getting started with Python and was hoping to use it to query routers and switches for information like device names, serial numbers etc. My knowledge of Python is from looking at other people's scripts so the syntax might not be perfect.
I am using pysnmp. I pass the IP address and SNMP string as command line parameters and I know that part of the script is working fine . I confirmed by also replacing these variables with hardcoded values in the script. The error that I get stays consistent.
What could I be doing wrong? Please let me know if this post goes to some other group on this forum.
My code is
The error that I get is
File "snmpq.py", line 18, in <module>
v_transport = cmdgen.UdpTransportTarget(ip_addr,SNMP_PORT)
File "C:\Python27\lib\site-packages\pysnmp\hlapi\transport.py", line 19, in __
init__
self.transportAddr = self._resolveAddr(transportAddr)
File "C:\Python27\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@9@2@.@1@6@8@.@1@.@1
: [Errno 11004] getaddrinfo failed
I am trying to reach 192.168.1.1 and can successfully query SNMP using snmpwalk or snmpget
I am using pysnmp. I pass the IP address and SNMP string as command line parameters and I know that part of the script is working fine . I confirmed by also replacing these variables with hardcoded values in the script. The error that I get stays consistent.
What could I be doing wrong? Please let me know if this post goes to some other group on this forum.
My code is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import pysnmp import sys import os from pysnmp.carrier.asyncore.dispatch import AsyncoreDispatcher from pysnmp.carrier.asyncore.dgram import udp, udp6, unix from pysnmp.entity.rfc3413.oneliner import cmdgen from pyasn1.codec.ber import encoder, decoder from pysnmp.proto import api if __name__ = = '__main__' : ip_addr = sys.argv[ 1 ] community_string = sys.argv[ 2 ] SNMP_PORT = 161 cg = cmdgen.CommandGenerator() comm_data = cmdgen.CommunityData(community_string) v_transport = cmdgen.UdpTransportTarget(ip_addr,SNMP_PORT) # v_transport = cmdgen.UdpTransportTarget('192.168.1.1,SNMP_PORT) v_oid = ( 1 , 3 , 6 , 1 , 4 , 1 , 9 , 3 , 6 , 3 , 0 ) result = cg.getCmd(comm_data,v_transport,v_oid) print result |
File "snmpq.py", line 18, in <module>
v_transport = cmdgen.UdpTransportTarget(ip_addr,SNMP_PORT)
File "C:\Python27\lib\site-packages\pysnmp\hlapi\transport.py", line 19, in __
init__
self.transportAddr = self._resolveAddr(transportAddr)
File "C:\Python27\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@9@2@.@1@6@8@.@1@.@1
: [Errno 11004] getaddrinfo failed
I am trying to reach 192.168.1.1 and can successfully query SNMP using snmpwalk or snmpget