0

Is it possible to create a single SSH tunnel to address a remote host like it was sitting on the local system, eg. type the following pseudo-examples locally and achieve the desired effect:

  • Copy databases: mysqldump remote | mysql local or mysqldump local | mysql remote
  • Append text to remote files: echo blah >> remote:file.txt
  • Change remote files: sed ... remote:file.txt
  • Execute remote commands: apk add blah

If a single connection is not possible, what is the most convenient way to achieve these things?

  • Why not use an automation tool like Ansible to do this? – ryekayo Sep 22 '16 at 16:46
  • 1. It's always good to know what standard Unix can do. 2. Can you run spontaneous manual commands on a live system with Ansible & Co.? (I thought they were only for pre-configuration.) – forthrin Sep 22 '16 at 17:09
  • You can run commands using the shell/command modules. However, Ansible is an automation tool that is Python under the hood, so i believe you need to have Python 2.7+. And yes i agree it is good to learn the conventional commands that Linux/Unix has to get a broader understanding of it. – ryekayo Sep 22 '16 at 17:22
  • If this is a duplicate, then (parts of) the proposed answer should be moved over to the duplicate. It's very concise and explains how to do some specific things one would want to do. That said, I feel that this question starts more with "How do I accomplish these tasks?" while the proposed duplicate says specifically "How do I reuse a SSH tunnel"? – forthrin Sep 23 '16 at 6:03
2

For accessing remote mysql you can use localhost:12345 by forwarding:

ssh -Nf -L 12345:localhost:3306 user@remote

For the file operations you can mount the remote host via sshfs:

mkdir /tmp/remote
sshfs user@remote:/ /tmp/remote
echo blah >> /tmp/remote/file.txt
sed ... /tmp/remote/file.txt

Executing remote commands works as usually via ssh:

ssh user@remote command ....

You can do all this by using just one connection if you add this to your ~/.ssh/config:

ControlMaster auto
ControlPath ~/.ssh/masters/%l%r@%h:%p

The first ssh connection will be the master the next ones re-use it. New slave connections will be very fast available. No initial key exchange overhead, etc.

|improve this answer|||||
  • Yes! Now we're talking. I knew the individual techniques from before, but I didn't know you could have master SSH connections to be reused. This is brilliant. So this is as good at it gets, then? – forthrin Sep 22 '16 at 17:23
  • One minor problem which may happen in real life: If you kill the master connection by mistake then any slave connection dies. You can minimize that risk by avoiding ControlMaster auto and starting the first master manually in the background. Another problem: sshfs is not very fast. If you want to do much I/O I would use a network file system. – rudimeier Sep 22 '16 at 17:35
  • Thanks for pointing out the performance issue. I've actually never used nfs before, but I suppose it's quite simple like outlined in this tutorial? digitalocean.com/community/tutorials/… – forthrin Sep 22 '16 at 17:45

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