Skip to content

Instantly share code, notes, and snippets.

@ashiqopu
Last active February 22, 2020 21:45
Show Gist options
  • Save ashiqopu/e57e62dc169cb4ad99fed9677d022632 to your computer and use it in GitHub Desktop.
Save ashiqopu/e57e62dc169cb4ad99fed9677d022632 to your computer and use it in GitHub Desktop.
Python_telnet_script_file_io

This python (2.7) script reads credentials and input from input.txt file for telnet test and generates the telnet output to output.txt

make sure to modify the input.txt file accordingly for proper _ip-address_, _user_ and _password_.

_ip-address_
_user_
_password_
ls
exit
import getpass
import sys
import telnetlib
infile = open("input.txt","r")
outfile = open("output.txt","w")
HOST = infile.readline()
user = infile.readline()
password = infile.readline()
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
while True:
line = infile.readline()
if not line:
break
tn.write(line)
outfile.write(tn.read_all().decode('ascii'))
infile.close()
outfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment