pythonsshpexpectheredocpxssh

Use a here-document with pxssh (pexpect)


In my python script I need to execute a command over SSH that also takes a heredoc as an argument. The command calls an interactive script that can be also called as follows:

dbscontrol << EOI
HELP
QUIT
EOI

I also found this Q&A that describes how to do it using subprocess but I really like pexpect.pxssh convenience. Code example would be greatly appreciated


Solution

  • I don't have pexpect handy to test my answer to your question, but I have a suggestion that should work and, if not, may at least get you closer.

    Consider this command:

    $ ssh oak 'ftp << EOF
    lpwd
    quit
    EOF'
    Local directory: /home/jklowden
    $ 
    

    What is happening? The entire quoted string is passed as a single argument to ssh, where it is "executed" on the remote. While ssh isn't explicit about what that means, exactly, we know what execv(2) does: if execve(2) fails to execute its passed arguments, the execv function will invoke /bin/sh with the same arguments (in this case, our quoted string). The shell then evaluates the quoted string as separate arguments, detects the HereDoc redirection, and executes per usual.

    Using that information, and taking a quick look at the pexpect.pxssh documentation, it looks like you want:

    s = pxssh.pxssh()
    ...
    s.sendline('ftp << EOF\nlpwd\nquit\nEOF')
    

    If that doesn't work, something is munging your data. Five minutes with strace(1) will tell you what happened to it, and you can start pointing fingers. ;-)

    HTH.