Pysnmp openServerMode与IPv6

时间:2016-07-19 12:31:42

标签: python-2.7 ipv6 pysnmp

我尝试使用pysnmp启动SNMP代理。 对于IPv4和IPv6绑定,它对localhost('127.0.0.1'和':: 1')非常好用

但是当我尝试使用从界面获取的其他IPv6 IP时,由于

而失败
[vagrant@test SOURCES]$ sudo python snmp_agent.py enp0s8
Traceback (most recent call last):
  File "snmp_agent.py", line 172, in <module>
    master_agent_startup(ifname=sys.argv[1])
  File "snmp_agent.py", line 101, in master_agent_startup
    (get_ipv6_address(interface_name), SNMP_AGENT_PORT))
  File "/usr/lib/python2.7/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 50, in openServerMode
    raise error.CarrierError('bind() for %s failed: %s' % (iface, sys.exc_info()[1],))
pysnmp.carrier.error.CarrierError: bind() for ('fe80::a00:27ff:fe9e:9c16', 8001) failed: [Errno 22] Invalid argument

这是接口'enp0s8'的输出:

[vagrant@test SOURCES]$ ifconfig enp0s8
enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.20.26  netmask 255.255.255.0  broadcast 172.20.20.255
        inet6 fe80::a00:27ff:fe9e:9c16  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:9e:9c:16  txqueuelen 1000  (Ethernet)
        RX packets 874053  bytes 115842841 (110.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 862314  bytes 114652475 (109.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

这是我用于IPv6绑定的代码:

def get_ipv6_address(ifname):
    return netifaces.ifaddresses(ifname)[netifaces.AF_INET6][0]['addr'].split('%')[0]

config.addSocketTransport(snmpEngine, udp.domainName,
                          udp.UdpTransport().openServerMode(
                              (get_ipv4_address(interface_name), SNMP_AGENT_PORT))
                          )
config.addSocketTransport(snmpEngine, udp6.domainName,
                          udp6.Udp6SocketTransport().openServerMode(
                              (get_ipv6_address(interface_name), SNMP_AGENT_PORT))
                          )

从pysnmp示例中,似乎“openServerMode()”中的参数只是IP和端口的元组。 从输出错误来看,我认为给定的IP和端口没有错误。 那么为什么它由于无效参数而失败? 你可以@Ilya Etingof或其他一些pysnmp专家帮助我吗?

感谢。

  

更新:我尝试将其与给定的建议绑定,但仍然无法正常工作。   bind命令是从新安装的CentOS运行的。但它仍然失败:       [root @ test~] #ifconfig

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.20.4  netmask 255.255.255.0  broadcast 10.10.20.255
        inet6 fe80::f816:3eff:fee1:5475  prefixlen 64  scopeid 0x20<link>
        ether fa:16:3e:e1:54:75  txqueuelen 1000  (Ethernet)
        RX packets 12242  bytes 962552 (939.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12196  bytes 957826 (935.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@test ~]# python
Python 2.7.5 (default, Oct 11 2015, 17:47:16)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, 0)
>>> addr_and_port = ('fe80::f816:3eff:fedb:ba4f', 8001)
>>> s.bind(addr_and_port)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 22] Invalid argument
>>>
[1]+  Stopped                 python
[root@test ~]# netstat -anp | grep 8001
[root@test ~]#
  

还有一个更新:   我认为绑定失败是由于我的环境在IPv6配置方面存在一些问题。因为我只能通过使用“socket.getaddrinfo()”方法获得一个IPv4地址。

BR, -Dapeng Jiao

2 个答案:

答案 0 :(得分:0)

如果尝试多次绑定同一个套接字,则可能会出现此错误。但我不能在你的代码中看到这种情况。

.openServerMode()方法毫无魔力 - 它只是在套接字对象上调用.bind()。有关灵感,这是否适用于您的Python提示符?

from pysnmp.carrier.asyncore.dgram import udp6

addr_and_port = ('fe80::a00:27ff:fe9e:9c16', 8001)
udp6.Udp6SocketTransport().openServerMode(addr_and_port)

甚至:

import socket

s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, 0)
addr_and_port = ('fe80::a00:27ff:fe9e:9c16', 8001)
s.bind(addr_and_port)

我希望像这样的测试可以帮助你解决问题......

答案 1 :(得分:0)

使用IPv6链接本地地址you must always use the scope along with it时。没有范围,链接本地地址无效,因此您将收到Invalid argument错误。

例如,您必须使用fe80::a00:27ff:fe9e:9c16

,而不是使用fe80::a00:27ff:fe9e:9c16%enp0s8