Automating Cisco Switch Serial Number Extraction with Python

Introduction:

In network administration, keeping track of hardware serial numbers is essential for inventory management, support, and warranty purposes. Manually retrieving these serial numbers from multiple switches can be time-consuming. Fortunately, with Python and the Paramiko library, you can automate the extraction of serial numbers from Cisco switches, saving time and reducing the risk of errors. This post walks you through a Python script designed to ping switches, connect via SSH, and extract only the switch's serial number (excluding SFPs or other connected devices), storing the results in an Excel file.

Prerequisites:

Before running the script, ensure you have the following:

  1. Python Installed: Make sure Python is installed on your machine. You can download it from python.org.
  2. Paramiko Library: Paramiko is a Python library used for making SSH connections. Install it using pip:
    pip install paramiko
  3. Openpyxl Library: This library is used to create and manipulate Excel files. Install it using pip:
    pip install openpyxl
  4. Switch Access: Ensure you have SSH access to the Cisco switches, with the correct username and password. The account should have at least read-only access.
  5. IP Address List: Prepare a text file containing the IP addresses of the switches you want to query. Save it as Aug2024.txt in the same directory as the script.

Script Explanation:

  1. Ping the Switches: The check_switch function sends 4 ping packets to each switch to verify it's reachable before attempting to connect via SSH.
  2. Extract Serial Number: The extract_switch_serial function processes the output from the show inventory command, specifically extracting the serial number associated with the switch itself.
  3. Save to Excel: The save_to_excel function creates an Excel workbook, writes the IP addresses and serial numbers to it, and saves the file with a timestamp in its name.
  4. Main Function: This function orchestrates the entire process, reading IPs from Aug2024.txt, connecting to each switch, extracting the serial number, and saving the results to an Excel file.

Running the Script:

  1. Prepare the Environment: Ensure your Python environment has the necessary libraries installed and that your switches are reachable via SSH.
  2. Run the Script: Execute the script from your command line or IDE. You’ll be prompted for your SSH password. The script will then ping each switch, extract the serial number, and save the results in an Excel file.
  3. Check the Output: After the script completes, navigate to D:\Python\Backup\SerialNumbers (or your specified directory) to find the Excel file with the serial numbers.

Conclusion:

Automating the retrieval of serial numbers from Cisco switches not only saves time but also ensures accurate documentation. This Python script provides a robust solution for network administrators, especially when managing large-scale network environments. With minor modifications, this script can be adapted to suit various other network devices or different inventory tasks.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.