Question

I am using the following code:

import pysnmp
from pysnmp.entity.rfc3413.oneliner import cmdgen


errorIndication, errorStatus, errorIndex, \
           varBindTable = cmdGen.nextCmd (
                          cmdgen.CommunityData(agent, community_string),
                          cmdgen.UdpTransportTarget ( (ip, port) ),
                          (str(oid))
            )

where ip is the ipv4 address. How can I use a ipv6 address. I have read that pysnmp supports ipv6 as well. I dont know how to use the addresses here.

Thanks.

Was it helpful?

Solution

A little tweaking is required to make use of IPv6 with the "oneline" interface:

from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.carrier.asynsock.dgram import udp6
import socket

class Udp6TransportTarget(cmdgen.UdpTransportTarget):
    transportDomain = udp6.domainName

    def __init__(self, transportAddr, timeout=1, retries=5):
        self.transportAddr = (
            socket.getaddrinfo(transportAddr[0], transportAddr[1],
                               socket.AF_INET6,
                               socket.SOCK_DGRAM,
                               socket.IPPROTO_UDP)[0][4]
            )
        self.timeout = timeout
        self.retries = retries

    def openClientMode(self):
        self.transport = udp6.Udp6SocketTransport().openClientMode()
        return self.transport

print cmdgen.CommandGenerator().getCmd(
        cmdgen.CommunityData('public'),
        Udp6TransportTarget(('::1', 161)),
        '1.3.6.1.2.1.1.1.0')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top