2013-03-21 66 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到一個服務器上執行重複/自動任務。