问题描述
echo Something=Something > file
我可以使用 paramiko 的 exec_command 来执行 cat
、grep
和 ls
,但是每当我尝试修改文件时它什么都不做.在此之前我已经运行了 su
.该文件与运行该命令之前的文件完全相同.
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.
推荐答案
这是因为您必须为每个 exec_command 调用打开一个新通道.这将失去 su 命令的身份验证,因为它与特定通道相关联.
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.
您有几个选择.
- 使用 sudo 运行命令,这可能无法通过 paramiko 实现
- 以 root 身份登录,这不一定是个好主意
- 在您的频道上使用 invoke_shell(),然后通过 std 将命令发送到 shell
选项 3 允许 ssh 与 paramiko 交互使用,保持状态信息完整.这就是 su 命令所需要的.它还允许您围绕 shell 连接创建 pexpect 类型包装器,观察标准输出管道以指示事情已完成,并且您可以通过标准输入发送其他命令.在读取数据之前,请注意管道是否已填满和阻塞.
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.
这篇关于为什么 paramiko 不能运行这个命令?(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!