I want to execute some sequence of ssh commands through python script like below, (and want to display the output of the last command)
ssh hostname
sudo su userdb
cd /some/path/
./script_file -D option
I have tried with subprocess.Popen(each_command_from_list).
But it is getting stuck after connecting to hostname with ssh.
import subprocess
commands = ["ssh hostname", "sudo su userdb", "cd /some/path/", "./script_file -D option"]
for cmd in commands:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
proc_stdout = process.communicate()[0].strip()
if cmd.startswith("./script_file"):
print(proc_stdout)
I expect, script should connect to hostname with ssh, and then run the commands in that host, and should get the results.