Skip to content

Instantly share code, notes, and snippets.

@startling
Created March 22, 2012 23:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save startling/2165518 to your computer and use it in GitHub Desktop.
Save startling/2165518 to your computer and use it in GitHub Desktop.
udhcpc config script
#!/bin/sh
# udhcpc config script
# $interface is the name of the device udhcpc is using
# $router is the (space_separated) list of gateways
# $broadcast is the broadcast address
# $ip is the ip address that we get from the dhcp server
# $dns is the list of dns servers we get from the dhcp server
# $domain is the domain of the network
# constants for the resolv.conf and backup files
resolv_conf="/etc/resolv.conf"
resolv_bak="/etc/resolv.backup_dhcp"
case $1 in
deconfig)
# deconfig - put the interface up but deconfigured
# called when udhcpc is started or a lease is lost
# replace resolv.conf with the backup, if the backup exists:
if [ -f "$resolv_bak" ]; then
mv "$resolv_bak" "$resolv_conf"
fi
# set the interface's address to 0.0.0.0 for now
ifconfig $interface 0.0.0.0;;
renew|bound)
# renew - lease is renewed
# bound - move from unbound to bound state
# configure the interface with the given ip, broadcast address, and netmask
ifconfig $interface $ip ${broadcast:+broadcast $broadcast} ${subnet:+netmask $subnet}
# for each given gateway, overwrite the defaults??? hell if I know.
for i in $router; do
route add default gw $i dev $interface
done
#
if [ ! -f "$resolv_bak" ] && [ -f "$resolv_conf" ]; then
mv "$resolv_conf" "$resolv_bak"
fi
# clear `resolv.conf`
echo -n "" > "$resolv_conf"
# if there's a domain given, add it to `resolv.conf`.
if [ -n "$domain" ]; then
echo "search $domain" >> "$resolv_conf"
fi
# for each given dns server, add it to `resolv.conf`.
for i in $dns; do
echo "nameserver $i" >> "$resolv_conf"
done;;
esac
exit 0
# adapted from http://lists.debian.org/debian-boot/2002/11/msg00500.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment