kontron / robotframework-snmplibrary

SnmpLibrary for Robot Framework
Apache License 2.0
12 stars 11 forks source link

IPv6 Support #16

Closed molyland closed 4 years ago

molyland commented 4 years ago

I try to connect and get by IPv6 protocol and received this error:

Test Snmp Base Config IPv6 :: Test Snmp service with default confi... | FAIL | PySnmpError: Bad IPv4/UDP transport address 2019:db8:2222::100@161: [Errno -9] Address family for hostname not supportedcaused by <class 'socket.gaierror'>: [Errno -9] Address family for hostname not supported

molyland commented 4 years ago

I solve the problem in this way:

diff --git a/src/SnmpLibrary/library.py b/src/SnmpLibrary/library.py
index 913f7f1..1dc84f5 100644
--- a/src/SnmpLibrary/library.py
+++ b/src/SnmpLibrary/library.py
@@ -28,6 +28,7 @@ with warnings.catch_warnings():
     from pysnmp.entity.rfc3413.oneliner import cmdgen
     from pyasn1.type import univ
     from pysnmp.proto import rfc1902, rfc1905
+    from pysnmp.error import PySnmpError

 class _SnmpConnection:
@@ -80,10 +81,13 @@ class SnmpLibrary(_Traps):
         if alias:
             alias = str(alias)

-        authentication_data = cmdgen.CommunityData(self.AGENT_NAME,
-                                                   community_string)
-        transport_target = cmdgen.UdpTransportTarget(
-                                        (host, port), timeout, retries)
+        authentication_data = cmdgen.CommunityData(self.AGENT_NAME, community_string)
+        
+        transport_target = None
+        try:
+            transport_target = cmdgen.UdpTransportTarget((host, port), timeout, retries)
+        except PySnmpError:
+            transport_target = cmdgen.Udp6TransportTarget((host, port), timeout, retries)

         connection = _SnmpConnection(authentication_data, transport_target)
         self._active_connection = connection
@@ -161,9 +165,12 @@ class SnmpLibrary(_Traps):
                                     encryption_password,
                                     authentication_protocol,
                                     encryption_protocol)
-
-        transport_target = cmdgen.UdpTransportTarget(
-                                        (host, port), timeout, retries)
+        
+        transport_target = None
+        try:
+            transport_target = cmdgen.UdpTransportTarget((host, port), timeout, retries)
+        except PySnmpError:
+            transport_target = cmdgen.Udp6TransportTarget((host, port), timeout, retries)

         conn = _SnmpConnection(authentication_data, transport_target)
         self._active_connection = conn