Enhance your Career in Networking With IPinBits!!!​

Network automation using telnet library

First thing to do when automating a networking device (Router/Switch) will be to access the device. Now we have multiple choices for accessing device such as telnet and ssh. 
The only difference between telnet and SSH is the security. 

??SSH is secure while telnet is not.??

So today we will checkout the python library “TELNET”

import getpass     ==> This will be used to prompt the password
import sys
import telnetlib   ==> This is the library we will use to login in remote device

HOST = "localhost"   ==> Here the HOST will be the IP of your router
user = raw_input("Enter your Username: ")   ==> This is for the VTY username
password = getpass.getpass()  ==> Here we will prompt user for password and store it in variable called password

tn = telnetlib.Telnet(HOST)  ==> We made a object "tn" and called TELNET library function to login the HOST

tn.read_until("Username: ") ==> Here username will be variable User which we saved earlier
tn.write(user + "\n")  ==> it is similar to writing username and press ENTER
if password:
    tn.read_until("Password: ") ==> It will Access the variable password.
    tn.write(password + "\n")  ==>it is similar to writing username and press ENTER


tn.write("en\n")
tn.write("Conf t\n")
 

Related blog posts