Python,Paramiko:如何执行“ssh -n user@host cmd"使用paramiko? [英] Python, Paramiko: How to do a "ssh -n user@host cmd" using paramiko?
问题描述
我正在尝试通过 SSH 从 Python 远程执行命令,在这种特殊情况下需要将 stdin 重定向到/dev/null.
I'm trying to execute a command remotely via SSH from Python, and in this particular case need stdin to be redirected to /dev/null.
也就是说,与使用带有 -n 标志的 OpenSSH 客户端相同:
That is, the same as using the OpenSSH client with its -n flag:
ssh -n user@host cmd
您如何使用 Paramiko 实现这一点 (-n)?
How do you achieve this (-n) with Paramiko?
paramiko.SSHClient.exec_command() 似乎不允许这样做,但也许我遗漏了什么?
paramiko.SSHClient.exec_command() doesn't seem to allow this, but maybe I'm missing something?
推荐答案
除非我错误地理解您的问题:您不需要实现这一点.除非您自己明确这样做,否则不会自动从标准输入读取/写入远程进程的标准输入.所以你不需要阻止从 stdin 读取?
Unless I understand your question incorrectly: you don't need to achieve this. Nothing is automatically read from stdin/written to the remote process' stdin unless you yourself explicitly do so. So you don't need to prevent reading from stdin from happening?
如果远程进程需要标准输入上的数据,并且一直在等待它,那么可能会出现问题?尝试在频道上调用 shutdown_write()
:
there might be an issue if the remote process expects data on stdin, and keeps waiting for it? Try calling shutdown_write()
on the channel:
stdin, stdout, stderr = client.exec_command(cmd)
stdin.channel.shutdown_write()
这篇关于Python,Paramiko:如何执行“ssh -n user@host cmd"使用paramiko?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!