0

I'm creating a small script and I'd like to pass a varable into a bash command using the python programming language, so for example:

number = raw_input("digit: ")

then i'd like to take the number variable and put it in bash command so for example:

ssh 'foo%s.bar'(number) <- where the %s is located id like it be replaced with the input

Finally I'd like to take that and run it in a bash command still within the python script:

ssh foo45.bar

How can I make this work?

secure_20
  • 19
  • 4

1 Answers1

2
import subprocess
number = raw_input("digit: ")
subprocess.call(('ssh', 'foo{}.bar'.format(number)))

For some good reading, try the python docs for string formatting and for subprocess.

If you want to integrate the above with sshpass, replace the subprocess call with:

subprocess.call(('sshpass', '-p', 'YOUR_PASSWORD', 'ssh', '-o', 'StrictHostKeyChecking=no', 'foo{}.bar'.format(number)))
John1024
  • 109,961
  • 14
  • 137
  • 171
  • @secure_20 Did this answer your question? Or, was there other information that you were looking for? – John1024 Aug 13 '14 at 01:46
  • it kinda answered my question but it's asking me to enter password. I'd like that to be passed in automatically as well @John1024 – secure_20 Aug 13 '14 at 01:49
  • That question has been asked elsewhere on Stackoverflow: see [Automatically Enter SSH Password With Script](http://stackoverflow.com/questions/12202587/automatically-enter-ssh-password-with-script). – John1024 Aug 13 '14 at 01:55
  • @secure_20 I just updated the answer with info for the sshpass solution. If you can use public key authentication (as described in diegows' answer to that question, then I would recommend that instead. – John1024 Aug 13 '14 at 01:58