python paramiko ssh session does not get the system path

paramiko, path, python, system

Solution

I don't think any bashrc or profile scripts are being sourced when you use exec_command(). Maybe try the following:

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

If the problem is that you are trying to run a command that's normally in your PATH, but isn't when you use exec_command(), you're probably better off calling the command by its absolute path (run "which [command]" when you're logged into the other machine normally to find out what that is).

Problem

I'm facing a problem that as I ssh to another machine, my paramiko ssh session does not see the same system PATH as when I manually ssh to the machine. Here is my python code: ``` 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() ``` The output show /usr/bin:/bin but when I manually ssh to the machine, there are several other paths on the system PATH. Please help.

Original source