繁体   English   中英

python paramiko ssh session没有得到系统路径

[英]python paramiko ssh session does not get the system path

我遇到一个问题,当我ssh到另一台机器时,我的paramiko ssh会话看不到与我手动ssh到机器时相同的系统路径。 这是我的python代码:

cmd = "echo $PATH"
try:
    ssh.connect(ip, username=username, password=password)
except Exception as ex:
    raise Exception("Failed to connect to %s with credentials username='%s' password='%s' %s" \
          % (ip, username, password, ex.message) )

ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd)
output = ssh_stdout.read()

输出显示/ usr / bin:/ bin但是当我手动ssh到机器时,系统PATH上还有其他几个路径。 请帮忙。

我不认为在使用exec_command()时会获取任何bashrc或profile脚本。 也许尝试以下方法:

stdin, stdout, stderr = ssh.exec_command("bash -lc 'echo $PATH'")
my_path = stdout.read().rstrip()

如果问题是你正在尝试运行一个通常在你的PATH中的命令,但是当你使用exec_command()时,你可能最好用它的绝对路径调用命令(运行“which [command]”)当你正常登录到另一台机器时,找出它是什么)。

最好在运行命令之前加载bash_profile。 否则,您可能会收到“未找到命令”的异常。

例如,我为了转储Mysql表而编写命令command = 'mysqldump -uu -pp -h1.1.1.1 -P999 table > table.sql'

然后我必须通过键入在该转储命令之前手动加载bash_profile . ~/.profile; .~/.bash_profile; . ~/.profile; .~/.bash_profile;

my_command  = 'mysqldump -uu -pp -h1.1.1.1 -P999 table > table.sql;'

pre_command = """
. ~/.profile;
. ~/.bash_profile;
"""

command = pre_command + my_command

stdin, stdout, stderr = ssh.exec_command(command)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM