I am no expert in python but just trying to share , something i used recently. its helpful if you want to telnet to IOS devices and get some output. using python scripts
i am using python version 2.7
you can download it from here
https://www.python.org/download/releases/2.7/
Script:
import os
import telnetlib
def telNetCall():
host = "xx.xx.xx.xx"
user = "username"
password = "password"
telnet = telnetlib.Telnet(host)
telnet.read_until('Username: ', 3)
telnet.write(user + '\r')
telnet.read_until('Password: ', 3)
telnet.write(password + '\r')
telnet.write('enable' + '\r\n')
telnet.write('enable_password' + '\r\n')
telnet.write('term len 0' + '\r\n')
telnet.write("show version"+ "\r\n")
telnet.write('exit' + '\r')
a=telnet.read_all()
f = open(host, 'w')
f.write(str(a))
telNetCall()
the script will telnet to the device and write the show version (or anything you define) into a file with the name as the host IP address.
f = open(host, 'w')
so lets run the script now for a small demo, but before that make sure you saved your script with proper indentation and file extension as .py
step1: in your command prompt change your directory to the python27 directory
i am using python version 2.7
you can download it from here
https://www.python.org/download/releases/2.7/
Script:
import os
import telnetlib
def telNetCall():
host = "xx.xx.xx.xx"
user = "username"
password = "password"
telnet = telnetlib.Telnet(host)
telnet.read_until('Username: ', 3)
telnet.write(user + '\r')
telnet.read_until('Password: ', 3)
telnet.write(password + '\r')
telnet.write('enable' + '\r\n')
telnet.write('enable_password' + '\r\n')
telnet.write('term len 0' + '\r\n')
telnet.write("show version"+ "\r\n")
telnet.write('exit' + '\r')
a=telnet.read_all()
f = open(host, 'w')
f.write(str(a))
telNetCall()
the script will telnet to the device and write the show version (or anything you define) into a file with the name as the host IP address.
f = open(host, 'w')
so lets run the script now for a small demo, but before that make sure you saved your script with proper indentation and file extension as .py
step2: execute the script by just typing the script file name with the extension as shown above
step3: done !!! your output file should be created in the python directory as show below with the file name as the host IP address
why do you sometimes use '\r' and sometimes use '\r\n'?
ReplyDeletenice, thanks!
ReplyDelete