- udhcpc命令
udhcpc为busybox里的命令,可用于客户端自动获取ip并设置网关;
Usage: udhcpc [-fbnqvoCRB] [-i IFACE] [-r IP] [-s PROG] [-p PIDFILE]
[-V VENDOR] [-x OPT:VAL]... [-O OPT]...
-i,--interface IFACE Interface to use (default eth0)
-p,--pidfile FILE Create pidfile
-s,--script PROG Run PROG at DHCP events (default /usr/share/udhcpc/default.script)
-B,--broadcast Request broadcast replies
-t,--retries N Send up to N discover packets
-T,--timeout N Pause between packets (default 3 seconds)
-A,--tryagain N Wait N seconds after failure (default 20)
-f,--foreground Run in foreground
-b,--background Background if lease is not obtained
-n,--now Exit if lease is not obtained
-q,--quit Exit after obtaining lease
-R,--release Release IP on exit
-S,--syslog Log to syslog too
-a,--arping Use arping to validate offered address
-O,--request-option OPT Request option OPT from server (cumulative)
-o,--no-default-options Don't request any options (unless -O is given)
-r,--request IP Request this IP address
-x OPT:VAL Include option OPT in sent packets (cumulative)
Examples of string, numeric, and hex byte opts:
-x hostname:bbox - option 12
-x lease:3600 - option 51 (lease time)
-x 0x3d:0100BEEFC0FFEE - option 61 (client id)
-F,--fqdn NAME Ask server to update DNS mapping for NAME
-V,--vendorclass VENDOR Vendor identifier (default 'udhcp VERSION')
-C,--clientid-none Don't send MAC as client identifier
-v
#自动设置eth0网卡ip和网关
#-b 后台运行
#-s 指定配置脚本文件路径,默认路径为/usr/share/udhcpc/default.script
#-i 网卡接口
ifconfig eth0 up && udhcpc -b -s /etc/udhcpc.d/default.script -i eth0
- default.script文件创建及内容添加
创建文件
mkdir /etc/udhcpc.d
touch /etc/udhcpc.d/default.script
添加内容
#!/bin/sh
# udhcpc script edited by Tim Riker <Tim@Rikers.org>
[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1
RESOLV_CONF="/etc/resolv.conf"
[ -n "$subnet" ] && NETMASK="netmask $subnet"
# return 0 if root is mounted on a network filesystem
root_is_nfs() {
sed -n 's/^[^ ]* \([^ ]*\) \([^ ]*\) .*$/\1 \2/p' /proc/mounts |
grep -q "^/ \(nfs\|smbfs\|ncp\|coda\)$"
}
have_bin_ip=0
if [ -x /sbin/ip ]; then
have_bin_ip=1
BROADCAST="broadcast +"
fi
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
case "$1" in
deconfig)
if [ -x /sbin/resolvconf ]; then
/sbin/resolvconf -d "${interface}.udhcpc"
fi
if ! root_is_nfs ; then
if [ $have_bin_ip -eq 1 ]; then
/sbin/ip addr flush dev $interface
/sbin/ip link set dev $interface up
else
/sbin/ifconfig $interface 0.0.0.0
fi
fi
;;
renew|bound)
if [ $have_bin_ip -eq 1 ]; then
/sbin/ip addr add dev $interface local $ip/$mask $BROADCAST
else
/sbin/ifconfig $interface $ip $BROADCAST $NETMASK
fi
if [ -n "$router" ] ; then
if ! root_is_nfs ; then
if [ $have_bin_ip -eq 1 ]; then
while /sbin/ip route del default dev $interface 2>/dev/null ; do
:
done
else
while /sbin/route del default gw 0.0.0.0 dev $interface 2>/dev/null ; do
:
done
fi
fi
metric=10
for i in $router ; do
if [ $have_bin_ip -eq 1 ]; then
/sbin/ip route add default via $i metric $metric
else
/sbin/route add default gw $i dev $interface metric $metric 2>/dev/null
fi
metric=$(($metric + 1))
done
fi
# Update resolver configuration file
R=""
[ -n "$domain" ] && R="domain $domain
"
for i in $dns; do
echo "$0: Adding DNS $i"
R="${R}nameserver $i
"
done
if [ -x /sbin/resolvconf ]; then
echo -n "$R" | /sbin/resolvconf -a "${interface}.udhcpc"
else
echo -n "$R" > "$RESOLV_CONF"
fi
;;
esac
exit 0