Linux – Use bash script to send commands to SSH client

bashlinuxshell-scriptingssh

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!

Best Answer

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