8

I would like to actually ssh into a machine (not remotely execute a command like ssh -i key.pem user@host 'bash -s' < script.sh) and then once that is complete run a command to launch an interactive command line program on the remote machine. Any ideas? Note all of this is done in a script and the end result should be a interactive command line program running within the shell on the remote host.

So far I have tried in Python 2.7 on Ubuntu 14.04

import os
os.system("xterm -e ssh -tt -i key.pem -o StrictHostKeyChecking=no ubuntu@ip_address yRouter/src/yrouter --interactive=1 user")

But the xterm session closes immediately after. The "yrouter" is the executable on the remote machine and --interactive=1 user are its arguments.

To clarify: the result after running the Python code above should be an xterm (or any terminal window) open, already sshed into the remote host AND have the interactive command line program yrouter running.

13
  • ssh -i key.pem user@host should give you an interactive login shell session after login...
    – heemayl
    Nov 17, 2015 at 1:19
  • Once what is complete?
    – deltab
    Nov 17, 2015 at 1:19
  • after the interactive shell session begins Nov 17, 2015 at 1:20
  • 1
    Then you should edit your question to add the contents of script.sh and also show whats your target and where it is failing..
    – heemayl
    Nov 17, 2015 at 1:31
  • 1
    Once you ssh into a machine, any program you run is executed on the remote machine. If you want to run a GUI program, you can use ssh -X (or Y); note that is uppercase for both. The remote machine needs to have X-windows running to run an X-windows app, but that's it. Or am I missing something? Nov 17, 2015 at 1:35

1 Answer 1

17

If you pass a command in the arguments ssh runs non-interactively;

$ ssh -t user@localhost echo foo
user@localhost's password: 
foo
Connection to localhost closed.

A solution is to replace the non-interactive shell with an interactive shell in the command passed to ssh:

$ ssh -t user@localhost 'echo foo; exec zsh'
user@localhost's password: 
foo
% 

In your case, to replace the non-interactive shell e.g. with an interactive Bash instance:

import os
os.system("xterm -e ssh -tt -i key.pem -o StrictHostKeyChecking=no ubuntu@ip_address 'yRouter/src/yrouter --interactive=1 user; exec bash'")
10
  • kos, even running non-interactively, until the command returns yRouter/src/yrouter --interactive=1 user (in this case), the session will be active while using -t..
    – heemayl
    Nov 17, 2015 at 2:19
  • As OP has marked it as accepted, perhaps i have misunderstood the question..
    – heemayl
    Nov 17, 2015 at 2:25
  • @heemayl Not really sure what you mean, but kos's answer works. Thanks for your help though. Nov 17, 2015 at 2:31
  • Bear with me @heemayl but I'm not getting exactly what you mean with "active", you mean "interactive"?
    – kos
    Nov 17, 2015 at 2:36
  • 1
    @CheckMateNinja Just a suggestion for you, start using subprocess module instead of deprecated and insecure os.system to run native shell commands..
    – heemayl
    Nov 17, 2015 at 2:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .