udhcpc6使用中遇到的问题
和udhcpc一样,udhcpc6是busybox中的一个工具,主要用来提供dhcpv6客户端服务。
在使用过程中遇到了一个问题,直接执行udhcpc6 -i eth0,可以看到打印信息中显示正在发送discover包,如果本地有配置dhcpv6服务器,则显示IPv6 obtained,lease time xxxx,即IPv6地址已经获取,lease time是租约时间,但是没有将获取到的ipv6地址打印出来。
在运行udhcpc时,如果获取ip成功,会有已获取ip的打印,如下图:
看busybox相关部分的源码,确认是udhcpc6代码里没有将ip打印出来,实际是正常获取到了ipv6地址。
无论是udhcpc还是udhcpc6,获取到ip之后,都要执行一个脚本将获得的ip地址配置到设备中,udhcpc可以使用官方文件里给的simple.script,将其改名为default.script后,放到/usr/share/udhcpc/文件夹下,在运行udhcpc的时候会自动去该文件夹下找。还可以在运行时加入-s参数去指定运行脚本。
udhcpc6官方没有提供脚本示例,经测试, 原脚本并不能配置ipv6地址。研究后确定,原因在于原脚本使用的环境变量是$ip,而udhcpc6代码中将获取的ipv6地址配置到环境变量 $ipv6中,只要将原脚本中的 $ip替换为 $ipv6即可使用。
下面放一个我正在使用的udhcpc6脚本,供参考:
#!/bin/sh
# udhcpc6 script edited by Tim Riker <Tim@Rikers.org>
RESOLV_CONF="/etc/resolv.conf"
[ -n "$1" ] || { echo "udhcpc6:Error: should be called from udhcpc"; exit 1; }
NETMASK=""
[ -n "$subnet" ] && NETMASK="netmask $subnet"
BROADCAST="broadcast +"
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
case "$1" in
deconfig)
echo "udhcpc6:Setting IP address 0.0.0.0 on $interface"
# ifconfig $interface 0.0.0.0
;;
renew|bound)
echo "udhcpc6:Setting IP address $ipv6 on $interface"
ifconfig $interface $ipv6 $NETMASK $BROADCAST
if [ $interface != "eth0" ] && [ $interface != "bond0" ] && [ $interface != "wlan0" ] ; then
echo "udhcpc6:$interface is not allowd set router"
exit 0;
else
echo "udhcpc6:set router for $interface"
fi
if [ -n "$router" ] ; then
echo "udhcpc6:Deleting routers"
while route del default gw 0.0.0.0 dev $interface ; do
:
done
metric=0
for i in $router ; do
echo "udhcpc6:Adding router $i"
if [ "$subnet" = "255.255.255.255" ]; then
# special case for /32 subnets:
# /32 instructs kernel to always use routing for all outgoing packets
# (they can never be sent to local subnet - there is no local subnet for /32).
# Used in datacenters, avoids the need for private ip-addresses between two hops.
ip route add $i dev $interface
fi
route add default gw $i dev $interface metric $((metric++))
done
fi
echo "udhcpc:Recreating $RESOLV_CONF"
tmpfile="$RESOLV_CONF-$$"
> "$tmpfile"
for i in $dns ; do
echo "udhcpc: Adding DNS server $i"
echo "nameserver $i" >> "$tmpfile"
done
mv "$tmpfile" "$RESOLV_CONF"
;;
esac
exit 0