开发者

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

开发者 https://www.devze.com 2023-04-08 08:50 出处:网络
I\'m trying to execute a command remotely via SSH from Python, and in this particular case need stdin to be redirected to /dev/nu开发者_StackOverflow社区ll.

I'm trying to execute a command remotely via SSH from Python, and in this particular case need stdin to be redirected to /dev/nu开发者_StackOverflow社区ll.

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?


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


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号