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:
- Imports: These lines bring in tools like paramiko (for secure connections) and time (for short pauses).
- Setting Up File Names: This section creates unique filenames for the saved output based on the date and time.
- 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.
- Processing Switches: The script opens the myswitches.txt file and loops through each switch IP address listed.
- 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.
- 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.
- 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).
- 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.