Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to fire up openvpn (or stop it) depending on what wireless network I'm connected to.

But dumping env from postup() does not indicate ${SSID} being available at that time.

Staring at net/wpa_supplicant.sh doesn't seem to help, so I'm turning to hivemind for help.

share|improve this question
up vote 0 down vote accepted

I was originally looking to not launch openvpn based on the SSID I was connecting to. But the better solution I arrived at is to look at the IP.

/etc/conf.d/net:

postup() {
    # http://stackoverflow.com/questions/845694/how-do-i-find-my-computers-ip-address-using-the-bash-shell
    IP=$(ifconfig ${IFACE} | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p')
    OPENVPN=$(if [[ "${IFACE}" != "tun0" && ${IP%.*} != 192.168.[13] ]]; then echo "YES"; fi)

    sw-notify-send -i "${ICONS}/network-status.png" "${IFACE}" "$(ip addr show dev ${IFACE} | grep inet)"

    if [[ -n ${OPENVPN} ]]; then
        sw-notify-send -i "${ICONS}/network-status.png" "${IFACE}" "No IP range conflict detected for ${IP}, starting openvpn"
        /etc/init.d/openvpn -D restart
    fi
    return 0
}

EDIT Even further, I've now moved this logic on to a dhcpcd hook. For this to work, you should send some unique dhcp option value from your special DHCP server: I'm using option 114 "default_url" here.

/etc/dhcpcd.enter-hook:

ACTION="stop"
COMMAND="openvpn.noroutes"    

if [ "$reason" = "BOUND" ]; then
    if [ ! -z ${new_default_url} ]; then
        sw-notify-send $ifname "Detected DHCP default_url $new_default_url, skipping openvpn"
        exit 1
    fi

    OPENVPN=$(if [[ $ifname != "tun0" && ${new_network_number%.*} != 192.168.[13] ]]; then echo "YES"; fi)
    if [[ -n $OPENVPN ]]; then
        sw-notify-send $ifname "IP range for $new_ip_address is free, starting openvpn"
        COMMAND="openvpn"
    fi

    ACTION="restart"

    /etc/init.d/"${COMMAND}" -D "${ACTION}"
fi
share|improve this answer
    
I've now moved my VPN start/stop logic into /etc/dhcpcd.enter-hook. – lkraav Dec 25 '12 at 0:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .