Python - using env variables of remote host with /

2019-04-24 15:47发布

Any help on this issue would be greatly appreciated.

Basically I'm writing a python script that will ssh onto various servers and to execute scripts. The problem is that these scripts use an env variable to start. Ie the script is test.sh but we use an env variable to launch it, run test.sh.

So far the routes I have taken, such as Paramiko module execute commands but do not actually take on the env variables.

import paramiko
ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('testserver' )

stdin, stdout, stderr = ssh.exec_command(cd /home/test/;$run ./test.sh')
print stdout.readlines()
print stderr.readlines()
ssh.close()

Is there a way to use paramiko? Or another way I should take?

Thanks

@ Rob

I edited the script to test. The $run variable is coming back blank.

stdin, stdout, stderr = ssh.exec_command('whoami;echo hello; echo $run ; echo goodbye')

['testuser\n', 'hello\n', '\n', 'goodbye\n']
[]

@ Rob part 2

Logging onto the server, I am able to echo $run and it returns the correct path/script I also checked and this is an env variable that is set in the .profile. I feel like python is not invoking the .profile .

2条回答
Juvenile、少年°
2楼-- · 2019-04-24 16:28

Plumbum to the rescue.

Among other things, it provides a nifty interface, leveraging paramiko (see ParamikoMachine), and also lets you manipulate the remote environment.

查看更多
Rolldiameter
3楼-- · 2019-04-24 16:46

ssh.exec_command doesn't interpret the remote .profile. Try this:

ssh.exec_command('. .profile ; cd /home/test/;$run ./test.sh')
查看更多
登录 后发表回答