【问题标题】:pxssh how to sendline 'N' to prompt "[Y/N]"pxssh 如何发送'N'以提示“[Y/N]”
【发布时间】:2018-01-05 21:43:38
【问题描述】:

我想使用 pxssh 为 NetScaler 制作一个自动升级脚本——一个基于 FreeBSD 的产品。

我在 FreeBSD 上成功上传和 tar 固件并安装它。

使用 promop "Do you want to enable it NOW? [Y/N]" ,我的脚本无法发送“否”给它

错误代码是“'str' object is not callable”

下面是输出:

You can also configure this feature anytime using the command line 
interface or the configuration utility.  Please see the documentation 
for further details.

Do you want to enable it NOW? [Y/N]
'str' object is not callable
[root@raynor NS_Auto_Upgrade]# 

下面是我的脚本

try:

    ssh_command = pxssh.pxssh()
    hostname = remotename
    username = loginname
    password = loginpassword
    #ssh_command.PROMPT= '> | Do you want to enable it NOW? [Y/N]'           #self-defined original prompt 
    ssh_command.PROMPT= '> '           #self-defined original prompt 
    #ssh_command.login(hostname, username, password,original_prompt='[#$>]')
    ssh_command.login(hostname, username, password,auto_prompt_reset=False)
    ssh_command.prompt()
    print(ssh_command.before)
    ssh_command.sendline('shell')   # run a command
    #print(ssh_command.before)
    #ssh_command.PROMPT='Do you want to enable it NOW? [Y/N]'             # match the prompt
    print(f[0:-4])
    ssh_command.prompt()
    print(ssh_command.before)
    ssh_command.sendline('mkdir /var/nsinstall/%s && ls -l /var/nsinstall' % f[0:-4])
    ssh_command.prompt()
    ssh_command.sendline('tar xzvf /var/nsinstall/%s -C /var/nsinstall/%s' % (f, f[0:-4]))
    ssh_command.prompt()
    print(ssh_command.before)
    ssh_command.sendline('/var/nsinstall/%s/installns' % f[0:-4])
    ssh_command.PROMPT='Do you want to enable it NOW? [Y/N]'
    ssh_command.prompt()
    print(ssh_command.before)
    ssh_command.sendline('No')
    ssh_command.PROMPT('Reboot NOW? [Y/N]')
    ssh_command.prompt()
    ssh_command.sendline('Yes')
    #ssh_command.prompt()
    print(ssh_command.before)

【问题讨论】:

    标签: python pexpect pxssh


    【解决方案1】:

    我根本不知道你的图书馆,但我猜问题是这两行之间的冲突:

    ssh_command.PROMPT='Do you want to enable it NOW? [Y/N]'
    ...
    ssh_command.PROMPT('Reboot NOW? [Y/N]')
    

    我不确定哪个是正确的,但可能您想要么两次都调用PROMPT,要么两次都分配给它,而不是分配一次并调用另一个。

    【讨论】:

    • 感谢您的回复!第一个提示是“do you want to enable…”,我需要发送行“No”,然后进程继续,第二个提示“rebo​​ot now”,此时,我需要输入“Yes” 希望它可以帮助你明白
    • 是的,但是在一个地方你在做PROMPT=...,在另一个地方你在做PROMPT(...)。第二个可能应该和第一个一样(尽管我想可能是相反的,我根本不知道你的图书馆)。请注意,对于调试,删除用于捕获异常的 try/except 块可能很有用。让它使程序崩溃可能会有所帮助,因为它会向您显示异常的回溯,以确定它发生的确切位置。
    • 您的意思是,如果我之前定义了 PROMPT,它将持续到函数的末尾。感谢您的建议,我将尝试删除 try/except 块。我很困惑的是 pxssh 可以期待这样的提示“现在重新启动[Y/N]”
    • 我不明白为什么不这样做。我已经简要地查看了文档,我认为如果你想为几行搜索一种特殊的提示,你可能不应该使用 prompt() 方法,而是更基本的 expect方法(记录在here)。 prompt 方法适用于您计划重复匹配同一事物时(就像您在代码的前面几行中所做的那样)。
    • 是的!当然,我通过expect_exact()函数测试通过了,这是prompt()可能只能匹配“$#>”的关键点。非常感谢您的建议!!!
    猜你喜欢
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 2013-01-09
    • 2017-09-04
    • 2015-03-13
    相关资源
    最近更新 更多