13

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.

  • 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? – hytromo 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
5

I'm having a similar need sometimes, as long as I connect over Putty to our VPN server and from there over ssh to some other host, which is not reachable for me directly due to VPN setup.

Sometimes I just need to quickly check something on a VPN server machine, while still having my "ssh-session" running. One approach is to run a ssh session under screen, which, as I've noticed, add some delay in comparison with "regular ssh". Another approach, which I'd like to share here, is the following:

While under SSH session, Press Enter, then ~, (make sure it's not displayed, i.e. you're in a command mode), then Ctrl-Z. This will put the ssh client process on a "host" to background, and you'll something along the lines of:

[root@client170 ~]# ~^Z [suspend ssh]

[1]+  Stopped                 ssh root@10.0.0.167
[root@vps291736 ~]#

Now you're on the "host", can do whatever you want (although, I am not sure how long ssh session will be kept alive), and then return to SSH session by running fg.

At least, this works for me while I am connected from a Win10 workstation via Putty on a CentOS-based VM, and from that VM connect over SSH to some other host.

Hope it helps someone!

  • 2
    If you have PermitLocalCommand yes in your .ssh/config for a host, you can also do <Enter>~C, then !<command>. – muru Nov 18 '16 at 13:13
4

@62mkv's answer is a much better solution. Use that.

For completeness and curiosity though, if you have an ssh server running on your local machine, you could create an ssh tunnel to allow ssh connections from the remote host on port 20202 back to the local one on port 22. Example command:

ssh -R20202:localhost:22 remoteuser@remotehost.com

This will start an ssh connection, but also set up a tunnel back to the ssh server running on your machine. Then you can do this, when ssh'ed into the remote host:

ssh -p 20202 localuser@localhost

Of course, this can quickly get confusing - especially if the technique is nested more than once. It adds a bit of latency, too - since everything you execute on your local machine is bounced through the remote host.

Additional information about ssh tunneling for those whose curiosity hasn't been satisfied yet can be found in answer to this unix stackexchange question.

0

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
Password:
remoteuser@remote> hostname
remote
remoteuser@remote> ssh localuser@local
Password:
localuser@local> hostname
local
localuser@local> exit
remoteuser@remote> exit
localuser@local>

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

from fabric.api import *

@hosts('host1')
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')
0

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.

0

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: http://tombuntu.com/index.php/2008/02/20/public-key-authentication-for-ssh-made-easy/

0

If you have access to another terminal, you can send SIGSTOP using "kill -19 PID" to the ssh command that initiated the conection. You will then get control of terminal on the terminal on the client machine. Once you have finished, you can restore ssh connection by simply typing fg to wake up ssh client.

But of course the question here is : if you already have a terminal on the client machine why would you do this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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