您好我是一名尝试在Python中进行日常活动的人,以便熟悉新语言并将Python与tcl进行比较。没有人想要tcl程序了:))
好的,所以我已经提供了一个代码来自动执行这个典型的活动,我会去清除一个控制台行。
$ telnet 172.28.247.240
Trying 172.28.247.240...
Escape character is '^]'.
User Access Verification
Password:
labc-f18-ts>en
Password:
labc-f18-ts#clear line 66
[confirm]
[OK]
我的代码如下:
import getpass
import sys
import telnetlib
a_tuple = [ ('172.28.247.240' , 66)]
HOST = j[0]
user = "admin"
password = "1234"
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
tn.read_until("labc-f18-ts>")
tn.write ("en" +"\n")
tn.read_until("labc-f18-ts#")
tn.write("clear line %d" %j[1])
tn.write("exit\n")
sess_op = tn.read_all()
print sess_op
但我似乎没有得到任何输出,也不知道它是否真的清除了线 - 没有任何输出。请帮忙。
还有类似pexpect
的东西,我应该与之合作吗?
答案 0 :(得分:0)
import telnetlib
# ...
a_tuple = [('172.28.247.240', 66)]
HOST = a_tuple[0] # do you mean to use 'a_tuple' instead of 'j'?
# ...
tn = telnetlib.Telnet(HOST[0], HOST[1]) # <-- pass the port, I don't think this accepts tuples of hostname/ip and port
# ...
您的部分代码应该如上所示吗?
另外,在发送命令时,我通常会使用回车符(\ r)而不是换行符(\ n)。一些主持人对此很挑剔。