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上。