Python, Paramiko: How to do a “ssh -n user@host cmd” using paramiko?

妖精的绣舞 提交于 2019-12-19 10:29:16

问题


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.

That is, the same as using the OpenSSH client with its -n flag:

ssh -n user@host cmd

How do you achieve this (-n) with Paramiko?

paramiko.SSHClient.exec_command() doesn't seem to allow this, but maybe I'm missing something?


回答1:


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?

EDIT: 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()



回答2:


I would ditch paramiko and start using fabric. It will let you do remote calls on the system. It uses paramiko for the ssh connection and provides the nice clean interface for doing alot more.

I am not sure why you need to pip stdin to /dev/null but there are settings to suppress it with fabric.

Goodluck!



来源:https://stackoverflow.com/questions/7555805/python-paramiko-how-to-do-a-ssh-n-userhost-cmd-using-paramiko

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!