On linux,I did that, using fping:
my script:
Code: Select all
#!/bin/bash
#create tmp file:
>/tmp/ping-result
#Define your OpenVPN hosts in HOSTS
HOSTS=( '192.168.20.30' '172.16.30.20' 'server1.example.com' )
ELEMENTS=${#HOSTS[@]}
# echo each element in array
# for loop
for (( i=0;i<$ELEMENTS;i++)); do
RESULT[${i}]=`fping -e ${HOSTS[${i}]}`
echo ${RESULT[${i}]} |awk {'print $4, $1'} |cut -c2-32>>/tmp/ping-result
done
#Now, return the host with the smallest PING
#Or, display DEBUG output
#VPNHOST=`cat /tmp/ping-result |sort -h `
VPNHOST=`cat /tmp/ping-result |sort -h |head -1 |cut -d " " -f2`
echo $VPNHOST
#CLEAN up tmp files:
rm /tmp/ping-result
Now, Using the variable $VPNHOST, You can use that to start your OpenVPN Connect command.
Kind regards,
Frank Uittenbosch
[script edited by dazo, to not use public IP addresses nor official hostname ... some people are stupid enough to run such scripts without modifying them]