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?

0

1 Answer 1

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)))
Sign up to request clarification or add additional context in comments.

4 Comments

@secure_20 Did this answer your question? Or, was there other information that you were looking for?
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
That question has been asked elsewhere on Stackoverflow: see Automatically Enter SSH Password With Script.
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.