开发者

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

开发者 https://www.devze.com 2023-03-11 06:08 出处:网络
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 fi开发者_Python百
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 fi开发者_Python百科le remains exactly the same as it was before running the command.


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.

0

精彩评论

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

关注公众号