Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

Prerequesits:

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

#!/bin/bash
TMPFILE=/tmp/._chrissvpn_$RANDOM

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

fi
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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