我正在尝试在 Paramiko exec_command
的帮助下从 Python 在 Unix 服务器中运行 sesu
命令。但是,当我运行此命令 exec_command('sesu test')
时,我得到了
sh: sesu: not found
当我运行简单的 ls
命令时,它会给我所需的输出。只有 sesu
命令才能正常工作。
我的代码是这样的:
import paramiko
host = host
username = username
password = password
port = port
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username,password)
stdin,stdout,stderr=ssh.exec_command('sesu test')
stdin.write('Password')
stdin.flush()
outlines=stdout.readlines()
resp=''.join(outlines)
print(resp)
最佳答案
默认情况下,SSHClient.exec_command
不会在“登录”模式下运行 shell,也不会为 session 分配伪终端。因此,与您的常规交互式 SSH session (特别是对于非交互式 session ,.bash_profile
不是来源)相比,(可能)获取了一组不同的启动脚本。和/或根据 TERM
环境变量的存在/不存在,采用脚本中的不同分支。
可能的解决方案(按优先顺序):
修复命令不依赖于特定环境。在命令中使用
sesu
的完整路径。例如:/bin/sesu test
如果您不知道完整路径,在常见的 *nix 系统上,您可以在交互式 SSH session 中使用
which sesu
命令。修复您的启动脚本,为交互式和非交互式 session 设置相同的
PATH
。尝试通过登录 shell 显式运行脚本(使用普通 *nix shell 的
--login
开关):bash --login -c "sesu test"
如果命令本身依赖于特定的环境设置并且您无法修复启动脚本,则可以在命令本身中更改环境。其语法取决于远程系统和/或 shell。在常见的 *nix 系统中,这是可行的:
PATH="$PATH;/path/to/sesu" && sesu test
另一种(不推荐)方法是使用
get_pty
参数强制为“exec” channel 分配伪终端:stdin,stdout,stderr = ssh.exec_command('sesu test', get_pty=True)
使用伪终端自动执行命令会给您带来严重的副作用。参见示例 Is there a simple way to get rid of junk values that come when you SSH using Python's Paramiko library and fetch output from CLI of a remote machine?
您可能对 LD_LIBRARY_PATH
和定位共享对象有类似的问题。
另见:
关于python - 使用 Python Paramiko exec_command 执行时,一些 Unix 命令失败并显示 "<command> not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55419330/