Skip to content

Instantly share code, notes, and snippets.

@smoser
Created September 1, 2011 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoser/1186970 to your computer and use it in GitHub Desktop.
Save smoser/1186970 to your computer and use it in GitHub Desktop.
crappy shell script to wait for given mac addrs to have IP addresses
#!/bin/sh
#
# this sits and waits for interfaces passed in on the cmdline
# to come up and have an ipv4 address.
#
# note, this would be much easier done in C, i'm not sure how you
# could avoid polling, surely there is some way.
#
#
# you could even use inotify on /proc/dev/net, as it appears to me to
# ip addr show output like: (wlan0 was copied twice, once up, once down)
#1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
# link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
# inet 127.0.0.1/8 scope host lo
# inet6 ::1/128 scope host
# valid_lft forever preferred_lft forever
#2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
# link/ether 00:22:68:10:c1:e6 brd ff:ff:ff:ff:ff:ff
# inet 192.168.1.102/24 brd 192.168.1.255 scope global eth0
# inet6 fe80::222:68ff:fe10:c1e6/64 scope link
# valid_lft forever preferred_lft forever
#3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
# link/ether 00:1e:65:1f:45:26 brd ff:ff:ff:ff:ff:ff
# inet 192.168.1.101/24 brd 192.168.1.255 scope global wlan0
# inet6 fe80::21e:65ff:fe1f:4526/64 scope link
# valid_lft forever preferred_lft forever
#3: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
# link/ether 00:1e:65:1f:45:26 brd ff:ff:ff:ff:ff:ff
# inet6 fe80::21e:65ff:fe1f:4526/64 scope link
# valid_lft forever preferred_lft forever
#4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
# link/ether 8e:f6:6f:06:c4:44 brd ff:ff:ff:ff:ff:ff
# inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
inargs() {
local needle="$1" hay=""
shift;
for hay in "$@"; do [ "$needle" = "$hay" ] && return 0; done
return 1
}
aremacsup() {
local cur_if="" cur_up=0 cur_inargs=0 cur_good=0 cur_ipv4="" cur_ipv6=""
local inlist="$*"
[ $# -eq 0 ] && return 0
while read line; do
case "$line" in
[0-9]:*|[0-9][0-9]:*|[0-9][0-9][0-9]:*)
[ -z "$cur_if" ] || [ $cur_inargs -eq 0 -o $cur_good -eq 1 ] || break;
set -- ${line}
cur_if=${2%:}
cur_up=0
cur_good=0
cur_ipv4=""
cur_ipv6=""
case "$3" in
*,UP,*|*,UP\>|\<UP,*) cur_up=1;;
esac
inargs $cur_if $inlist && cur_inargs=1 || cur_inargs=0;
esac
[ $cur_inargs -eq 1 ] || continue # we dont care about ifs not in args
[ $cur_good -eq 1 ] && continue
[ $cur_up -eq 1 ] || break; # if its not 'UP' then return false;
case "$line" in
*inet\ *)
set -- $line
[ "${2#*/}" != "$2" ] && cur_ipv4="${2%/*}";;
*inet6\ *) :
## don't know how to do this
#set -- $line
#[ "${2#*/}" != "$2" ] && cur_ipv6="${2%/*}";;
;;
esac
[ -n "$cur_ipv4" -o -n "$cur_ipv6" ] && cur_good=1
done
[ $cur_inargs -eq 0 -o $cur_good -eq 1 ] || { echo "$cur_if is not good"; return 1; }
return 0
}
yes=0
n=0
while ! { ip addr show | aremacsup "$@" && yes=1; } ; do
n=$(($n+1))
[ $n -gt 15 ] && break
sleep 3
done
echo yes=$yes
[ $yes -eq 1 ] && echo "YES" && exit 0 || { echo NO ; exit 1; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment