That's pretty easy to achieve using the nc tool and ssh tunnels.
1. Open ssh tunnel
In your ssh session, type ~C on a new line. You will get the ssh "service console" prompt which looks like this:
ssh>
Type in the local forward command to open an ssh tunnel:
ssh> -L22000:targethost:22001
Forwarding port.
Where targethost
is the hostname or IP address of the machine you are connected to.
Now, assuming the ssh server on the target machine wasn't configured to forbid tunnels, you have the desired connection forwarding: ssh
client on your machine listens to port 22000, and it will forward any traffic sent to it to the 22001 port on targethost
.
2. Start a network server on the remote machine
This is as simple as entering into your already open ssh session the following command:
remote$ nc -l localhost 22001 | sh
This will start a TCP server listening on port 22001 – which is the target port of our ssh tunnel – and route the received data (presumably, shell commands) to a targethost
shell instance.
3. Send your script over the tunnel
local$ cat yourscript.sh | nc localhost 22000
This will send the script's body to your ssh tunnel and will end up being executed in a shell on the targethost
. You will see script's output in your terminal with ssh session.
I'll also note that ssh tunnel (step 1.) in this scenario isn't strictly required; you could as well start the server open and connect to it directly over the internet. However, you will need to use the tunnel if the target host can't be reached directly (e.g. is behind a NAT), or ssh encryption is desired.
/var/log/secure
and/var/log/auth.log
log SSH connections; a slave connection does not appear there because it piggybacks on an existing connection. If your ssh session allocates a terminal (i.e.ssh somehost
with no command supplied, orssh -t
), that is (normally) logged inwtmp
, regardless of how that terminal appeared (sshd whatever method was used to establish the connection, terminal emulator application, …). – Gilles 'SO- stop being evil' Sep 06 '13 at 15:51ControlPersist 600
which is a delay in seconds of the socket being in idle before it gets automatically deleted. Otherwise it will close down automatically when the master connection ends. That's no good for executing a series of commands remotely (eg. a series of rsync commands to different folders) – Jun 19 '16 at 01:05-S
(specify socket) and-M
(create master connection) of the SSH client. – yankee Oct 07 '16 at 20:07