Network Automation using Python – Part III – Script to login to multiple switches
Script to login to multiple switches
So, here is our part-3 of the Networking Automation using Python blog series.
Here we will see how to login to multiple switches and execute a set of commands using Python Scripting. Please read the Part 1 and Part 2 before reading this article to know how to install Python and take the Telnet of single switch. The below script used to update SNMP string on multiple switches.
In this post, we will discuss a script which can be used to login to multiple switches and to configure SNMP string in them.
Create an IP address file.
The script will be taking the IP address from the file “ipfile.txt”. Create a text file with name ipfile.txt on the same folder where the script is saved with all the device IP address – line by line.
Creating Script
Following are the main steps involved in Script. Please read our previous post to get more info about step 1 and telnet lib
Step 1. Import modules
import getpass,sys,telnetlib,time
——————- advertisements ——————-
———————————————————-
Step 2. Getting credential
user=input(“Enter User name: “)
password=getpass.getpass()
Step 3. Open file where we have stored the IP address
iplist=open(“ipfile.txt”)
This will open a file called ipfile.txt and assign a symbolic link to file using “iplist” variable. The rest of operation we will do with “iplist” variable.
Step 4. For loop to read IP address from file
we will use for loop to read one by one IP address from file and execute the telnet session.
for line in iplist:
Host=line.strip(“\n”) // this is to strip the new line charecter
print (“Configuring Switch”,Host)
tn = telnetlib.Telnet(Host) // creating telnet session
tn.read_until(b”Username: “) // “b” used to convert ASCII value
tn.write(user.encode(‘ascii’) + b”\n”) // encode(ascii) using to convert provided user name in to ascii value and supply to device
tn.read_until(b”Password: “)
tn.write(password.encode(‘ascii’)+b”\n”)
time.sleep(2) // pausing the operation for two second
tn.write(b”enable\n”) //moving to enable mode
time.sleep(2)
tn.write(b”admin\n”) // “admin” is enable password
time.sleep(2)
——————- advertisements ——————-
———————————————————-
tn.write(b”config t\n”)
time.sleep(2)
tn.write(b”snmp-server community Secured ro\n”) // “Secured is the community string
print (“Switch “,Host,”Configured”)
line=tn.read_some()
tn.close() // closing the telnet session to the device
print (line) // printing all out put on screen
The above part of script will read first line (which is IP address) from file and assign to variable called “line”. So the variable “line” will have the IP address and later we will assign these IP address to “Host” variable. The command available under for loop will execute against the IP address . Once all the commands executed, for loop will read the next line available on the file and make the telnet session to that IP address and execute all the commands. These process will continue until the last IP address available on the file.
Step 5. Close file
iplist.close()
——————- advertisements ——————-
———————————————————-
Complete Script – download
You can download the complete script here. You can execute this script in Python version 3.6 and above. Please change the file extension from .txt to .py format before executing the downloaded script.
Hope you have enjoyed the reading. You can read more posts on Network automation using Python here.We value your suggestions and queries, please use the comments section below.
Reference :