Search code examples
pythonpexpectpxssh

pxssh how to sendline 'N' to prompt "[Y/N]"


I want to use pxssh to make a auto-upgrade script for NetScaler---a FreeBSD based product.

I successfully upload and tar firmware on FreeBSD and install it.

With promop "Do you want to enable it NOW? [Y/N]" , my script failed to sendline "No" to it

Error Code is "'str' object is not callable"

Below is output:

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]# 

Below is my script

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)

Solution

  • I don't know your library at all, but I'd guess the issue is the conflict between these two lines:

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

    I'm not sure which is correct, but probably you want to either be calling PROMPT both time, or assigning to it both times, not assigning once and calling the other.