pexpect:TypeError:“不支持的操作数类型为+:'float'和'str'”

时间:2016-09-03 11:48:39

标签: python-2.7 ssh pexpect

我对python不太熟悉。在“pexpect”示例回购中发现了这段代码。我做了必要的改变。但是,它正在抛出“TypeError”。无法弄清楚为什么。任何人都可以解释为什么以及如何解决这个问题?

“pdb”在“child = ssh_command(用户,主机,密码)”上发出错误

pdb错误:

TypeError: "unsupported operand type(s) for +: 'float' and 'str'"

代码如下,

import pexpect

def ssh_command(user, host, password):
    new_sshkey = 'Are you sure you want to continue connecting'
    child = pexpect.spawn('ssh %s@%s' %(user, host))
    i = child.expect(new_sshkey, 'password: ')
    if i == 0:
        print 'ERROR.! SSH could not login.'
        print child.before, child.after
    if i == 1:
        child.sendline('yes')
        child.expect('password: ')
        i = child.expect('password: ')
        if i == 0:
            print 'ERROR.! Permission Denied'
            print child.before, child.after
    return child

def main():
    host = raw_input('Hostname: ')
    user = raw_input('Username: ')
    password = raw_input('Password: ')
    child = ssh_command(user, host, password)
    child.expect(pexpect.EOF)
    print child.before

if __name__ == '__main__':
    try:
        main()
    except Exception, e:
        print str(e)

1 个答案:

答案 0 :(得分:1)

很可能错误是由此行引起的

i = child.expect(new_sshkey, 'password: ')

根据文档,expect方法声明如下

child.expect(pattern, timeout=-1, searchwindowsize=-1, async=False)

在你的情况下,你传递'password: '作为第二个参数(timeout) 可能应该是int