【问题标题】:Using pexpect to send a second password, but not working?使用 pexpect 发送第二个密码,但不起作用?
【发布时间】:2021-08-23 12:45:15
【问题描述】:

我正在尝试使用pexpect 发送密码。它在提示输入密码时第一次起作用("azureuser@xx.xx.xx.xxx's password:"),但第二次不起作用("[sudo] password for azureuser:")。为什么会这样?

我需要留在我正在登录的虚拟机中才能输入密码并执行命令。我应该生成另一个进程吗?

import pexpect

def access_azure_vm(blob_name, src, dest):
    child = pexpect.spawn(key.SSH_KEY)
    child.logfile = sys.stdout.buffer
    child.expect("azureuser@xx.xx.xx.xxx's password:")
    child.sendline(key.PASSKEY)
    child.expect("azureuser@vm")
    # Run sudo azcopy copy <src> <dest>
    child.sendline('sudo azcopy copy ' + "\"" + src + "\"" + " " + "\"" + dest + blob_name + "\"" + " --recursive")
    child.expect("[sudo] password for azureuser:")
    child.sendline(key.PASSKEY)
    child.expect("azureuser@vm")

【问题讨论】:

    标签: command-line process child-process pexpect


    【解决方案1】:

    child.expect(s) 中,字符串s 是一个正则表达式模式,所以[sudo] 表示集合中的单个字符:s u d o。这将不匹配。您可以从字符串中删除此初始部分,也可以改用child.expect_exact(s)

    【讨论】:

    • 哦,谢谢!如此简单的修复,我忽略了。我删除了[sudo],现在它工作得很好,但感谢你告诉我.expect_exact(s) 命令。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多