Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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/ 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.


postup() {
    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
    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.



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

    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"


    /etc/init.d/"${COMMAND}" -D "${ACTION}"
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

Your Answer


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.