Question

I'm trying to do some telnet automation with Python (only pure Python). When I try to print some of my read's in the function read_until, all I see are a series of bs's -- that's bs, as in the backspace character, not something else. :-)

Does anyone know if there's some kind of setting I can change in the on tn, my instance of the Telnet class, or correct this? Or is this something that my host is spewing back? I've done some Googling on the telnetlib library, and I haven't seen many examples where folks have output from the Telnet.read_until function.

This is a cut-down version of my code:

import getpass
import sys
import telnetlib

HOST = "blahblah"

def writeline(data):
    local.write(data + '\n')

def read_until(tn, cue, timeout=2):
    print 'Before read until "%s"' % cue
    print tn.read_until(cue, timeout)
    print 'After read until "%s"' % cue

def tn_write(tn, text, msg=''):
    if not msg:
        msg = text
    print 'Before writing "%s"' % msg
    tn.write(text)
    print 'After writing "%s"' % msg    


user = 'me'
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

read_until(tn, "Username: ")
tn_write(tn, user + "\n", user)

if password:
    read_until(tn, "Password: ")
    tn_write(tn, password + "\n", 'password')

read_until(tn, "continue")
tn_write(tn, "\n", '<Enter>')

#tn.write("vt100\n")

tn_write(tn, 'main_menu' + '\n', 'start menu')
read_until(tn, 'Selection: ')

I don't think it matters, but I'm using Python 2.7 on Windows.

Was it helpful?

Solution

I figured out the problem here. I tried writing my commands with both \n and \r but not both combined. When I changed to '\r\n', I got the expected output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top