2012-10-11 93 views
0

我想用Python做一些telnet自動化(只有純Python)。當我嘗試在函數read_until中打印一些閱讀內容時,我看到的是一系列bs的 - 即bs,如backspace中的字符,而不是其他內容。 :-)Python telnetlib只讀打印「bs」

有沒有人知道是否有某種設置我可以改變在tn,我的Telnet類的實例,或更正此?或者這東西是我的主人正在噴涌而回?我在telnetlib庫上做了一些Google搜索,我還沒有看到很多例子,其中人們從Telnet.read_until函數輸出。

這是我的代碼簡化版本:

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: ') 

,我不認爲它很重要,但我使用Python 2.7在Windows上。

回答

1

我想通了這裏的問題。我試圖用\n\r編寫我的命令,但不是兩者結合。當我更改爲'\r\n'時,我得到了預期的輸出。