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)
  • 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. –  Dec 05 '17 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? – user7876465 Dec 05 '17 at 21:31
  • What you probably want is to check [how to run command on remote server](https://www.garron.me/en/go2linux/ssh-sudo-run-commands-remote-server.html), but I'm not sure that you can nest another ssh call into it... – Yaroslav Admin Dec 05 '17 at 21:32
  • Possible duplicate of [Calling an external command in Python](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – xaav Dec 05 '17 at 21:35
  • 1
    Apologies, but if you are downvoting answers instantly because they do not suite your requirement, please provide a relevant reason as to why. –  Dec 05 '17 at 21:35
  • 1
    Would ssh'ing and sending your commands as a batch be an option in your Python script? https://superuser.com/questions/247152/how-to-excute-commands-over-ssh-in-a-batch-file – PhillipD Dec 05 '17 at 21:36
  • The reason it runs individually is because it is a non-blocking function, calling a shell to open up outside the thread that the program sits on. Thus they will call almost instantly. However if you SSH'd into the machine correctly, the next shell call wouldn't "simultaneously" open up *yet* another window, but rather directly influence the current one. –  Dec 05 '17 at 21:37
  • I know what I did is not correct, so is there any way to do this? – user7876465 Dec 05 '17 at 21:42
  • @YaroslavAdmin Isn't the `|` operator for shell a way of executing calls one after the other or is that a concatenation thing where it takes the output of the last command and feeds it into the next? –  Dec 05 '17 at 21:43
  • @finnrayment that doesn't work either because as you said, it takes the output of the last command and feeds it into the next. – user7876465 Dec 05 '17 at 21:47
  • Yes, but this is not what op wants. Op want to connect to first server, from there connect to second server and execute command there. This should be single command probably using `ssh myhost `. Since op needs two hops it should be something like this: `ssh abc.def.com ssh qa echo "I run on QA server"`. But I'm not sure one can nest ssh calls this way. – Yaroslav Admin Dec 05 '17 at 21:47
  • @YaroslavAdmin Ah ok, I was thinking you could just chain it that way. I don't know of any ssh nesting either. OP might need a hook on the shell to keep executing directly to it. –  Dec 05 '17 at 21:50

1 Answers1

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)
pankaj mishra
  • 2,555
  • 2
  • 17
  • 31