开发者

pexpect ssh not able to handle command options

开发者 https://www.devze.com 2023-03-23 00:21 出处:网络
am using pexpect ssh to write down a script for compilation, the ssh automation looks like this, enter code here

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.ex开发者_如何学JAVApect(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 :)


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
0

精彩评论

暂无评论...
验证码 换一张
取 消