如何telnet到一个盒子并从字典中读/写用户名/密码;并识别某些提示

时间:2016-05-23 18:09:56

标签: python-2.7 python-3.x telnet

如何远程登录到一个框并让它回显我输入的内容以进行验证 我真的登录了??????????

telnet = telnetlib.Telnet()
host = "10.59.50.171"
newline = "\n"
telnet = telnetlib.Telnet(host, '23')
telnet.read_until("Username: ".encode())
telnet.write(username)
telnet.read_until("Password: ")
telnet.write(password)

user_acct = {'ADMIN':'PASSWORD','READONLY':'PASSWORD'}

if username in user_acct and password == user_acct[username]: 
    print('Correct Login')
else:
    print('Invalid Login... Please Try Again')

1 个答案:

答案 0 :(得分:0)

这是如何使用latest pexpect来控制telnet会话的示例:

<xsl:variable name="targetUrl" select="'sftp://username:password@host:22/~/Test.xml'"/>

您需要from pexpect import fdpexpect import pexpect import select import socket class SocketSpawn(fdpexpect.fdspawn): def read_nonblocking(self, size=1, timeout=-1): if timeout == -1: timeout = self.timeout rlist = [self.child_fd] wlist = [] xlist = [] rlist, wlist, xlist = select.select(rlist, wlist, xlist, timeout) if self.child_fd not in rlist: raise pexpect.exceptions.TIMEOUT('Timeout exceeded.') return super(fdpexpect.fdspawn, self).read_nonblocking(size) if __name__ == '__main__': username = 'username' password = 'password' host = '10.59.50.171' port = 23 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) session = SocketSpawn(s, timeout=10) session.expect_exact('Username: ') print session.before session.write("%s\n" % username) session.expect_exact('Password: ') print session.before session.write("%s\n" % password) try: session.expect_exact(pexpect.EOF) except pexpect.TIMEOUT: pass print session.before 类来修复最新版本的SocketSpawn中引入的缺陷。但是,这也适用于pexpect的旧版本。