Reputation: 17
I have a requirement where I have to run these few commands in the terminal using a python script.The requirement is as such:
ssh abc.xyz.com
After ssh to that machine, ssh to the environment
ssh qa
and then run few commands in the environment
I have tried achieving this using os.system() and using subprocess.call() but no luck.
Specifically, I tried doing this :
import subprocess
from time import sleep
subprocess.call("ssh abc.def.com", shell=True)
subprocess.call("python", shell=True)
sleep(0.3)
subprocess.call("ssh qa", shell=True)
Upvotes: 0
Views: 120
Reputation: 2615
i am not sure about your approach of connecting to remote server and executing command there with subprocess.
Instead You can use paramiko module to connect with remote server and execute command
import paramiko
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.1.26',port=22,username='root',password='default')
# you can use your own commands in exec_command()
stdin,stdout,stderr=ssh.exec_command('echo 123')
output=stdout.readlines()
print '\n'.join(output)
Upvotes: 1