Python – Script1: Configure Network Device via Telnet

By creating telnet script on ‘Network Automation’ appliance using Python 3, we will telnet to router and loopback interface 0 with IP Address

Step1: Go to Network Automation Appliance. Make sure you are able to ping Router.
Step2: Configure router with username Rachit password cisco and enable password cisco. Allow transport telnet and login local on vty line.
Step3: Create python script using nano editor by command - nano Else we have to import python script and before executing script we have to make it executable by command "chmod +x <filename>“.
Step4: Add below lines 
import getpass
import sys
import telnetlib
HOST = raw_input(“Enter Device IP: “)
user = raw_input(“Enter your telnet username:”)
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until(“Username: “)
tn.write(user + “\n”)
if password:
    tn.read_until(“Password: “)
    tn.write(password + “\n”)
tn.write(“conf t\n”)
tn.write(“int loop 0\n”)
tn.write(“ip address\n”)
print tn.read_all()
Step5: Save the python script by Command – Ctrl+X, then Y to Confirm.
Step6: Check your script by command – cat
Step7: Run script by command – python3
Picture2 Python - Script1: Configure Network Device via Telnet
Python - Script1: Configure Network Device via Telnet 3
Step8: Check on router, you will see the loopback0 is created with IP Address

Network Architect | CCIEx3 #29824 JNCIE #2197 VCIX-NV

Leave a Comment