Setting a session variable for a paramiko session

Does anyone know how to make the environment variables registered for exec_command calls when using SSHClient?

I use a base script that creates an SSHClient class, connects to another computer using the connect method, and then sends commands using the exec_command method. However, none of the environment variables seem to be logged when I try to issue commands. I can do basic things such as "ls" and see stdout, but when I try to run installed programs, the fact that there are no environment variables makes them impossible to run. Using ssh on the command line to perform the same action works as the environment variables for the user are set.

#!/usr/bin/python import paramiko ssh.connect('mymachine',username='myname',password='pass') stdin,stdout,stderr=ssh.exec_command('cd /myfolder/path') stdin,stdout,stderr=ssh.exec_command('ls') .... .... ssh.close() 

Note. I cannot change my directory in paramiko. I added the cd to the following command in one ssh.exec_command('cd /dddd/ddd;ls') . I gave ls as an example, but my actual follow-up command is different.

+6
source share
3 answers
 #!/usr/bin/python import paramiko client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.WarningPolicy) client.connect(myhostname, theport, myuser, thepass) stdin,stdout,stderr = client.exec_command('cd /tmp;pwd;ls -al') #returns your output print stdout.read() 

it all works great for me. If you have special environment variables, you can set them on the remote access command line. Maybe this will help if you write the variables to the myENV file and then call

stdin,stdout,stderr = client.exec_command('source ./myEnv')

Have you tried something like this?

+1
source

You can do: client.exec_command(..., get_pty=True) .

This will force paramiko to allocate a pseudo-terminal similar to ssh .

+1
source

Starting with version 2.1.0 2016-12-09, you can add a dictionary of environment variables in exec_command :

 import paramiko paramiko.util.log_to_file("paramiko.log") ssh = paramiko.SSHClient() k = paramiko.RSAKey.from_private_key_file("<private_key_file>") ssh.connect(<hostname>,username=<username>,pkey=k) env_dict={"LC_TELEPHONE":"ET_HOME","LC_MEASUREMENT":"MILES_APART"} stdin , stdout, stderr = ssh.exec_command('echo $LC_TELEPHONE; echo "..."; echo $LC_MEASUREMENT',environment=env_dict) print stdout.read() 

output:

 ET_HOME ... MILES_APART 

But why did I choose LC_TELEPHONE and LC_MEASUREMENT? Since these are two of several environments that the target ssh configurator allows you to configure:

 grep AcceptEnv /etc/ssh/sshd_config 

output:

 AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL 

In other words, this does not work:

 env_dict={"HELLO":"WORLD","GOODBYE":"CRUEL_WORLD"} stdin , stdout, stderr = ssh.exec_command("echo $HELLO; echo '...'; echo $GOODBYE") print stdout.read() 

output:

 ... 

As the documentation warns, environment variables are silently rejected http://docs.paramiko.org/en/2.1/api/client.html http://docs.paramiko.org/en/2.1/api/channel.html#paramiko.channel .Channel.set_environment_variable

If you cannot control the sshd configuration of the target server by inserting environment variables into the file and using it:

 stdin , stdout, stderr = ssh.exec_command("cat .set_env;source .set_env; echo $HELLO; echo '...'; echo $GOODBYE") print stdout.read() 

output:

 # begin .set_env HELLO="WORLD" GOODBYE="CRUEL_WORLD" # end .set_env WORLD ... CRUEL_WORLD 
+1
source

All Articles