2

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.

ashokadhikari
  • 1,182
  • 3
  • 15
  • 29

1 Answers1

3

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')
Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
  • I tried the code and I got the results : **(, 0, 0, [])** When I try the code with **localhost** for **ipv4**, i get (None, Integer('noError'), Integer(0), [(ObjectName(1.3.6.1.2.1.1.1.0), OctetString('Darwin ashok.local 11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:47:41 PST 2012; root:xnu-1699.24.23~1/RELEASE_X86_64 x86_64'))]) i.e. it works. What is the problem? – ashokadhikari Jul 05 '12 at 07:29
  • 1
    Make sure your Agent is listening at [::1]:161. – Ilya Etingof Jul 05 '12 at 07:30
  • Sorry for messing up my comments, i tried with getnext as well, but i didnt get any results. – ashokadhikari Jul 05 '12 at 07:31
  • Assuming you query an Agent at localhost, the "netstat -an | grep 161" command should display ::1:161 – Ilya Etingof Jul 05 '12 at 07:37
  • Yes, the ::1:161 is not on the list. How is it that I make the agent listen at the port? – ashokadhikari Jul 05 '12 at 07:43
  • It depends of the Agent you use. If it's NET-SNMP's snmpd, see "man snmpd.conf" (agentaddress clause). A quick solution would be to add "udp:161 udp6:161" params to snmpd command line. – Ilya Etingof Jul 05 '12 at 07:52
  • Now my netstat -an | grep 161 shows : udp6 0 0 ::1.161 *.* udp4 0 0 *.161 *.* . But i still am not getting the results. What could be the problem? – ashokadhikari Jul 05 '12 at 08:52
  • Try to query snmpd with stock snmpwalk over ipv6 to rule out pysnmp issues and make sure snmpd config is correct. Another approach would be to enable pysnmp debug to see what is going on behind the hood /from pysnmp import debug; debug.setLogger(debug.Debug('all'))/ – Ilya Etingof Jul 05 '12 at 21:45
  • This is what i get : http://hastebin.com/vovafocagi.sm I tried to do : snmpwalk -v 2c -c public ::1 1.3, i got, snmpwalk: Unknown host (::1) (Invalid argument) I have udp6 0 0 ::1.161 *.*, which must mean that the snmp config is correct isnt it. – ashokadhikari Jul 06 '12 at 06:14
  • snmpwalk -c public -v2c udp6:[::1]:161 .1.3.6 works for me. According to snmpd.conf man page, there should be a separate configuration clause "rocommunity6" for IPv6 access. – Ilya Etingof Jul 06 '12 at 07:00
  • As for pysnmp exception whilst debugging, that is a known bug. Please upgrade to the latest release candidate from pysnmp SourceForge page. However your problem seems to be with snmpd configuration at the moment. – Ilya Etingof Jul 06 '12 at 07:02
  • Yes I had missed the r?community6 clause in the configuration file. It worked. Thank you very much :) – ashokadhikari Jul 06 '12 at 07:50