0

I'm very new to python scripting, but I thought I would give it a go to see if I can automate a few simple tasks I preform regularly over Telnet.

I found a basic python telnet script:

import getpass
import sys
import telnetlib

HOST = "localhost"
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()

This 'seems' to work, but returns nothing to the console. Since the login process takes a few seconds, I figure it's just going too fast & needs to wait for the user prompt. So I tried adding this.

tn.read_until(">")

It does not work... I get this error

  File "test.py", line 16, in <module>
    tn.read_until(">")
  File "/usr/lib/python2.7/telnetlib.py", line 319, in read_until
    return self.read_very_lazy()
  File "/usr/lib/python2.7/telnetlib.py", line 395, in read_very_lazy
    raise EOFError, 'telnet connection closed'
EOFError: telnet connection closed

I feel like I'm missing something simple... but the solution seems to be alluding me. Any input will be highly appreciated!

user2735454
  • 335
  • 1
  • 5
  • 14

2 Answers2

0

Check if this link - it may help you out. I guess you may want to specify

tn.write("vt100\n") 

before printing out anything.

Community
  • 1
  • 1
Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22
0

You may have to use another line separator depending on the platform. try \r or \r\n

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93