home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
tusportal.tus.k12.pa.us
/
tusportal.tus.k12.pa.us.tar
/
tusportal.tus.k12.pa.us
/
Wyse
/
latest-image.raw
/
0.img
/
sbin
/
dhclient-script
< prev
next >
Wrap
Text File
|
2010-05-12
|
15KB
|
441 lines
#!/bin/bash
# dhclient-script for Linux. Dan Halbert, March, 1997.
# Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
# No guarantees about this. I'm a novice at the details of Linux
# networking.
# Notes:
# 0. This script is based on the netbsd script supplied with dhcp-970306.
# 1. ifconfig down apparently deletes all relevant routes and flushes
# the arp cache, so this doesn't need to be done explicitly.
# 2. The alias address handling here has not been tested AT ALL.
# I'm just going by the doc of modern Linux ip aliasing, which uses
# notations like eth0:0, eth0:1, for each alias.
# 3. I have to calculate the network address, and calculate the broadcast
# address if it is not supplied. This might be much more easily done
# by the dhclient C code, and passed on.
# 4. TIMEOUT not tested. ping has a flag I don't know, and I'm suspicious
# of the $1 in its args.
# Debugging:
#
# logs entire run of dhclient-script to /var/log/dhclient-script,
# if DHCLIENT_DEBUG is set in sysconfig/network/dhcp
#
SYSCONFIG_CFG_DIR="/etc/sysconfig/network"
. "$SYSCONFIG_CFG_DIR/scripts/functions"
SYSCONFIG_RUN_DIR=${SYSCONFIG_RUN_DIR:-/dev/.sysconfig/network}
eval `grep "^DHCLIENT_DEBUG=" "$SYSCONFIG_CFG_DIR/dhcp" 2>/dev/null`
if [ "$DHCLIENT_DEBUG" = yes ]; then
set -a # allexport
(
echo '****************'
echo "$0 $*"
date
echo '----------------'
env
echo '----------------'
) >> /var/log/dhclient-script.log
exec 2>> /var/log/dhclient-script.log
set +a
set -x
fi
is_ifup_controlled()
{
test -f "$SYSCONFIG_RUN_DIR/if-${interface}"
}
# netconfig makes all "additional" modifications as DNS or NTP
use_netconfig() {
test -x /sbin/netconfig
}
netconfig_modify() {
if test -x /sbin/netconfig -a -n "$interface" ; then
{
echo "INTERFACE='$interface'"
for v in ${!new_*}; do
case $v in
(new_ip_address) k='IPADDR' ;;
(new_subnet_mask) k='NETMASK' ;;
(new_network_number) k='NETWORK' ;;
(new_broadcast_address) k='BROADCAST' ;;
(new_interface_mtu) k='MTU' ;;
# (new_static_routes) k='ROUTES' ;;
(new_routers) k='GATEWAYS' ;;
(new_host_name) k='HOSTNAME' ;;
(new_domain_search) k='DNSSEARCH' ;;
(new_domain_name) k='DNSDOMAIN' ;;
(new_domain_name_servers) k='DNSSERVERS' ;;
(new_ntp_servers) k='NTPSERVERS' ;;
(new_nis_domain) k='NISDOMAIN' ;;
(new_nis_servers) k='NISSERVERS' ;;
(new_root_path) k='ROOTPATH' ;;
(new_dhcp_server_identifier)k='DHCPSID' ;;
(new_lpr_servers) k='LPRSERVER' ;;
(new_log_servers) k='LOGSERVER' ;;
(new_netbios_dd_server) k='NETBIOSDDSERVER' ;;
(new_netbios_name_servers) k='NETBIOSNAMESERVER' ;;
(new_netbios_node_type) k='NETBIOSNODETYPE' ;;
(new_netbios_scope) k='NETBIOSSCOPE' ;;
(*) k="dhclient_${v#new_}" ;;
esac
[ "k${k}" != k ] && echo "${k}='${!v}'"
done
} | /sbin/netconfig modify -s "dhclient3" -i "$interface"
fi
}
netconfig_remove() {
if test -x /sbin/netconfig -a -n "$interface" ; then
/sbin/netconfig remove -s "dhclient3" -i "$interface" </dev/null
fi
}
set_default_gw_route()
{
if is_ifup_controlled ; then
if grep -qs '^primary=yes' "$SYSCONFIG_RUN_DIR/if-${interface}" 2>/dev/null ;
then
eval `grep --no-filename '^[[:space:]]*DHCLIENT_SET_DEFAULT_ROUTE=' \
"$SYSCONFIG_CFG_DIR/dhcp"
"$SYSCONFIG_CFG_DIR/ifcfg-${interface}" 2>/dev/null`
else
eval `grep --no-filename '^[[:space:]]*DHCLIENT_SET_DEFAULT_ROUTE=' \
"$SYSCONFIG_CFG_DIR/ifcfg-${interface}" 2>/dev/null`
fi
else
eval `grep --no-filename '^[[:space:]]*DHCLIENT_SET_DEFAULT_ROUTE=' \
"$SYSCONFIG_CFG_DIR/dhcp" 2>/dev/null`
fi
if [ "$DHCLIENT_SET_DEFAULT_ROUTE" = yes ] ; then
for router in $new_routers; do
err=`route add default gw $router $metric_arg dev $interface 2>&1` && break || \
case "$err" in
SIOCADDRT:*)
# The gateway seems to be not reachable via local network
# route (implicitely created by ifconfig based on the IP
# an netmask provided by dhcp).
# Check this, set an explicit host route to the gateway
# over the current interface and try again (bnc#266215).
matches=$(test -x /sbin/ip && \
/sbin/ip -f inet -o route list match $router | \
grep -v ^default | grep -c -v "^$" 2>/dev/null)
if test -n "$matches" -a $(($matches)) -eq 0 ; then
route add -host $router dev $interface && \
route add default gw $router $metric_arg dev $interface && break
fi
;;
esac
done
fi
}
set_hostname()
{
if is_ifup_controlled ; then
if grep -qs '^primary=yes' "$SYSCONFIG_RUN_DIR/if-${interface}" 2>/dev/null ;
then
eval `grep --no-filename "^[[:space:]]*DHCLIENT_SET_HOSTNAME=" \
"$SYSCONFIG_CFG_DIR/dhcp"
"$SYSCONFIG_CFG_DIR/ifcfg-${interface}" 2>/dev/null`
else
eval `grep --no-filename "^[[:space:]]*DHCLIENT_SET_HOSTNAME=" \
"$SYSCONFIG_CFG_DIR/ifcfg-${interface}" 2>/dev/null`
fi
else
eval `grep --no-filename "^[[:space:]]*DHCLIENT_SET_HOSTNAME=" \
"$SYSCONFIG_CFG_DIR/dhcp" 2>/dev/null`
fi
if [ "$DHCLIENT_SET_HOSTNAME" = yes ] ; then
current_hostname=`hostname`
if [ "x${current_hostname%%.*}" = "x" ] || \
[ "x${current_hostname%%.*}" = "x(none)" ] || \
[ "x${current_hostname%%.*}" = "xlocalhost" ] || \
[ "x${current_hostname%%.*}" != "x${new_host_name%%.*}" ]; then
if [ "x${new_host_name%%.*}" != "x" ]; then
hostname "${new_host_name%%.*}"
else
if [ -x /usr/bin/host ] ; then
if out=`host -W 2 "$new_ip_address" 2>/dev/null` ; then
_hostname="`echo "$out" | sed 's:^.* ::; s:\..*::'`"
if [ "x${_hostname}" != "x" -a \
"x${_hostname}" != "x${current_hostname%%.*}" ]; then
hostname "${_hostname}"
fi
fi
fi
fi
fi
fi
if is_ifup_controlled ; then
# check regardless the DHCLIENT_SET_HOSTNAME setting
# and whether we've set it above or not, because when
# it changed, we've to handle it anyway...
local OLD_HOSTNAME=`read_cached_config_data hostname $interface`
local CUR_HOSTNAME=`hostname 2>/dev/null`
if test "x$OLD_HOSTNAME" != "x$CUR_HOSTNAME" ; then
write_cached_config_data hostname "$CUR_HOSTNAME" $interface
commit_cached_config_data $interface
# reload syslog so it knows the new hostname
/etc/init.d/syslog reload
fi
fi
}
resolv_conf_dhclient="/var/lib/dhcp/resolv.conf.dhclient.$interface"
make_resolv_conf() {
###
### note: this function is never used by default
### and it has been modified to create the
### /var/lib/dhcp/resolv.conf.dhclient.$interface
### file instead /etc/resolv.conf.
###
if [ x"$new_domain_name_servers" != x ]; then
cat /dev/null > "$resolv_conf_dhclient"
chmod 644 "$resolv_conf_dhclient"
if [ x"$new_domain_search" != x ]; then
echo search $new_domain_search >> "$resolv_conf_dhclient"
elif [ x"$new_domain_name" != x ]; then
# Note that the DHCP 'Domain Name Option' is really just a domain
# name, and that this practice of using the domain name option as
# a search path is both nonstandard and deprecated.
echo search $new_domain_name >> "$resolv_conf_dhclient"
fi
for nameserver in $new_domain_name_servers; do
echo nameserver $nameserver >> "$resolv_conf_dhclient"
done
### mv /etc/resolv.conf.dhclient /etc/resolv.conf
fi
}
restore_resolv_conf()
{
rm -f "$resolv_conf_dhclient"
}
# Must be used on exit. Invokes the local dhcp client exit hooks, if any.
exit_with_hooks() {
exit_status=$1
if [ -f /etc/dhclient-exit-hooks ]; then
. /etc/dhclient-exit-hooks
fi
# probably should do something with exit status of the local script
exit $exit_status
}
# Invoke the local dhcp client enter hooks, if they exist.
if [ -f /etc/dhclient-enter-hooks ]; then
exit_status=0
. /etc/dhclient-enter-hooks
# allow the local script to abort processing of this state
# local script must set exit_status variable to nonzero.
if [ $exit_status -ne 0 ]; then
exit $exit_status
fi
fi
release=`uname -r`
release=`expr $release : '\(.*\)\..*'`
relminor=`echo $release |sed -e 's/[0-9]*\.\([0-9][0-9]*\)\(\..*\)*$/\1/'`
relmajor=`echo $release |sed -e 's/\([0-9][0-9]*\)\..*$/\1/'`
if [ x$new_broadcast_address != x ]; then
new_broadcast_arg="broadcast $new_broadcast_address"
fi
if [ x$old_broadcast_address != x ]; then
old_broadcast_arg="broadcast $old_broadcast_address"
fi
if [ x$new_subnet_mask != x ]; then
new_subnet_arg="netmask $new_subnet_mask"
fi
if [ x$old_subnet_mask != x ]; then
old_subnet_arg="netmask $old_subnet_mask"
fi
if [ x$alias_subnet_mask != x ]; then
alias_subnet_arg="netmask $alias_subnet_mask"
fi
if [ x$new_interface_mtu != x ]; then
if [ $(( $new_interface_mtu )) -lt 576 ] ; then
# 68 is the minimal legal value, but 576 the real life minimum
unset new_interface_mtu
else
mtu_arg="mtu $new_interface_mtu"
fi
fi
if [ x$IF_METRIC != x ]; then
metric_arg="metric $IF_METRIC"
fi
if [ x$reason = xMEDIUM ]; then
# Linux doesn't do mediums (ok, ok, media).
exit_with_hooks 0
fi
if [ x$reason = xPREINIT ]; then
if [ x$alias_ip_address != x ]; then
# Bring down alias interface. Its routes will disappear too.
ifconfig $interface:0- inet 0
fi
if [ $relmajor -lt 2 ] || ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] )
then
ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
broadcast 255.255.255.255 up
# Add route to make broadcast work. Do not omit netmask.
route add default dev $interface netmask 0.0.0.0
else
ifconfig $interface 0 up
fi
# We need to give the kernel some time to get the interface up.
sleep 1
exit_with_hooks 0
fi
if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then
exit_with_hooks 0
fi
if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \
[ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then
if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \
[ x$alias_ip_address != x$old_ip_address ]; then
# Possible new alias. Remove old alias.
ifconfig $interface:0- inet 0
fi
if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then
# IP address changed. Bringing down the interface will delete all routes,
# and clear the ARP cache.
ifconfig $interface inet 0
if is_ifup_controlled ; then
write_cached_config_data dhcp4_state "new" $interface
commit_cached_config_data $interface
fi
elif is_ifup_controlled ; then
write_cached_config_data dhcp4_state "up" $interface
commit_cached_config_data $interface
fi
if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
[ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then
ifconfig $interface inet $new_ip_address $new_subnet_arg \
$new_broadcast_arg $mtu_arg
# Add a network route to the computed network address.
if [ $relmajor -lt 2 ] || \
( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then
route add -net $new_network_number $new_subnet_arg dev $interface
fi
set_default_gw_route
fi
if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ];
then
ifconfig $interface:0- inet 0
ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
route add -host $alias_ip_address $interface:0
fi
if use_netconfig ; then
netconfig_modify
else
make_resolv_conf
fi
if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then
if is_ifup_controlled ; then
ifdown $interface -o dhcp
fi
fi
set_hostname
if is_ifup_controlled ; then
# execute ifservice and if-up.d scripts
ifup $interface -o dhcp
write_cached_config_data dhcp4_state "complete" $interface
commit_cached_config_data $interface
fi
exit_with_hooks 0
fi
if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \
|| [ x$reason = xSTOP ]; then
if [ x$alias_ip_address != x ]; then
# Turn off alias interface.
ifconfig $interface:0- inet 0
fi
if [ x$old_ip_address != x ]; then
# Shut down interface, which will delete routes and clear arp cache.
ifconfig $interface inet 0
fi
if [ x$alias_ip_address != x ]; then
ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
route add -host $alias_ip_address $interface:0
fi
exit_with_hooks 0
fi
if [ x$reason = xTIMEOUT ]; then
if [ x$alias_ip_address != x ]; then
ifconfig $interface:0- inet 0
fi
ifconfig $interface inet $new_ip_address $new_subnet_arg \
$new_broadcast_arg $mtu_arg
set -- $new_routers
if ping -q -c 1 $1; then
if [ x$new_ip_address != x$alias_ip_address ] && \
[ x$alias_ip_address != x ]; then
ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
route add -host $alias_ip_address dev $interface:0
fi
if [ $relmajor -lt 2 ] || \
( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then
route add -net $new_network_number
fi
set_default_gw_route
if use_netconfig ; then
netconfig_modify
else
make_resolv_conf
fi
set_hostname
if is_ifup_controlled ; then
# execute ifservice and if-up.d scripts
ifup $interface -o dhcp
write_cached_config_data dhcp4_state "complete" $interface
commit_cached_config_data $interface
fi
exit_with_hooks 0
fi
ifconfig $interface inet 0
exit_with_hooks 1
fi
if is_ifup_controlled ; then
write_cached_config_data dhcp4_state "down" $interface
commit_cached_config_data $interface
fi
if use_netconfig ; then
netconfig_remove
else
restore_resolv_conf
fi
exit_with_hooks 0