Skip to content

Instantly share code, notes, and snippets.

@costastf
Created September 21, 2017 07:23
Show Gist options
  • Save costastf/968bc0e162f0795889fd78a7d1862d01 to your computer and use it in GitHub Desktop.
Save costastf/968bc0e162f0795889fd78a7d1862d01 to your computer and use it in GitHub Desktop.
Legacy script checking switch port status via snmp
from pysnmp.entity.rfc3413.oneliner import cmdgen
#################################################################################################
# Global Settings
deviceIPAddress = "10.12.250.1"
snmpPort = 161
snmp_username = "monitoring"
# Port Status Displayed Information
statusID = {1: "Disabled",
2: "Blocking",
3: "Listening",
4: "Learning",
5: "Forwarding",
6: "Broken"}
#################################################################################################
def get_if_names():
interface_names = {}
username = '%s@%s' % (snmp_username, 1)
error_indication, \
error_status, \
error_index, \
var_binds = cmdgen.CommandGenerator().nextCmd(
cmdgen.CommunityData('agent', username, 0),
cmdgen.UdpTransportTarget((deviceIPAddress, snmpPort)),
(1, 3, 6, 1, 2, 1, 31, 1, 1, 1, 1))
for row in var_binds:
interface_index = str(row[0][0]).split('.')[-1]
port, _, number = str(row[0][1]).rpartition('/')
name = port + '/' + number.zfill(2)
interface_names[interface_index] = name
return interface_names
def get_if_index(vlan, snmp_username):
interface_index = {}
username = '%s@%s' % (snmp_username, vlan)
error_indication, error_status, error_index, var_binds = cmdgen.CommandGenerator().nextCmd(
cmdgen.CommunityData('agent', username, 0),
cmdgen.UdpTransportTarget((deviceIPAddress, snmpPort)),
(1, 3, 6, 1, 2, 1, 17, 1, 4, 1, 2))
for row in var_binds:
number = str(row[0][0])
index = str(row[0][1])
interface_index[number] = index
return interface_index
def get_bridge_ports(vlan, snmp_username):
bridge_port = {}
username = '%s@%s' % (snmp_username, vlan)
error_indication, error_status, error_index, var_binds = cmdgen.CommandGenerator().nextCmd(
cmdgen.CommunityData('agent', username, 0),
cmdgen.UdpTransportTarget((deviceIPAddress, snmpPort)),
(1, 3, 6, 1, 2, 1, 17, 4, 3, 1, 2))
for row in var_binds:
name = str(row[0][0])
port = str(row[0][1])
bridgePort[name] = port
return bridgePort
def get_macs(vlan, snmp_username):
macs = {}
username = '%s@%s' % (snmp_username, vlan)
error_indication, error_status, error_index, var_binds = cmdgen.CommandGenerator().nextCmd(
cmdgen.CommunityData('agent', username, 0),
cmdgen.UdpTransportTarget((deviceIPAddress, snmpPort)),
(1, 3, 6, 1, 2, 1, 17, 4, 3, 1, 1))
for row in var_binds:
name \
= '1.3.6.1.2.1.17.4.3.1.2.' + '.'.join(
str(row[0][0]).split('.')[-6:])
mac = row[0][1]
mac = ':'.join(['%02x' % ord(x) for x in mac])
macs[mac] = name
return macs
def get_arp():
ips = {}
username = snmp_username
error_indication, error_status, error_index, var_binds = cmdgen.CommandGenerator().nextCmd(
cmdgen.CommunityData('agent', username, 0),
cmdgen.UdpTransportTarget((deviceIPAddress, snmpPort)),
(1, 3, 6, 1, 2, 1, 3, 1, 1, 2))
for row in var_binds:
ip = '.'.join(str(row[0][0]).split('.')[-4:])
mac = row[0][1]
mac = ':'.join(['%02x' % ord(x) for x in mac])
ips[mac] = ip
return ips
# Retrieve VLAN Table
error_indication, error_status, error_index, var_binds = cmdgen.CommandGenerator().nextCmd(
cmdgen.CommunityData('agent', snmp_username, 0),
cmdgen.UdpTransportTarget((deviceIPAddress, snmpPort)),
(1, 3, 6, 1, 4, 1, 9, 9, 46, 1, 3, 1, 1, 2))
# Store VLAN Table
vlans = []
for row in var_binds:
for item in row:
vlans.append(item[0][15])
switch = {}
ips = get_arp()
interface_names = get_if_names()
for vlan in vlans:
interface_index = get_if_index(vlan, snmp_username)
bridge_ports = get_bridge_ports(vlan, snmp_username)
macs = get_macs(vlan, snmp_username)
for mac, bridgePort in macs.iteritems():
switch[interface_names[
interface_index['1.3.6.1.2.1.17.1.4.1.2.' + bridge_ports[bridgePort]]]] = (
mac, vlan)
for port in sorted(switch):
try:
print "Port {0} has connected mac :{1} with ip : {2} on vlan {3}".format(
port, switch[port][0], ips[switch[port][0]], switch[port][1])
except KeyError:
print "Port {0} has connected mac :{1} on vlan {2}".format(port,
switch[port][
0],
switch[port][
1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment