Why can't paramiko run this command? (Python)

微笑、不失礼 提交于 2019-12-24 09:30:58

问题


echo Something=Something > file

I can use paramiko's exec_command to do cat, grep and ls, but whenever I try to modify a file it does nothing. I already ran su before this. The file remains exactly the same as it was before running the command.


回答1:


This is because you have to open a new channel for each exec_command call. This loses the authentication of the su command since it is associated to a specific channel.

You have a couple of options.

  1. run the command with sudo, which may not be possible over paramiko
  2. Log in as root, which is not necessarily a good idea
  3. Use invoke_shell() on your channel, then send commands via std in to the shell

Option 3 allows for interactive use of ssh with paramiko, keeping state information intact. That is what you need for su commands. It also allows you to create a pexpect type wrapper around your shell connection, watching the stdout pipe for indications that things are done, and you can send additional commands through stdin. Just watch out for the pipes filling up and blocking until you read data.



来源:https://stackoverflow.com/questions/6269000/why-cant-paramiko-run-this-command-python

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