簡體   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