Re: [pysnmp-users] pySNMP error handling - OSError: [Errno 64] Host is down - exception created ins
Brought to you by:
elie
From: Ilya E. <il...@gl...> - 2017-07-14 22:10:23
|
Hi Frank, It looks like a routing problem on your network. I guess the “No route to host” error might show up when you are trying to send an IP packet to a host which is neither directly connected to your host nor your host has a default route. I’d first make sure that you can ping the hosts 10.10.10.10, 10.10.10.20. The other idea is to enable pysnmp debugging and read the log to see what pysnmp is doing: from pysnmp import debug debug.setLogger(debug.Debug(‘io’)) > > On 14 Jul 2017, at 22:25, Frank Tonjes <in...@ho...> wrote: > > Hi, > > Just FYI, The error is identical using Python 3.5: (Apologies - should have sent 3.5 in the first place!). > > 10.10.10.10 > SNMPv1 community string 'public' on port '161: > Traceback (most recent call last): > File "/Users/guardian/Sync/Projects/test/lib/python3.5/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 142, in handle_write > self.socket, outgoingMessage, transportAddress > File "/Users/guardian/Sync/Projects/test/lib/python3.5/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 35, in <lambda> > self._sendto = lambda s, b, a: s.sendto(b, a) > OSError: [Errno 65] No route to host > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "/Users/guardian/Sync/Projects/test/lib/python3.5/site-packages/pysnmp/carrier/asyncore/dispatch.py", line 46, in runDispatcher > use_poll=True, map=self.__sockMap, count=1) > File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncore.py", line 207, in loop > poll_fun(timeout, map) > File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncore.py", line 188, in poll2 > readwrite(obj, flags) > File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncore.py", line 123, in readwrite > obj.handle_error() > File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncore.py", line 110, in readwrite > obj.handle_write_event() > File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncore.py", line 442, in handle_write_event > self.handle_write() > File "/Users/guardian/Sync/Projects/test/lib/python3.5/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 148, in handle_write > raise error.CarrierError('sendto() failed for %s: %s' % (transportAddress, sys.exc_info()[1])) > pysnmp.carrier.error.CarrierError: sendto() failed for ('10.10.10.10', 161): [Errno 65] No route to host > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "./networking.py", line 71, in <module> > main() > File "./networking.py", line 66, in main > snmp_query(['10.10.10.10', '10.10.10.20'], 'v1/public:161', ['1.3.6.1.2.1.1.2.0', '1.3.6.1.2.1.1.1.0']) > File "./networking.py", line 28, in snmp_query > result = snmp_get(target, snmp_str, snmp_port, oids, int(snmp_ver[1:])) > File "./networking.py", line 41, in snmp_get > *snmp_oids) > File "/Users/guardian/Sync/Projects/test/lib/python3.5/site-packages/pysnmp/entity/rfc3413/oneliner/cmdgen.py", line 183, in getCmd > **kwargs): > File "/Users/guardian/Sync/Projects/test/lib/python3.5/site-packages/pysnmp/hlapi/asyncore/sync/cmdgen.py", line 113, in getCmd > snmpEngine.transportDispatcher.runDispatcher() > File "/Users/guardian/Sync/Projects/test/lib/python3.5/site-packages/pysnmp/carrier/asyncore/dispatch.py", line 50, in runDispatcher > raise PySnmpError('poll error: %s' % ';'.join(format_exception(*exc_info()))) > pysnmp.error.PySnmpError: poll error: Traceback (most recent call last): > ; File "/Users/guardian/Sync/Projects/test/lib/python3.5/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 142, in handle_write > self.socket, outgoingMessage, transportAddress > ; File "/Users/guardian/Sync/Projects/test/lib/python3.5/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 35, in <lambda> > self._sendto = lambda s, b, a: s.sendto(b, a) > ;OSError: [Errno 65] No route to host > ; > During handling of the above exception, another exception occurred: > > ;Traceback (most recent call last): > ; File "/Users/guardian/Sync/Projects/test/lib/python3.5/site-packages/pysnmp/carrier/asyncore/dispatch.py", line 46, in runDispatcher > use_poll=True, map=self.__sockMap, count=1) > ; File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncore.py", line 207, in loop > poll_fun(timeout, map) > ; File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncore.py", line 188, in poll2 > readwrite(obj, flags) > ; File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncore.py", line 123, in readwrite > obj.handle_error() > ; File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncore.py", line 110, in readwrite > obj.handle_write_event() > ; File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncore.py", line 442, in handle_write_event > self.handle_write() > ; File "/Users/guardian/Sync/Projects/test/lib/python3.5/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 148, in handle_write > raise error.CarrierError('sendto() failed for %s: %s' % (transportAddress, sys.exc_info()[1])) > ;pysnmp.carrier.error.CarrierError: sendto() failed for ('10.10.10.10', 161): [Errno 65] No route to host > > Pretty much identical as in 3.6.1 and no - I didn't just do a search and replace!!! :D > > Frank > >> On 14 Jul 2017, at 20:45, Frank Tonjes <in...@ho... <mailto:in...@ho...>> wrote: >> >> Hi All, >> >> I've only been using Python since Dec last year, so please excuse my lack of Knowledge, but I'm trying to write a script which tests polling before adding the devices in our 3rd party tools. One of the tests are SNMP. Currently I'm looking at just the SNMP v1/v2c as most of our devices fall into this category (well most are v2c with some old stuff on v1). >> >> I've found some examples online and looked at examples others have used and come up with this 'test' script. It's only to test, so I know it's not following good python practice!!!: >> >> #!/usr/bin/env python >> >> import re >> from pysnmp.hlapi import * >> from pysnmp.entity.rfc3413.oneliner import cmdgen >> >> >> def snmp_query(targets, sequence, oids): >> >> # If we get single strings, make it a list with 1 item in it! >> if isinstance(targets, str): >> targets = list([targets]) >> >> if isinstance(oids, str): >> oids = list([oids]) >> >> for target in targets: >> print(target) >> >> for seq in str.split(sequence, sep=','): >> >> info = re.search(r'^(v[123])/(.*):(\d+)$', seq) >> if info: >> >> (snmp_ver, snmp_str, snmp_port) = (info.group(1), info.group(2), info.group(3)) >> print(" SNMP{0} community string '{1}' on port '{2}:".format(snmp_ver, snmp_str, snmp_port)) >> >> if snmp_ver == 'v1' or snmp_ver == 'v2': >> result = snmp_get(target, snmp_str, snmp_port, oids, int(snmp_ver[1:])) >> print(result) >> >> exit() >> >> >> def snmp_get(host, snmp_community, snmp_port, snmp_oids, snmp_ver): >> >> cgen = cmdgen.CommandGenerator() >> >> if snmp_ver == 1: >> error_indication, error_status, error_index, varbinds = cgen.getCmd( >> cmdgen.CommunityData(snmp_community, mpModel=0), cmdgen.UdpTransportTarget((host, snmp_port)), >> *snmp_oids) >> elif snmp_ver == 2: >> error_indication, error_status, error_index, varbinds = cgen.getCmd( >> cmdgen.CommunityData(snmp_community), cmdgen.UdpTransportTarget((host, snmp_port)), >> *snmp_oids) >> else: >> print("\n ERROR: Only snmp_ver of '1' or '2' is supported using snmp_get!") >> exit() >> >> if error_indication: >> return error_indication >> elif error_status: >> return '%s at %s' % (error_status.prettyPrint(), error_index and varbinds[int(error_index) - 1][0] or '?') >> else: >> result = {} >> for varbind in varbinds: >> >> oid, value = varbind >> result[str(oid)] = str(value) >> >> return result >> >> >> def main(): >> >> snmp_query(['10.10.10.10', '10.10.10.20'], 'v1/public:161', ['1.3.6.1.2.1.1.2.0', '1.3.6.1.2.1.1.1.0']) >> exit() >> >> >> if __name__ == "__main__": >> main() >> >> >> It seems to be failing with pysnmp somewhere and doesn't return any errors within the error handling I've copied from the example section in PySNMP documentation: >> >> The output after running the script is: >> >> 10.10.10.10 >> SNMPv1 community string ‘public' on port '161: >> Traceback (most recent call last): >> File "/Users/guardian/Sync/Projects/serenity/lib/python3.6/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 142, in handle_write >> self.socket, outgoingMessage, transportAddress >> File "/Users/guardian/Sync/Projects/serenity/lib/python3.6/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 35, in <lambda> >> self._sendto = lambda s, b, a: s.sendto(b, a) >> OSError: [Errno 64] Host is down >> >> During handling of the above exception, another exception occurred: >> >> Traceback (most recent call last): >> File "/Users/guardian/Sync/Projects/serenity/lib/python3.6/site-packages/pysnmp/carrier/asyncore/dispatch.py", line 46, in runDispatcher >> use_poll=True, map=self.__sockMap, count=1) >> File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncore.py", line 207, in loop >> poll_fun(timeout, map) >> File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncore.py", line 188, in poll2 >> readwrite(obj, flags) >> File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncore.py", line 123, in readwrite >> obj.handle_error() >> File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncore.py", line 110, in readwrite >> obj.handle_write_event() >> File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncore.py", line 442, in handle_write_event >> self.handle_write() >> File "/Users/guardian/Sync/Projects/serenity/lib/python3.6/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 148, in handle_write >> raise error.CarrierError('sendto() failed for %s: %s' % (transportAddress, sys.exc_info()[1])) >> pysnmp.carrier.error.CarrierError: sendto() failed for ('10.10.10.10', 161): [Errno 64] Host is down >> >> During handling of the above exception, another exception occurred: >> >> Traceback (most recent call last): >> File "./networking.py", line 71, in <module> >> main() >> File "./networking.py", line 66, in main >> snmp_query(['10.10.10.10', '10.10.10.20'], 'v1/public:161', ['1.3.6.1.2.1.1.2.0', '1.3.6.1.2.1.1.1.0']) >> File "./networking.py", line 28, in snmp_query >> result = snmp_get(target, snmp_str, snmp_port, oids, int(snmp_ver[1:])) >> File "./networking.py", line 41, in snmp_get >> *snmp_oids) >> File "/Users/guardian/Sync/Projects/serenity/lib/python3.6/site-packages/pysnmp/entity/rfc3413/oneliner/cmdgen.py", line 183, in getCmd >> **kwargs): >> File "/Users/guardian/Sync/Projects/serenity/lib/python3.6/site-packages/pysnmp/hlapi/asyncore/sync/cmdgen.py", line 113, in getCmd >> snmpEngine.transportDispatcher.runDispatcher() >> File "/Users/guardian/Sync/Projects/serenity/lib/python3.6/site-packages/pysnmp/carrier/asyncore/dispatch.py", line 50, in runDispatcher >> raise PySnmpError('poll error: %s' % ';'.join(format_exception(*exc_info()))) >> pysnmp.error.PySnmpError: poll error: Traceback (most recent call last): >> ; File "/Users/guardian/Sync/Projects/serenity/lib/python3.6/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 142, in handle_write >> self.socket, outgoingMessage, transportAddress >> ; File "/Users/guardian/Sync/Projects/serenity/lib/python3.6/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 35, in <lambda> >> self._sendto = lambda s, b, a: s.sendto(b, a) >> ;OSError: [Errno 64] Host is down >> ; >> During handling of the above exception, another exception occurred: >> >> ;Traceback (most recent call last): >> ; File "/Users/guardian/Sync/Projects/serenity/lib/python3.6/site-packages/pysnmp/carrier/asyncore/dispatch.py", line 46, in runDispatcher >> use_poll=True, map=self.__sockMap, count=1) >> ; File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncore.py", line 207, in loop >> poll_fun(timeout, map) >> ; File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncore.py", line 188, in poll2 >> readwrite(obj, flags) >> ; File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncore.py", line 123, in readwrite >> obj.handle_error() >> ; File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncore.py", line 110, in readwrite >> obj.handle_write_event() >> ; File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncore.py", line 442, in handle_write_event >> self.handle_write() >> ; File "/Users/guardian/Sync/Projects/serenity/lib/python3.6/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 148, in handle_write >> raise error.CarrierError('sendto() failed for %s: %s' % (transportAddress, sys.exc_info()[1])) >> ;pysnmp.carrier.error.CarrierError: sendto() failed for ('10.10.10.10', 161): [Errno 64] Host is down >> >> >> I'm on Python 3.6.1 and saw you only support 3.5 so set up a virtualenv with 3.5 and get exactly the same error. It obviously sees the 'Errno 64', but seems this error is not making it back to the error handling (error_indication or error_status?). >> I’m on macOS Sierra 10.12.5 and devices are all on my local LAN - nothing fancy either. >> >> Any idea where to look - does this look like a bug - or is there something wrong with my implementation? Is there a better or more propper way to do what I'm trying to do? >> Idea is to have a pollSequence and use the first one that works e.g. pollSequence of 'v2/public:161,v1/readonly:1961' lets me specific 2 different types of polling with version/community/port no. >> It seems to work fine if the device is reachable via said polling info - but fails like this when the Host is down or doesn't talk SNMP! >> >> Any help would be greatly appreciated. I'm planning to do a lot more complex things with SNMP so need to make sure I'm on the right path early on! >> >> Regards, >> >> Frank >> ------------------------------- |