telnetlib python example

de1337ed picture de1337ed · Jun 8, 2012 · Viewed 87.8k times · Source

So I'm trying this really simple example given by the python docs:

import getpass
import sys
import telnetlib

HOST = "<HOST_IP>"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(user + "\n")
if password:
   tn.read_until("Password: ")
   tn.write(password + "\n")

tn.write("ls\n")
tn.write("exit\n")
print tn.read_all()

My issue is that it hangs at the end of the read_all()... It doesn't print anything out. I've never used this module before so I'm trying to get this really basic example to work before continuing. BTW, I'm using python 2.4 Thank you.

Answer

de1337ed picture de1337ed · Jun 8, 2012

Okay, I found a solution. Before I entered ls and exit, I needed to first specify the terminal type. Adding

tn.write("vt100\n") 

before the "ls" fixed the problem for me.