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 1.1.1.1 255.255.255.255

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 python31.py. 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(“enable\n”)
tn.write(“cisco\n”)
tn.write(“conf t\n”)
tn.write(“int loop 0\n”)
tn.write(“ip address 1.1.1.1 255.255.255.255\n”)
tn.write(“end\n”)
tn.write(“exit\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 python31.py
Step7: Run script by command – python3 python31.py
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 1.1.1.1/32

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

Leave a Comment