我是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)。但是没有用。我无法弄清楚我在这里缺少什么。请帮助我的朋友
答案 0 :(得分:1)
找出我所缺少的东西。这是一件非常小的事情。我错过了这个代码来做&#34; ENTER&#34;每个session.write下面的opereation。我错过的代码是session.write(&#34; \ r&#34;)其中&#34; \ r&#34;表示进入操作