Telnet client session by Python script

Telnet client session by Python script

python

 

#!/usr/bin/env python

import getpass
import sys
import telnetlib

def run_telnet_session():
host = raw_input(“Enter remote hostname e.g. localhost:”)
user = raw_input(“Enter your remote account: “)
password = getpass.getpass()
session = telnetlib.Telnet(host)

session.read_until(“login: “)
session.write(user + “\n”)
if password:
session.read_until(“Password: “)
session.write(password + “\n”)
session.write(“ls\n”)
session.write(“exit\n”)
print session.read_all()
if __name__ == ‘__main__’:
run_telnet_session()

 

root@ubuntu:~/python# python 7_1_telnet.py
Enter remote hostname e.g. localhost:localhost
Enter your remote account: khoa
Password:

Last login: Sun Sep 11 11:22:45 PDT 2016 from localhost on pts/20
ls
exit
Welcome to Ubuntu Xenial Xerus (development branch) (GNU/Linux 4.4.0-36-generic x86_64)

* Documentation: https://help.ubuntu.com/

330 packages can be updated.
0 updates are security updates.

khoa@ubuntu:~$ ls
Desktop Downloads Music Public Templates
Documents examples.desktop Pictures readme.txt Videos
khoa@ubuntu:~$ exit
logout

root@ubuntu:~/python#

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>