Linux – Retaining SSH session in bash script

bashlinuxscriptingssh

I have a bash script which loops over a server list, runs a couple of remote commands via SSH, and writes the output of those commands to a local file.
For every command I run on the remote server I open a new SSH session.
Is there a way that only one SSH session remains open to a server, and that I can run multiple commands which the output I can redirect to a local file?

Best Answer

You want to look into the ControlMaster setting for SSH. That allows you to create a single SSH connection and then have all the other SSH connections to the same server multiplex over that one connection. Very handy.