2015-09-25 55 views
1

我試圖使用一個子進程通過sftp與一些文件進行交互。我可以讓它連接並執行一個命令,但是這個過程會死掉。我怎樣才能保持對這個過程的控制,並繼續在sftp中移動?如何向Python子進程提交多個命令?


import os 
import subprocess 

serverFiles = 'sftp.servername.com' 
userFiles = 'myusername' 
keyfileFiles = 'C:\\key\\file\\path' 


with subprocess.Popen(['sftp','-Q','-i',keyfileFiles,userFiles+'@'+serverFiles], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT) as proc: 
    (out, err) = proc.communicate(input='ls -l'.encode()) 
    print(out.decode()) 
    #process dies. cannot proc.stdin.write, cannot proc.communicate 

我的輸出顯示我歡迎橫幅,然後從頂層文件夾中的LS,則進程結束,任何下列命令的錯誤出與proc被關閉。

+0

你能使用其他python模塊的SFTP像paramiko? http://www.paramiko.org/ 你也可以在shell上編寫交互命令的pexpect腳本? 我不認爲subprocess是用於交互式shell命令的正確模塊 – cmidi

+0

我的第一個想法是使用paramiko,但有一些限制阻止方式的限制。即版本管理和管理對外部軟件包的厭惡。你有沒有在子流程之外的建議? – meteorainer

+0

我會建議pexpect https://pexpect.readthedocs.org/en/stable/爲交互式shell命令創建一個期望腳本,如果pexpect可用 – cmidi

回答

相關問題