Environment variable differences when using Paramiko

The SSHClient.exec_command by default 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). And/or different branches in the scripts are taken, based on an absence/presence of TERM environment variable.


To emulate the default Paramiko behavior with the ssh, use the -T switch:

ssh -T myuser@host

See the ssh man:

-T Disable pseudo-tty allocation.


Contrary, to emulate the default ssh behavior with Paramiko, set the get_pty parameter of the exec_command to True:

def exec_command(self, command, bufsize=-1, timeout=None, get_pty=False):

Though rather than working around the issue by allocating the pseudo terminal in Paramiko, you should better fix your startup scripts to set the same PATH for all sessions.

For that see Some Unix commands fail with "<command> not found", when executed using Python Paramiko exec_command.


Working with the Channel object instead of the SSHClient object solved my problem.

chan=ssh.invoke_shell()
chan.send('echo $PATH\n')
print (chan.recv(1024))

For more details, see the documentation