The following script gives an example of a fault safe setup of IP (and IPv6, if it is compiled into the kernel) in the common case of a node attached to a single broadcast network. A more advanced script, which may be used both on multihomed hosts and on routers, is described in the following section.
The utilities used in the script may be found in the directory ftp://ftp.inr.ac.ru/ip-routing/:
ip
-- package iproute2
.
arping
-- package iputils
.
rdisc
-- package iputils
.
dhcpcd
. I should refrain from
recommending a good DHCP client to use. All that I can
say is that ISC dhcp-2.0b1pl6
patched with the patch that
can be found in the dhcp.bootp.rarp
subdirectory of
the same ftp site does work,
at least on Ethernet and Token Ring.
#! /bin/bash
ifone ADDRESS[/PREFIX-LENGTH] [DEVICE]
eth0
is asssumed.
ifone 193.233.7.90
dev=$2 : ${dev:=eth0} ipaddr=# Parse IP address, splitting prefix length.
if [ "$1" != "" ]; then ipaddr=${1%/*} if [ "$1" != "$ipaddr" ]; then pfxlen=${1#*/} fi : ${pfxlen:=24} fi pfx="${ipaddr}/${pfxlen}"
ip link set up dev lo ip addr add 127.0.0.1/8 dev lo brd + scope host
if [ "$dev" = "lo" ]; then if [ "$ipaddr" != "" -a "$ipaddr" != "127.0.0.1" ]; then ip address add $ipaddr dev $dev exit $? fi exit 0 fi
# Step 1 -- enable device $dev
if ! ip link set up dev $dev ; then echo "Cannot enable interface $dev. Aborting." 1>&2 exit 1 fi
UP
. IPv6 started stateless autoconfiguration itself,
if [ "$ipaddr" = "" ]; then echo "No address for $dev is configured, trying DHCP..." 1>&2 dhcpcd exit $? fi
if ! arping -q -c 2 -w 3 -D -I $dev $ipaddr ; then echo "Address $ipaddr is busy, trying DHCP..." 1>&2 dhcpcd exit $? fi
if ! ip address add $pfx brd + dev $dev; then echo "Failed to add $pfx on $dev, trying DHCP..." 1>&2 dhcpcd exit $? fi
# Step 4 -- Announce our presence on the link.
arping -A -c 1 -I $dev $ipaddr noarp=$? ( sleep 2; arping -U -c 1 -I $dev $ipaddr ) >& /dev/null </dev/null &
ip route add unreachable 224.0.0.0/24 ip route add unreachable 255.255.255.255 if [ `ip link ls $dev | grep -c MULTICAST` -ge 1 ]; then ip route add 224.0.0.0/4 dev $dev scope global fi
if [ "$noarp" = "0" ]; then ip ro add default dev $dev metric 30000 scope global fi
killall -HUP rdisc || rdisc -fs exit 0