Detect rogue in Cisco switches with Python

Jamal Shahverdiev
2 min readDec 5, 2022

--

With this Python code files, we will compare statically defined MAC address list with MAC addresses of selected Cisco switches for special VlanID. Statically defined MAC address list you must write in the StaticMacs file. If script couldn’t find statically defined StaticMacs file it will print you how you can prepare this with createstaticmacs.py script. switchnotificator.py script needs 3 arguments (switchusername, switchpassword and vlanID). If you execute script without needed arguments it will prompt information about this.

Note: I did refactor all code files from Python2 to Python3 and I don’t have real environment to test all these stuff. I hope everything will work as it was in Python2 and sorry if it is not working :(.

Script connects to selected switches and get MAC address list. If script cannot find one of MAC addresses from Cisco switches in the statically defined MAC address list file, it will send email immediately to the Network engineer(defined in the lib/variables.py file).

Script reads iplist file to get IP address of Cisco switches. That is why you must write IP address list of Cisco switches to this file.

To configure gmail settings just edit frommail, fromemailpass, tomail variables in the lib/variables.py file.

Execute the following commands to download and use code files:

# git clone https://github.com/jamalshahverdiev/python-general-codes.git
# cd python-general-codes/switch-notificator
# ./switchnotificator.py switchusername 'switch_long_password' vlanID

If you want use this script automatically every minute, just add the following line to your crontab file:

* * * * * /path/to/switchnotificator.py switchusername 'switch_long_password' vlanID

--

--