Question

To test an OpenVPN tunnel setup I need to detect when a newly started OpenVPN daemon has established a tunnel (or tried and failed to do so) before trying to send something over the tunnel.

Était-ce utile?

La solution

Based on an excellent way to timeout tail:

ssh hostname '
    sleep 5 &
    timerPID=$!;
    tail -n0 -F --pid=$timerPID /var/log/messages | grep openvpn | while read -r line;
    do
        if echo "$line" | grep -qF -e "Initialization Sequence Completed" -e "Connection refused";
        then
            kill $timerPID;
            break;
        fi;
    done &
    /etc/init.d/openvpnA start &
    wait $timerPID'

That is:

  1. Connect to the VPN client or server
    1. Start a timer asynchronously
    2. Collect log lines asynchronously containing openvpn until the timer dies
      1. Look for signs that the connection succeeded or was refused
        1. Kill the timer
        2. Exit the loop
    3. Start the daemon
    4. Wait for the timer to die

It seems to work, but being unfamiliar with OpenVPN I'm not sure if this is the correct way to detect the connection status. Since I'm the only one using the machine I think the possibility of a timing issue resulting in the grep succeeding because of an earlier process startup can be ignored. But I'm guessing only someone familiar with the OpenVPN code can really answer this: Is the current test sufficient to establish whether the connection was successful or refused?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top