Python Telnet Library Network Automation














































Python Telnet Library Network Automation



PYTHON TELNET LIBRARY NETWORK AUTOMATION.


In the last article we were introduced to the Python Telnet library. We learnt about the
telnetlib library, What is Telnet? , and how to implement Telnet using telnetlib library.
We also saw an example of how to implement and use Telnet library in our program.

In this article we will learn about how to use Telnet library in Networking Automation with
an example. Also, we will discuss each step in detail.

What is Network Automation ? :
Network automation is the process of automating the planning, deployment, operations and optimization
of networks and their services. At a basic level, network automation solutions transition the manual tasks
and processes performed in each stage of the network lifecycle to software applications that are able to 
complete them repeatably and reliably.

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.

-> Import Telnet Library :
The first step is to import telnet library to our script , use the following command.


->Connecting a Host :
To connect a device using telnetlib, use following command.


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 Host :


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 :


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:

Step 1. Importing the required modules :

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

Step 2. Initialize the Host Variable :
The "Host" variable holding the IP address of Device . We can assign the IP address as follows.
       
 
Step 3. Read the user name and password :


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 :



Step 5. Configure the device :
In this step, we will be delivering configuration command to device one by one.



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 .

Here's the final code :
       

Conclusion :

In this article, we learnt how the Telnet library can help in Network Automation and 
step by step guidelines to access the switch.
In the next article, we will learn READ METHODS of the Telnet Class.


Comments

  • swara
    26-Sep-2020 04:03:20 PM
    Good work
  • Yash
    25-Sep-2020 11:25:17 PM
    //Enter Your Comment Here...So good
  • Sushma
    19-Sep-2020 11:56:22 AM
    Thank you ankur for this helpul article%u2764%uFE0F
  • Suraj
    18-Sep-2020 09:24:28 PM
    Nice !