Skip to content

Instantly share code, notes, and snippets.

@almorel
Created August 25, 2012 13:07
Show Gist options
  • Save almorel/3465395 to your computer and use it in GitHub Desktop.
Save almorel/3465395 to your computer and use it in GitHub Desktop.
List Cam Table of Juniper EX4200
#!/usr/bin/env python
from pysnmp.entity.rfc3413.oneliner import cmdgen
import re
import struct
import getopt
import sys
import signal
OID_BRIDGE=(1,3,6,1,2,1,17,1,4,1,2)
OID_ALIAS=(1,3,6,1,2,1,31,1,1,1,1)
OID_DESC=(1,3,6,1,2,1,31,1,1,1,18)
SNMP_AGENT=""
SNMP_COMMUNITY="public"
def signal_handler(signal, frame):
sys.exit(0)
def is_integer(i):
try:
int(i)
return True
except ValueError:
return False
def get_value_of_oid(oid, index):
theOid=list(oid)
theOid.append(index)
errorIndication, errorStatus, \
errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(
cmdgen.CommunityData('test-agent', SNMP_COMMUNITY),
cmdgen.UdpTransportTarget((SNMP_AGENT, 161)),
tuple(theOid)
)
if errorIndication:
print "ERROR",errorIndication
else:
if errorStatus:
print '%s ERROR at %s\n' % (
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex)-1] or '?'
)
else:
for name, val in varBinds:
return val.prettyPrint()
def list_cam():
errorIndication, errorStatus, errorIndex, varBindTable = cmdgen.CommandGenerator().nextCmd(cmdgen.CommunityData('test-agent', SNMP_COMMUNITY),cmdgen.UdpTransportTarget((SNMP_AGENT, 161)),(1,3,6,1,2,1,17,7,1,2,2,1,2))
if errorIndication:
print errorIndication
else:
if errorStatus:
print '%s at %s\n' % (
errorStatus.prettyPrint(),
errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
)
else:
for varBindTableRow in varBindTable:
for name, val in varBindTableRow:
tableau=re.findall("\d+", name.prettyPrint())[::-1]
mac=(tableau[5],tableau[4],tableau[3],tableau[2],tableau[1],tableau[0])
myBridge=get_value_of_oid(OID_BRIDGE,int(val.prettyPrint()))
if (is_integer(myBridge)):
ifAlias=get_value_of_oid(OID_ALIAS,int(myBridge))
ifDescription=get_value_of_oid(OID_DESC,int(myBridge))
print "%0.2x:%0.2x:%0.2x:%0.2x:%0.2x:%0.2x -> %s | %s" % (int(mac[0]), int(mac[1]), int(mac[2]), int(mac[3]), int(mac[4]), int(mac[5]), ifAlias, ifDescription)
def main():
global SNMP_AGENT
global SNMP_COMMUNITY
try:
opts, args = getopt.getopt(sys.argv[1:], 'h:c:', ["host", "community"])
except getopt.GetoptError, err:
print str(err)
sys.exit(1)
for opt, arg in opts:
if opt in ("-h", "--host"):
SNMP_AGENT = arg
print "Host:\t",arg
elif opt in ("-c", "--community"): SNMP_COMMUNITY = arg
if len(SNMP_AGENT)!=0:
list_cam()
else:
print "Please define the host with -h or --host"
sys.exit(1)
if (__name__ == "__main__"):
main()
@srooprai
Copy link

srooprai commented Sep 7, 2016

Hello
I am a total noob when it comes to this. Can someone please answer the question if i use this script from a linux server to a switch ex 4200
What in this script needs to be changed to applied to the switch ? Like IP, SNMP version ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment