2013-03-21 67 views
0

我想在Python中调用多个命令。我在Windows上安装了腻子。python子进程popen运行多个命令

  1. ssh来的Linux机器

    ssh_command_string: plink -i yourppk.ppk [email protected] 
    
  2. 调用该机器上的命令,这将产生一个文件

    export_project_command_string: ./AppManage -export -out /opt/notme-Facebook.xml -app "Silver Fabric/notme-FacebookPostsExtraction0118" -user username-pw password -domain ion 
    
  3. 下载这个文件

    download_command_string: pscp -scp -i yourppk.ppk [email protected]:/opt/notme-Facebook.xml d:\ 
    

当我逐一测试它们时,这些命令是可以的,但我不知道如何一次性在Python中调用它们。

我曾尝试下面的代码,但没有运气:

LINE_BUFFERED = 1 
    #NOTE: the first argument is a list 
    p = Popen(['cat'], shell = True, bufsize=LINE_BUFFERED, stdin=PIPE, stdout = PIPE, stderr = PIPE) 
    for cmd in [ssh_command_string, cd_command_string, export_project_command_string, download_command_string]: 
     time.sleep(1) # a delay to see that the commands appear one by one 
     p.stdin.write(cmd) 
     p.stdin.flush() 
    # even without .flush() it works as expected on my machine 
    p.stdin.close() 
+0

这可能对你有用:[使用Python进行SSH的最简单方法是什么?](http://stackoverflow.com/q/1233655/222914) – 2013-03-21 09:47:48

+0

这是与进程协调的错误方式。但我想你忘了使用Popen.comunicate将你的命令发送到进程......用你的代码,如果远程shell发送给你一些文本,你的缓冲区将被填满,并且进程将等待释放它,但是你不会不读p.stdout/p.stderr,所以远程进程永远等待 – ornoone 2013-03-21 10:17:00

回答

0

看看fabric到一个服务器上执行重复/自动任务。