2013-07-15 5 views
5

Ich habe ein Problem, dass, wie ich ssh auf eine andere Maschine, meine Paramiko SSH-Sitzung nicht das gleiche System PFAD sehen, als wenn ich manuell ssh auf die Maschine. Hier ist meine Python-Code:Python Paramiko SSH Sitzung erhält nicht den Systempfad

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() 

Die Ausgabe Show/usr/bin/bin aber wenn ich von Hand in die Maschine ssh, gibt es mehrere andere Pfade auf dem Systempfad. Bitte helfen.

Antwort

5

Ich glaube nicht, dass Bashrc oder Profilskripte stammen, wenn Sie exec_command() verwenden. Vielleicht ist der Versuch folgende:

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

Wenn das Problem ist, dass Sie einen Befehl auszuführen versuchen, die normalerweise in Ihrer PATH ist, ist aber nicht, wenn Sie exec_command() verwenden, sind Sie wahrscheinlich besser dran Aufruf des Befehls durch seinen absoluten Pfad (führen Sie "which [command]" aus, wenn Sie normalerweise bei der anderen Maschine angemeldet sind, um herauszufinden, was das ist).

3

Sie sollten besser das bash_profile laden, bevor Sie Ihren Befehl ausführen. Andernfalls erhalten Sie möglicherweise eine Ausnahme "Befehl nicht gefunden".

Zum Beispiel schreibe ich den Befehl command = 'mysqldump -uu -pp -h1.1.1.1 -P999 table > table.sql' in dem Zweck, eine Mysql-Tabelle von Dumping

Dann muss ich vor diesem Dumping Befehl bash_profile manuell laden, indem Sie . ~/.profile; .~/.bash_profile; eingeben.

Beispiel

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)