0

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)
12
  • 1
    Because this calls each command individually on your machine. Also, there is no need for sleep(0.3), and furthermore judging by your question, the subprocess.call("python", shell=True) should be the last call. Commented Dec 5, 2017 at 21:29
  • everything runs individually and I want to run everything step after step. How can I achieve that? After ssh into the machine, how can I ssh to the environment? Commented Dec 5, 2017 at 21:31
  • What you probably want is to check how to run command on remote server, but I'm not sure that you can nest another ssh call into it... Commented Dec 5, 2017 at 21:32
  • 1
    Apologies, but if you are downvoting answers instantly because they do not suite your requirement, please provide a relevant reason as to why. Commented Dec 5, 2017 at 21:35
  • 1
    Would ssh'ing and sending your commands as a batch be an option in your Python script? superuser.com/questions/247152/… Commented Dec 5, 2017 at 21:36

1 Answer 1

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.