Question

am using pexpect ssh to write down a script for compilation, the ssh automation looks like this,

enter code here
child = ssh_expect('root', server2, cmd)
child.expect(pexpect.EOF)
print child.before

where cmd is this:

cmd = "./configure CFLAGS=\"-g -O0 -DDEBUG\""

the problem happens is that it says,

configure: error: unrecognized option: -O0

whereas, if run the same command using commands.getoutput then it executes properly.

Question what is the problem that this kind of error is getting generated and how can I erradicate this one?

thanks in advance :)

Was it helpful?

Solution

The reason it's working if you're doing commands.getoutput is that all commands there are run though a shell, which will parse your commandline and understand that what's between the double quotes after CFLAGS is part of the same parameter.

When you're running cmds through pexpect, no shell is involved. Also, there's no shell involved on the other side of the ssh connection when you provide commands on the ssh commandline, so there's nothing that parse CFLAGS into one parameter. Therefore, instead of the configure script getting one parameter (CFLAGS=\"-g -O0 -DDEBUG\"), it gets three parameters ('CFLAGS=-g', '-O0', '-DDEBUG').

If possible, avoid sending commands where parameters are separated by spaces. It seems pexpect can take a list of arguments instead. Working code example:

#/usr/bin/env python

import pexpect

def ssh_expect(user, hostname, cmd):
    child = pexpect.spawn("ssh", ["%s@%s" % (user, hostname)] + cmd, timeout=3600)

    return child

child = ssh_expect("root", "server.example.com", ["./configure", "CFLAGS=\"-g -O0 -DDEBUG\""])
child.expect(pexpect.EOF)
print child.before
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top