I need to execute multiple shell commands in a ssh session using the subprocess
module.
I am able to execute one command at a time with:
subprocess.Popen(["ssh", "-o UserKnownHostsFile=/dev/null", "-o StrictHostKeyChecking=no", "%s" % <HOST>, <command>])
But is there a way to execute multiple shell commands with subprocess
in a ssh session? If possible, I don't want to use packages.
Thank you very much!
Strictly speaking you are only executing one command with Popen
no matter how many commands you execute on the remote server. That command is ssh
.
To have multiple commands executed on the remote server just pass them in your command
string seperated by ;
s:
commands = ["echo 'hi'", "echo 'another command'"]
subprocess.Popen([
"ssh",
"-o UserKnownHostsFile=/dev/null",
"-o StrictHostKeyChecking=no",
";".join(commands)
])
You could alternatively join commands on &&
if you wanted each command to only execute if the previous command had succeeded.
If you have many commands and you are concerned that you might exceed the command line limits, you could execute sh
(or bash) with the -s
option on the remote server, which will execute commands one-by-one as you send them:
p = subprocess.Popen([
"ssh",
"-o UserKnownHostsFile=/dev/null",
"-o StrictHostKeyChecking=no",
"sh -s",
], stdin=subprocess.PIPE)
for command in commands:
p.stdin.write(command)
p.stdin.write("\n")
p.flush()
p.communicate()
Note that in Python3 you will need to encode the command to a byte string (command.encode("utf8")
) before writing it to the stdin
of the subprocess
.
I feel like this is overkill for most simple situations though where the initial suggest is simplest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With