Tweeting OpenBSD Network IP Changes With Minimal Dependencies

August 10, 2011

OpenBSD’s ifstated allows you to run commands on network interface state change.

Add this to your /etc/ifstated.conf:

init-state auto

net_check = '( "ifconfig vr1 | grep \"status: active\" && \
              ifconfig vr1 | grep \"inet *.*.*.* netmask\" | grep -v \"inet 192\" && \
              ping -q -c 3 -i 2 -w 1 www.google.com > /dev/null" every 120)'

state auto {
    if $net_check {
        set-state if_online
    }
    if ! $net_check {
        set-state if_offline
    }
}

state if_online {
    init {
          run "tweet.sh `ifconfig vr1 | grep -e 'inet[^6]' | awk '{print \$2}'`"
    }
    if ! $net_check {
          set-state if_offline
    }
}

state if_offline {
    init {
         run "ifconfig egress | mail -s 'External Interface OFF-line' root@localhost"
    }
    if $net_check {
         set-state if_online
    }
}

Next download tweet.sh from 360percents.com and install curl via pkg-add -r. Adjust the authentication settings inside tweet.sh. Then kill network connection to test.

Thanks to calomel.org for the ifstated.conf template.