not found"-6ren"> not found"-我正在尝试在 Paramiko exec_command 的帮助下从 Python 在 Unix 服务器中运行 sesu 命令。但是,当我运行此命令 exec_command('sesu test')-6ren">
gpt4 book ai didi

python - 使用 Python Paramiko exec_command 执行时,一些 Unix 命令失败并显示 " not found"

转载 作者:太空宇宙 更新时间:2023-11-03 12:52:55 26 4
gpt4 key购买 nike

我正在尝试在 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 环境变量的存在/不存在,采用脚本中的不同分支。

可能的解决方案(按优先顺序):

  1. 修复命令不依赖于特定环境。在命令中使用 sesu 的完整路径。例如:

    /bin/sesu test

    如果您不知道完整路径,在常见的 *nix 系统上,您可以在交互式 SSH session 中使用 which sesu 命令。

  2. 修复您的启动脚本,为交互式和非交互式 session 设置相同的 PATH

  3. 尝试通过登录 shell 显式运行脚本(使用普通 *nix shell 的 --login 开关):

    bash --login -c "sesu test"
  4. 如果命令本身依赖于特定的环境设置并且您无法修复启动脚本,则可以在命令本身中更改环境。其语法取决于远程系统和/或 shell。在常见的 *nix 系统中,这是可行的:

    PATH="$PATH;/path/to/sesu" && sesu test
  5. 另一种(不推荐)方法是使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com