vai*_*sht 3 python ssh shell paramiko
我试图sesu
在Paramiko的帮助下从Python在Unix服务器中运行命令exec_command
。但是,当我运行此命令exec_command('sesu test')
时,
sh:sesu:未找到
当我运行简单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)
Run Code Online (Sandbox Code Playgroud)
The SSHClient.exec_command
by default does not run shell in "login" mode and does not allocate a pseudo terminal for the session. As a consequence a different set of startup scripts is (might be) sourced (particularly for non-interactive sessions, .bash_profile
is not sourced), than in your regular interactive SSH session. And/or different branches in the scripts are taken, based on an absence/presence of TERM
environment variable.
Possible solutions (in preference order):
Fix the command not to rely on a specific environment. Use a full path to sesu
in the command. E.g.:
/bin/sesu test
Run Code Online (Sandbox Code Playgroud)
If you do not know the full path, on common *nix systems, you can use which sesu
command in your interactive SSH session.
Fix your startup scripts to set the PATH
the same for both interactive and non-interactive sessions.
尝试通过登录外壳显式运行脚本(使用--login
带有通用* nix外壳的开关):
bash --login -c "sesu test"
Run Code Online (Sandbox Code Playgroud)如果命令本身依赖于特定的环境设置,并且您无法修复启动脚本,则可以在命令本身中更改环境。语法取决于远程系统和/或外壳。在常见的* nix系统中,这可以工作:
PATH="$PATH;/path/to/sesu" && sesu test
Run Code Online (Sandbox Code Playgroud)另一种(不推荐)的方法是使用get_pty
参数强制为“ exec”通道分配伪终端:
stdin,stdout,stderr=ssh.exec_command('sesu test', get_pty=True)
Run Code Online (Sandbox Code Playgroud)
使用伪终端自动执行命令会给您带来讨厌的副作用。例如,请参见是否有一种简单的方法来摆脱使用Python的Paramiko库进行SSH并从远程计算机的CLI提取输出时出现的垃圾值?
也可以看看:
归档时间: |
|
查看次数: |
969 次 |
最近记录: |