2015-05-01 78 views
1

我使用Python庫的paramiko到另一臺服務器上運行,通過ssh的命令。我現在面臨的問題是,SSHClient.exec_command()調用立即返回,送我stdinstdout,並stderr並給了我沒有別的辦法,我可以看到說,如果該進程仍在運行與否。我想,我可能會嘗試監測,看是否流返回仍然是開放的,但我找不到任何辦法做到這一點,除了試圖從stdoutstderr來讀取或寫入到stdin並等待接收ValueError。任何人都可以告訴我,我錯過了應該改變的東西嗎?我如何知道遠程進程正在運行或完成?

+0

使用通道API? http://paramiko-docs.readthedocs.org/en/latest/api/channel.html – fixxxer

+0

您可能要開始使用面料:http://www.fabfile.org/ –

+0

@ReutSharabani感謝。織物看起來有趣。我會在某個時候檢查一下。現在,我已經設法讓Paramiko開始工作。 – Adrian

回答

1

由於建議從@fixxxer我發現我需要知道。我的測試代碼現在看起來像這樣:

import paramiko 
import time 

ssh = paramiko.SSHClient() 
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
ssh.connect('localhost', username='user', password='password') 

transport = ssh.get_transport() 
channel = transport.open_session() 
channel.exec_command('./exec_test.py') 

status = channel.recv_exit_status() 

這很好用。它阻塞,直到命令完成,然後讓我繼續。

+0

警告字 - 如果命令輸出足夠大以填充管道,則會發生死鎖。 – tdelaney

相關問題