Modify port Vlan ID with PySnmp

  • 0
  • 1
  • Question
  • Updated 12 months ago
  • Answered
  • (Edited)
Hi, im trying to modify the vlan id of port 24 on slot 2 (2:24) which as the ifindex of 152  according to snmpGet. I have been reading the documentation of PySnmp and from my point of view it is a bit vague, probably because i dont have yet a deep knowledge of programming and also on how SNMP is structured.

Using the script provided from pysnmp documentation im unable to configure port 2:24 vlan ID. Im getting the following error:

pysnmp.smi.error.SmiError: No symbol SNMPv2-SMI::mib-2.17.7.1.4.5.1.1 at <pysnmp.smi.builder.MibBuilder object at 0x000000000230FD68>
my code:

from pysnmp.hlapi import *
g = setCmd(SnmpEngine(),
CommunityData(MyComm),
UdpTransportTarget(MyIP, 161)),
ContextData(),
ObjectType(ObjectIdentity('SNMPv2-SMI', 'mib-2.17.7.1.4.5.1.1', 152), '201'))
next(g)

Can someone help me?
Photo of GONÇALO NUNO CONTENTE PIMENTEL DA SILVA REIS

Posted 12 months ago

  • 0
  • 1
Photo of OscarK

OscarK, ESE

  • 7,702 Points 5k badge 2x thumb
It seems pysnmp cannot find the mib, try using the oid. Below an example with a get.

def SNMPGet(oid,host,community):
    errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
        cmdgen.CommunityData(community),
        cmdgen.UdpTransportTarget((host, 161)),
        cmdgen.MibVariable(oid)
    )

    # Check for errors and print out results
    if errorIndication:
        print(errorIndication)
    else:
        if errorStatus:
            print('%s at %s' % (
                errorStatus.prettyPrint(),
                errorIndex and varBinds[int(errorIndex)-1] or '?'
            )
        )
        else:
            for name, val in varBinds:
                return val.prettyPrint()

portInOctets=SNMPGet('1.3.6.1.2.1.31.1.1.1.6.1001',iphost,args.community)
Hi, Oscar. Hope you are doing well, thanks for the reply. I was finally able to set  the vlan Id via pySNMP with a different piece of code.
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.proto import rfc1902
errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().setCmd(
    cmdgen.CommunityData('my-agent', My_Community, 1),
    cmdgen.UdpTransportTarget(('192.168.20.10', 161)),
    ((1,3,6,1,2,1,17,7,1,4,5,1,1,1), rfc1902.Gauge32('201'))
    )
print errorIndication
print errorStatus 
output form the switch:

* (pacman) EXOS-VM.36 # sh port 1 vid
         Untagged
Port     /Tagged   VID(s)
-------- --------  ------------------------------------------------------------
1        Untagged  201

Thanks for trying to help.
Photo of OscarK

OscarK, ESE

  • 7,702 Points 5k badge 2x thumb
Glad to help.