OpenWrt Forum Archive

Topic: HOWTO check/detemine/findout status of an interface is up or is down

The content of this topic has been archived on 12 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

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)

After I got some more time for testing, the info from Dogge showed me that OpenWrt stores the current and actually used interface name in /var/state/
If an interface is up, then  OpenWrt stores all real values in /var/state/, values like IP address, DNS and gateway from the DHCP lease, the real connected interface and that is up.
This can be used for an easier and faster determination of the correct interface name.

# ifup wan
# uci -p /var/state show network.wan
network.wan=interface
network.wan.hostname=openwrt
network.wan.macaddr=00:1a:70:a0:38:81
network.wan.proto=pppoe
network.wan.username=XXXX
network.wan.password=XXXX
network.wan.ppp_redial=persist
network.wan.keepalive=10
network.wan.device=wan
network.wan.ifname=ppp0                    <<<<<
network.wan.unit=0
network.wan.ipaddr=84.60.110.68
network.wan.gateway=84.60.96.1
network.wan.dns=195.50.140.246 195.50.140.252
network.wan.up=1
# ifdown wan
# uci -p /var/state show network.wan
network.wan=interface
network.wan.ifname=wan                     <<<<<
network.wan.hostname=openwrt
network.wan.macaddr=00:1a:70:a0:38:81
network.wan.proto=pppoe
network.wan.username=XXXX
network.wan.password=XXXX
network.wan.ppp_redial=persist
network.wan.keepalive=10

Especially note the different .ifname values.

Another enhancement would be to source in /lib/config/uci.sh and use "uci_get_state()" instead of "uci -P /var/state" to work with future changes.

(Last edited by maddes.b on 13 Sep 2010, 19:06)

The discussion might have continued from here.