Automate Network Switch Configuration with Python and Paramiko

Introduction:

Have you ever wished you could manage your network switches with the click of a button? Well, Python can help! This post guides you through creating a Python script that connects to network switches, runs commands from a list, and saves the results. This automates tasks like configuration changes and backups, saving you time and effort.

Here's what you'll need:

  • Python 3: Download and install it from https://www.python.org/downloads/.
  • Text files:
    • myswitches.txt: List each switch's IP address on a separate line.
    • command.txt: List the commands you want to run on the switches, one per line.

For the Better output of commands always use "terminal lenght 0"

  • A folder: Create a folder named "commandlist" inside your Python directory (e.g., D:\\Python\\commandlist\) to store the output files.
  • Login credentials: If your switches don't use Tacacs+ or RADIUS, create a user account with the same username and password used in the script.

Python Full Code:

import paramiko
import time
import getpass
import datetime
import os.path
import os
from datetime import datetime
timestr = datetime.now().strftime("%d_%m_%Y--%H_%M")
file_name = ""+timestr
#Save output in D Drive
my_dir = "D:\\Python\\commandlist\"
fname = os.path.join(my_dir)
# Get Username and Password
username = "mehfooz"
password=getpass.getpass("Enter Password:")
# Open file with list of switches
f = open ("myswitches.txt")
# Telnet to each switch and cofigure it
for line in f:
ip_address = line.strip()
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)
print ("Successful connection", ip_address)
remote_connection = ssh_client.invoke_shell()
# Run command from the list (command.txt)
COMMANDS = open ('command.txt')
for LINES in COMMANDS:
#time.sleep(5)
remote_connection.send(LINES)
time.sleep(5)
readoutput = remote_connection.recv(655350)
saveoutput = open(fname + file_name + ip_address, "wb")
saveoutput.write(readoutput)
#saveoutput.write("\n") print ("Successful configured")
saveoutput.close
ssh_client.close

Breaking Down the Script:

The script uses Python libraries to connect to the switches and run commands. Here's a simplified explanation of what each part does:

  1. Imports: These lines bring in tools like paramiko (for secure connections) and time (for short pauses).
  2. Setting Up File Names: This section creates unique filenames for the saved output based on the date and time.
  3. Username and Password: Here, you define the username for connecting to the switches (replace with your actual username). The script then asks you to enter the password securely.
  4. Processing Switches: The script opens the myswitches.txt file and loops through each switch IP address listed.
  5. Connecting to a Switch: For each IP:
    • It connects to the switch using its IP address, username, and password.
    • A success message is displayed if the connection is established.
  6. Running Commands: The script opens the command.txt file and sends each command listed there to the switch, one by one. It then waits a few seconds (adjustable) for the switch to respond.
  7. Collecting and Saving Output:
    • The script gathers the switch's response to the commands.
    • It creates a filename that includes the date, time, and switch IP address.
    • The switch's response is saved to a file in the "commandlist" folder.
    • A message confirms successful configuration (you can customize this message).
  8. Closing the Connection: Finally, the script disconnects from the switch.

Conclusion:

This Python script equips you to automate switch management tasks, saving valuable time and effort. With its ability to connect to switches, execute commands from a list, and save the output, the script streamlines network configuration changes and backups.

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.