2011-07-24 90 views
1

正在使用Pexpect的ssh来写下一个脚本编译命令选项,ssh的自动化看起来像这样,Pexpect的SSH无法处理

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

其中cmd是这样的:

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

的问题发生在于它说,

configure: error: unrecognized option: -O0 

而如果使用commands.getoutput运行相同的命令,则它会正确执行。

问题这种错误正在产生的问题是什么,我该如何根除这一错误?

在此先感谢:)

+0

ssh_expect函数是否是您自己创建的?它到底做了什么? –

+0

yes Erik,ssh_expect是由我创建的只是使用pexpect,它的工作方式如下,ssh_expect(user,hostname,cmd): ssh_newkey ='你确定要继续连接' child = pexpect.spawn('ssh- l%s%s%s'%(user,hostname,cmd),timeout = 3600) child.sendline('yes') child.expect('password:') – OpenFile

回答

0

它的工作,如果你正在做commands.getoutput的原因是,所有的命令有虽然shell中运行,这将解析您的命令行和理解,双引号之间有什么后CFLAGS是部分相同的参数。

当您通过pexpect运行cmds时,不涉及shell。而且,当你在ssh命令行上提供命令时,ssh连接的另一端没有涉及shell,因此没有任何内容将CFLAGS解析为一个参数。因此,取代配置脚本获取一个参数(CFLAGS = \“ - g -O0 -DDEBUG \”),它会得到三个参数('CFLAGS = -g','-O0','-DDEBUG')。

如果可能,请避免发送参数以空格分隔的命令。看起来好像可以取而代之的是一个参数列表。工作代码示例:

#/usr/bin/env python 

import pexpect 

def ssh_expect(user, hostname, cmd): 
    child = pexpect.spawn("ssh", ["%[email protected]%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

感谢Eric的解答, – OpenFile