Instantly share code, notes, and snippets.

Embed
What would you like to do?
Commandline Tool to export current registered users at APs from an Cisco Wireless LAN Controller (WLC)
#!/usr/bin/env python
# Need following pip packages
# - easysnmp
# - tabulate
# Checkout blog article to tool
# https://lanbugs.de/netzwerktechnik/commandline-tool-to-export-current-registered-users-at-aps-from-an-cisco-wireless-lan-controller-wlc/
from easysnmp import Session
import argparse
from tabulate import tabulate
from operator import itemgetter
def main():
####
# ARGS
####
description = """
Cisco AP clients grabber\nVersion 0.1\nWritten by Maximilian Thoma 2018
"""
aparser = argparse.ArgumentParser(description=description)
aparser.add_argument('-H', dest='host', help='WLC IP address', required=True)
aparser.add_argument('-v', dest='snmp_version', help='SNMP version, valid are 2 or 3', required=True)
aparser.add_argument('-C', dest='snmp_community', help='SNMP Community (only v2)')
aparser.add_argument('-u', dest='snmp_user', help='SNMP user (v3)')
aparser.add_argument('-A', dest='snmp_auth', help='SNMP auth password (v3)')
aparser.add_argument('-a', dest='snmp_auth_method', help='SNMP auth method, valid are MD5 or SHA (v3)')
aparser.add_argument('-X', dest='snmp_privacy', help='SNMP privacy password (v3)')
aparser.add_argument('-x', dest='snmp_privacy_method', help='SNMP privacy method, valid are AES or DES (v3)')
aparser.add_argument('-L', dest='snmp_security',
help='SNMP security level, valid are no_auth_or_privacy, auth_without_privacy or auth_with_privacy (v3)')
aparser.add_argument('--csv', dest='csv', help='Result should be CSV', action='store_true')
args = aparser.parse_args()
####
# Setup SNMP connection
####
if args.snmp_version == "2":
try:
snmp = Session(hostname=args.host, version=2, use_numeric=True)
except Exception as e:
print e
if args.snmp_version == "3":
try:
snmp = Session(
hostname=args.host,
version=3,
security_level=args.snmp_security,
security_username=args.snmp_user,
auth_protocol=args.snmp_auth_method,
auth_password=args.snmp_auth,
privacy_protocol=args.snmp_privacy_method,
privacy_password=args.snmp_privacy,
use_numeric=True
)
except Exception as e:
print e
####
# Init Data Buffer
####
inventory = []
longids = []
####
# SNMP Walk Clients inventory
####
## Get longids for clients
result_longids = snmp.walk(".1.3.6.1.4.1.14179.2.1.4.1.1")
for rl in result_longids:
longids.append(rl.oid.replace(".1.3.6.1.4.1.14179.2.1.4.1.1.", "") + "." + rl.oid_index)
## Collect informations
for id in longids:
# Client MAC
result_mac = snmp.get(".1.3.6.1.4.1.14179.2.1.4.1.1." + id)
mac = ":".join(["%02s" % hex(ord(m))[2:] for m in result_mac.value]).replace(' ', '0').upper()
# Client Name
name = snmp.get(".1.3.6.1.4.1.14179.2.1.4.1.3." + id).value
# Client IP
ip = snmp.get(".1.3.6.1.4.1.14179.2.1.4.1.2." + id).value
# SSID
ssid = snmp.get(".1.3.6.1.4.1.14179.2.1.4.1.7." + id).value
# SSID MAC
result_ssid_mac = snmp.get(".1.3.6.1.4.1.14179.2.1.4.1.4." + id)
ssid_mac = ":".join(["%02s" % hex(ord(m))[2:] for m in result_ssid_mac.value]).replace(' ', '0').upper()
inventory.append([ip, mac, name, ssid, ssid_mac])
###
# Sort table
####
inv = sorted(inventory, key=itemgetter(0))
####
# Result
####
if args.csv is True:
print 'IP;MAC;Name;SSID;SSID MAC'
for ip, mac, name, ssid, ssid_mac in inv:
print '%s;%s;%s;%s;%s' % (ip, mac, name, ssid, ssid_mac)
else:
print tabulate(inv, headers=["IP", "MAC", "Name", "SSID", "SSID MAC"], tablefmt="orgtbl")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment