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.
“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 :
http://www.python.org/
www.tutorialspoint.com/python