![](/img/trans.png)
[英]How to send STDIN twice to Popen process, each time with EOF?
[英]How to send EOF to stdin in paramiko?
我想通過ssh執行一些程序並從文件中重定向其輸入。 以下代碼的行為:
channel.exec_command('cat')
with open('mumu', 'r') as f:
text = f.read()
nbytes = 0
while nbytes < len(text):
sent = channel.send(text[nbytes:])
if sent == 0:
break
nbytes += sent
應該等同於(假設公鑰驗證):
ssh user@host cat < mumu
然而,應用程序掛起等待更多輸入。 我認為這是因為stdin流永遠不會關閉。 我怎么做?
在通道上調用shutdown()
(或shutdown_write()
)。
調用方法: channel.shutdown_write()
。
因為我沒有明確地使用頻道,所以我必須做一些不同的事情。 對於任何可能發現它有幫助的人:
client = paramiko.SSHClient()
connection = client.connect(hostname)
stdin, stdout, stderr = connection.exec_command('cat')
stdin.write('spam')
# Close the channel, this results in an EOF for `cat`.
stdin.channel.shutdown_write()
# stdout/stderr are readable.
print(stdout.read().decode())
print(stderr.read().decode())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.