Python:使用pexpect通过脚本获取Telnet密码

时间:2013-05-06 10:22:48

标签: python telnet pexpect

我在使用pexpect运行我的telnet脚本时遇到问题。问题是它只使用脚本中的用户名而不是密码。它取密码的值,但不输入相同的内容。我的脚本下面有一个,

import pexpect
import sys,time
ipaddr = "192.168.100.85"
username = "usr"
password = "Pass@123"
telconn = pexpect.spawn("telnet " + ipaddr)
telconn.expect(":")
telconn.logfile=sys.stdout
time.sleep(15)
telconn.sendline(username + "\r")
telconn.expect(":")
telconn.sendline(password + "\r")
time.sleep(30)
telconn.expect(">")
print "Authentication Sucesss"

输出,

Trying 192.168.100.85...
Connected to 192.168.100.85.
Escape character is '^]'.
Welcome to Microsoft Telnet Service 


login: usr

password: Pass@123

The operation completed successfully.

Login Failed

1 个答案:

答案 0 :(得分:3)

我得到了解决方案,

import pexpect
import time,sys
telconn = pexpect.spawn('telnet 192.168.100.85')
time.sleep(20)
telconn.logfile = sys.stdout
telconn.expect(":")
time.sleep(20)
telconn.send("usr" + "\r")
telconn.expect(":")
telconn.send("Pass@123" + "\r")
telconn.send("\r\n")
time.sleep(20)
telconn.expect(">")

这对我有用