I need a lot of different aliases to create ssh tunnels to different servers. To give you a few of them:
alias tunnel_1='autossh -M 20000 -N -L 8080:localhost:8080 -N -L 9200:localhost:9200 -N -L 8090:localhost:8090 user@server1.xx'
alias tunnel_2='autossh -M 20000 -N -L 8000:localhost:8080 -N -L 9200:localhost:9200 -N -L 8090:localhost:8090 user@server2.xx'
I came up with this function I added in my aliases :
addPort () {
echo "-N -L $1:localhost:$1 "
}
tunnel () {
aliasString="autossh -M 20000 "
for port in "${@:2}"
do
aliasString+=$(addPort $port)
done
aliasString+="$1"
eval $aliasString
}
so I just need to do this to tunnel to the server I want:
tunnel abc@domain.com 8080 9000 7200
It’s working well, But I’d like not to use eval if it’s possible.
is there another way to call autossh directly and give it the correct params without using eval?
-Nalong with each tunnel. BTW, do you consistently tunnel the same ports to a bunch of different servers? If so, how about creating a wildcardHostentry in ~/.ssh/config, with the appropriateLocalForwarddirectives? – Gordon Davisson Sep 10 '19 at 21:41