Python for network engineer:- Telnet to Cisco router using Python

Below script can be used to telnet Cisco router using Python script and can capture show version output.


import telnetlib # –<< importing telnet library

user = “test”   #–<< username configured in router
password = “test” #–<<Password configured in router


HOST = “192.168.1.100” #–<<IP
address of the router

telnet = telnetlib.Telnet(HOST)

telnet.read_until(“Username: “) # –<<Reading username from the router CLI screen
telnet.write(user + “n”) #–<< Configure username which is configured above

telnet.read_until(“Password: “) #–<<Reading Password from the router CLI screen
telnet.write(password + “n”) #–<< Configure password which is configured above

telnet.write(“terminal length 0n”) #–<< Configure command to capture the complete
output
telnet.write(“show version n”)  #–<<configure  show run

telnet.write(“exitn”)  # –<<exit
from the router



print telnet.read_all()  #-<<Read output from above show
version command