9

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 .

5
  • Assuming you fix the syntax errors, what happens when you run the above program? Commented Nov 12, 2013 at 20:16
  • "the script is test.sh but we use an env variable to launch it, run test.sh" I can't figure out what this means. How do you use an env variable to launch a script? Commented Nov 12, 2013 at 20:23
  • That should work perfectly, assuming that the remote shell's .profile or similar start-up sets the $run environment variable. You can test this by ssh.exec_command('echo hello ; echo $run ; echo goodbye'). Commented Nov 12, 2013 at 20:23
  • @JimGarrison - the clue is in the code. He meant $run test.sh. Commented Nov 12, 2013 at 20:24
  • Then you haven't set the run environment variable on the remote machine. How are you trying to set run? Commented Nov 12, 2013 at 20:50

2 Answers 2

6

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

ssh.exec_command('. .profile ; cd /home/test/;$run ./test.sh')
Sign up to request clarification or add additional context in comments.

3 Comments

hmm is that a ./.profile? It still is returning blank for $run. It does not throw any errors though.
The first command is either . .profile or . ./.profile. It is not ./.profile.
Awesome, it worked with . ./.profile Thanks for your help!
0

Plumbum to the rescue.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.