I wanted to run a cron job only if a specific interface is really on.
Couldn't find anything in OpenWrt that gives a return code for this.
So I developed a small script for this on Backfire 10.03, but it should also work on Kamikaze 8.09.
Could be adopted to White Russian 0.9 by using "nvram get" and different config names, e.g. "lan_ifname".
Explanation:
#1
Calling ifconfig for the interface shows if the interface is UP and RUNNING, but this doesn't mean that it is connected.
For example an interface with DHCP can be up, but didn't get an IP, so it is not connected.
Therefore the script checks if the interface has an "inet addr".
#2
If checking for interfaces with the names from the uci network config (e.g. "lan" or "wan") then you have to determine what is the real interface for this network.
For example "lan" is normally a bridge, so the correct interface is "br-lan". And "wan" is mostly a pppoe device, so you have to check pppX.
Therefore the script determines the real interface from the uci config.
Current shortcomings of the script:
* Does it work on IPv6-only systems?
* Currently only tested with static, dhcp and pppoe protocol
Suggestions and additions are welcome, will update this first post accordingly.
Maddes
check_ifup.sh: (place in /bin and make it executable with chmod +x /bin/check_ifup.sh)
#!/bin/sh
INTERFACE=$1
[ -z "$INTERFACE" ] && exit 1
IFNAME=
# check for current and actually used interface from /var/state
[ "`uci -P /var/state -q get network.$INTERFACE.up`" == '1' ] && IFNAME=`uci -P /var/state -q get network.$INTERFACE.ifname`
# bridge
[ -z "$IFNAME" ] && {
IFTYPE=`uci -P /var/state -q get network.$INTERFACE.type`
[ "$IFTYPE" = 'bridge' ] && IFNAME="br-$INTERFACE"
}
# special protocol
[ -z "$IFNAME" ] && {
IFPROTO=`uci -P /var/state -q get network.$INTERFACE.proto`
case "$IFPROTO" in
ppp*)
for file in `ls -1 /tmp/.ppp-counter/ppp*`; do
[ `cat "$file"` = "$INTERFACE" ] && { IFNAME=`basename $file`; break; }
done
;;
esac
}
# fallback, use interface as ifname
[ -z "$IFNAME" ] && IFNAME=$INTERFACE
# check if interface is connected
ifconfig $IFNAME 2>/dev/null | grep -q -e '\binet addr:\b'
exit
This is how I use it in a cron job: (used in conjuction with the patch from #7316)
17 * * * * ( INTERFACE=`uci -q get system.@rdate[0].interface` ; check_ifup.sh $INTERFACE && { . /etc/functions.sh ; . /etc/hotplug.d/iface/40-rdate; } )
Updates:
* 2010-05-11 - use -P /var/state for network config
* 2010-05-27 - generalized for all PPP protocols
* 2010-05-27 - value from /var/state takes precedence over normal ifname determination
(Last edited by maddes.b on 27 May 2010, 18:48)