【发布时间】:2015-12-03 19:47:15
【问题描述】:
我是python新手。我正在尝试连接到telnet,然后使用用户名和密码登录并执行命令并一一grep结果。我编码如下:
print ("Starting Client...")
host = "<myIp>"
timeout = 120
user = "myUserName"
password = "myPassword"
print ("Connecting...")
try:
session = telnetlib.Telnet(host, 23, timeout)
session.read_until("USERNAME : ",2)
session.write(user + "\n")
print("UserName entered")
response = session.read_until("PASSWORD : ",2)
print(str(response))
session.write(password + "\n")
print("password Entered")
except Exception,e:
print ("socket timeout")
else:
print("Sending Commands...")
session.write("Mycommand".encode('ascii') + b"\r")
print("Reading...")
output = session.read_until("commandBash>", timeout )
session.close()
print(output)
print("Done")
对于上述程序,我的输出为:
Starting Client...
Connecting...
UserName entered
myUserName
password Entered
Sending Commands...
Reading...
myPasswordMycommand
PASSWORD:
Done
由此我可以知道,当它要求输入密码时,它甚至没有输入密码。它实际上是在提示密码之前输入密码和命令。所以我什至尝试在输入用户名后输入 time.sleep(20)。仍然没有用。我无法弄清楚我在这里缺少什么。请帮助我的朋友
【问题讨论】:
标签: python python-2.7 automation telnet telnetlib