Setting up a control socket is so easy that I would argue it is absolutely worth doing, even with only two calls to send_ssh
.
send_ssh() {
ssh -o ControlMaster=auto -o ControlPersist=10 -oBatchMode=yes -oConnectTimeout=5 -p "$1" -tq "$2"@"$3" "$4" || exit 1
}
ControlMaster=auto
means that an existing connection will be used if available, otherwise one will be opened for you.
The setting ControlPersist=10
means that the connection to the remote host will remain open for 10 seconds after the client exists, you can adjust this as desired. You can set it to 0
or yes
to keep it open permanently, in which case you'll want to make sure something like
ssh -o exit -p "$1" "$2@$3"
executes before your script exits to close the master connection.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…