0

Good Afternoon All,

I recently became aware of my ability to send commands back to my local workstation from a remote SSH connection by using the <enter>~C escape sequence, like:

[root@host ~]# 
[root@host ~]# 
ssh> !ip ad

Now, I would love to be able to do this using a bash script to automatically run commands on my local workstation or do other things, but I've been unable to find any way of sending commands back to this shell programmatically, nor found anyone else who's asked/tried. To be clear, I'm not trying to send commands TO a remote server via SSH - I'm trying to send commands BACK to my workstation FROM an SSH connection through the client (as pictured above).

If anyone has any suggestions or knows how to do this, I would be greatly appreciative of your help. Thanks!

1

I think this is not possible because the escape character never arrives at Bash on the remote system. It is intercepted by your local SSH client before the remote bash can every set it.

I know it isn't what you were asking for, and may not be feasible depending on your situation, but an alternative solution might achieve your objective:

Run the command on your local machine by SSH'ing back to it from the remote machine:

[root@remote-host ~]# ssh your-local-machine ip ad

2
  • Yeah, that's what I was afraid of and had been reading. Having an SSH server listening on my local workstation sounds like a terrible idea from a security perspective, but may be the most simple/practical solution to what I was looking for specifically.
    – Adam V
    Mar 28 '18 at 1:13
  • @AdamV To reduce the security risk, use firewall rules on your local workstation to prevent access to the SSH service from anywhere except the remote-host that you need to connect. To achieve this with iptables, use something like the following on your local workstation: iptables -I INPUT 1 -p tcp -s x.x.x.x --dport 22 -j ACCEPT followed by: iptables -I INPUT 2 -p tcp --dport 22 -j DROP where x.x.x.x is the IP address of the remote host.
    – bao7uo
    Mar 31 '18 at 0:57
0

As it is a security concern, you will not be able to run local commands from a distant host within a SSH connection.

The way you escape your connection to pass commands is thru the terminal, ie. from the local server.

So, if you want to run both a connection and a script thru a SSH connection, you may try a look at expect, but it will be by embedding or wrap your SSH connection in another application, not directly from the remote shell.

1
  • I hadn't actually considered the security implications, but that's a good point - were scripts able to communicate with the SSH client like that, then malware and other bad things could spread as soon as you connected.
    – Adam V
    Mar 28 '18 at 1:14

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.