Network Automation using Python – Part II – Telnet to a Switch and IP configuration
This is the 2nd post from my Networking Automation using Python blog series.
As part of network automation, the first thing is accessing (Telnet -ing) a switch. Here is a simple program explaining step by step way to access Cisco switch using telnet and to configure IP address on Vlan interface – all using Python.
Please check out our first post Network Automation using Python – Part I for getting started with Python. We have explained the basics of Python and the installation procedure in the previous post.
“telnetlib” module
“telnetlib” is the name of module which is supporting to take the telnet of device. This will be automatically installed as part of your python installation
->Import telnet library
The first step is to import telnet library to our script , use the following command
“import telnetlib”
->Connecting a Host
To connect a device using telnetlib, use following command.
tn=telnetlib.Telnet(HOST)
——————- advertisements ——————-
———————————————————-
Where HOST is the variable which is having the IP address of device and “tn” is the variable name which is creating virtual telnet link with your device. It could be any name as you wish ( like telnet or tnet) .You should use same name for the rest of the operation on the device.
-> Writing command to to Host
tn.write(“config t”)
The write() function using to deliver command to device. The above example will write “config t” command on the device telnet prompt.
-> Reading output from host
output=tn.read_all()
read_all() function will read the output of command from device and store in to the variable output
That concludes the basics for initiating a telnet session to the switch.
Following are the step by step guidelines to access the switch and then configure the IP on the switch. The steps explained below is based on 2.6 version for easy understanding. I have attached version 3.6 and 2.6 script also as there are changes in the script. The main difference in 3.6 as we need to convert all values in to ASCII before sending to device.
Step 1. Importing the required modules
import telnetlib
import getpass
import time
“getpass” is the module to read the password without printing on screen
“time” will be using to control the flow of program by pausing script certain duration
——————- advertisements ——————-
———————————————————-
Step 2. Initialise the Host Variable
The “Host” variable holding the IP address of Device . We can assign the IP address as follows. Please understa
HOST=”192.168.43.10″
Step 3. Read the user name and password
user = raw_input(“Enter your telnet username: “)
password = getpass.getpass()
raw_input() is an in built function used to read data giving by user and assigning to a variable. Here it will display “Enter your telnet username” on the screen and read the username provided by user and assign to variable called user. After executing those lines, we will have username on “user” variable and password on “password” variable.
Step 4. Connect to device and supply username and password
tn = telnetlib.Telnet(HOST)
tn.read_until(“Username: “)
tn.write(user + “\n”)
if password:
tn.read_until(“Password: “)
tn.write(password + “\n”)
tn = telnetlib.Telnet(Host) // This command will initiate a telnet session to the given IP address on background .
tn.read_until(b”Username: “) // This will be used to read output from device until it is asking for ‘Username’
tn.write(user + “\n”) // This will supply the username to the telnet console followed by enter key. “\n” using to provide enter key.
——————- advertisements ——————-
———————————————————-
Step 5. Configure the device
In this step, we will be delivering configuration command to device one by one
tn.write(“enable\n”) // changing to enable mode
tn.write(“cisco\n”) // providing enable password
tn.write(“conf t\n”) //moving to configuration mode
tn.write(“int vlan 10 \n”) // changing to vlan 10 interface
tn.write(“ip address 1.1.1.1 255.255.255.255\n”) // Assigning the IP address
tn.write(“end\n”) //ending the configuration
tn.write(“exit\n”)
We have delivered all commands using write() function. You can use same write function to deliver the command as per your requirement. Save and Execute the script using RUN . Please refer part 1 if you don’d know how to write and execute script.
Script download
You can download the script for version 2.6, here
You can download the script for version 3.6, here
So, that’s it. Hope this helped you. You can read more posts on Network automation using Python here. Please use the comments section for your queries/comments.
Reference :
Pingback: Network Automation using Python - Part IV - SSH to Cisco Device -
Pingback: Network Automation using Python – Part VI - Automatic backup of multiple switches -
Pingback: Network Automation using Python – Part V - running a set of commands on Cisco switches -
How can adapt this to do thousands of TELNET sessions (sequentially) with the IP addresses being read from one file and the commands to be issued being read from another? UserIDs/Passwords could either be supplied on an IP by IP basis in the IP address file. Or, if they all the same, it would be nicer to just prompt the user for the common UserID/Password once & apply it to every session. Additionally, a log file with the output of all sessions would be nice so you could sift through it after the run looking for errors.
THANKS.
Thanks for your question.
Please check our post “http://beginnersforum.net/blog/2017/09/27/network-automation-using-python-running-multiple-commands-cisco-switch/” which is already explain how to execute multiple commands on multiple switches. I hope this post help you with your requirement. Please do not hesitate to contact us if you need further help.
This paragraph has a link in it to an article that requires a WordPress login to view… Can you please fix this?
“Please check out our first post Network Automation using Python – Part I for getting started with Python. We have explained the basics of Python and the installation procedure in the previous post.”
Hi Cory,
Thanks for the comment and sincere apologies for the inconvenience caused. We have updated the link. Please let us know if you have any other queries/suggestions.
Regards,
Admin
Thanks..i am already having knowledge of python and networking (completed CCNP) but didn’t know how to apply it..now i got somewhat idea.
Thank you so much for the python3 program for telnet…. I was struggling a lot with the encodings!