I have several SSH servers I want to connect to that can only be reached via (different) OpenVPN networks.

Now my question is: I have for each network a different set of certificates and sightly varying configurations (*.crt, *.key, *.ovpn). Since my servers can be moved from one network to another (often twice or more per day) I was wondering if there is a simple wrapper for ssh/vpn that allows me to call:

 ssh server1@network.com -ovpn "ovpnfile" -cert "cert.crt"

So that I only have to change the servername each time. That wrapper should automatically tunnel my ssh connection through openvpn and terminate the connection when I close the ssh connection.

Is this possible?



  • Replace every "redirect-gateway" or "route" in your openvpn files with "##SERVERROUTE##"
  • Make sure that you can resolve Hostnames to IP-Adresses


SSHSERVER=`echo $1| cut -d "@" -f 2`

SSHSERVIP=`host $SSHSERVER | head -n 1 | awk '{print $4}'`

cat $2 | sed 's/##SERVERROUTE/$SSHSERVIP/g' > $TMPFILE

openvpn --config $TMPFILE

if [ "$?" -eq "0" ]; then

    ssh $1

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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