Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say I have connected to a remote computer via SSH. From a program on this remote computer, I need to execute a command on my local computer (the connection initiator).

Which raises the question: is it possible to leech onto the existing connection between the two computers to run a command on the local computer?

I have considered running the command ssh user@host-of-connecting-party <command> on the remote computer to establish a reversed connection. But this is harder to automate and will require user intervention. I was hoping I could fully automate it, or at least detect the user/hostname of the connected user.

share|improve this question
It is clear what you want to do. What is not clear to me is the part where you say " I was hoping I could fully automate it, or at least detect the user/hostname of the connected user." What do you mean? – hakermania Aug 23 '12 at 23:00
"harder to automate and will require user intervention" is synonymous to "secure" in this case. If a remote host was able to run commands on clients which connect to it, that would be highly insecure... imagine a server got compromised and anyone who connects to it just gets their local machine infected... that would be fun :) – Sergey Aug 24 '12 at 0:31

If both your local and remote computer are accessible from Internet, you can just open an SSH session from your local computer on the remote computer, and then in that session open another ssh session from the remote computer on the local computer:

localuser@local> ssh remoteuser@remote
remoteuser@remote> hostname
remoteuser@remote> ssh localuser@local
localuser@local> hostname
localuser@local> exit
remoteuser@remote> exit

For automating stuff have a look at Fabric (Python knowledge required)

from fabric.api import *

def clean_and_upload():
    local('find assets/ -name "*.DS_Store" -exec rm '{}' \;')
    local('tar czf /tmp/assets.tgz assets/')
    put('/tmp/assets.tgz', '/tmp/assets.tgz')
    with cd('/var/www/myapp/'):
        run('tar xzf /tmp/assets.tgz')
share|improve this answer

You can use sshpass to connect to the the remote computer and run the commands.

./sshpass -p "password" ssh user@remoteIP 'bash -s < EOI "Your Commands go here" EOI'

Use simple shell script on local computer and use above code whenever you want to run commands on remote computer. This is one of the way i generally used in automation.

share|improve this answer

No, you can't break into the existing session.

Your application would need a way to connect back to the client. SSH would work if the client has an SSH server running and the server can reach port 22 on the client. It wouldn't be hard to automate if you used key-based authentication instead of password authentication - this way user intervention would not be necessary.

Information on setting up key based authentication can be found here:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.