期望值超时

时间:2018-12-12 09:09:18

标签: python python-2.7 ssh paramiko

我正在尝试执行自动备份过程,并且一切正常,直到我尝试在此行interact.expect("Enter choice (1-10) or q to quit: ")上使用Expect。我遇到的错误是raise socket.timeout(),是由expect中的字符串引起的,我认为该错误是不正确的,尽管如果我手动登录并输入我会从shell中复制确切的信息?有人知道解决办法吗?谢谢!

import paramiko
from paramiko_expect import SSHClientInteraction
def backup():

# will match root user prompt
    root_prompt = '.*# '

# will match Password: or password:
    pass_prompt = '.*assword: '


    try:
        client = paramiko.SSHClient()
        client.load_system_host_keys() 
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 

        client.connect(hostname=server, username=user, password=pass)




    except paramiko.AuthenticationException:
        print("Authentication failed, please verify your credentials: %s")
    except paramiko.SSHException as sshException:
        print("Unable to establish SSH connection: %s" % sshException)
    except paramiko.BadHostKeyException as badHostKeyException:
        print("Unable to verify server's host key: %s" % badHostKeyException)
    except Exception as e:
        print(e.args)

    with SSHClientInteraction(client,timeout=10,display=True) as interact: 

        interact.send('su')
        interact.expect(pass_prompt)

        interact.send("pass")
        interact.expect(root_prompt)

        interact.send("BackupMenu")#displays specific menu for backup but menu is not using shell specific commands
        interact.expect("Enter choice (1-10) or q to quit: ")

        cmd_output_ls = interact.current_output_clean


        print cmd_output_ls


    client.close()

1 个答案:

答案 0 :(得分:0)

我已将上面的代码更新为可行的解决方案。我将interact.expect("Enter choice (1-10) or q to quit: ")更改为'.*: '