一、设备作为dhcp客户端连接:
udhcpc 由busybox配置系统时,选择网络服务中的udhcpc。
当没有网络的时候,板子一直发送dhcp请求,导致程序不往下执行,解决的办法是把它切换到后台运行,可是如何切换到后台呢,有办法,它自带参数可以实现该功能。如下:
udhcpc -b -i eth0 -p /var/run/udhcpc.pid
解释一下,-b就是切换到后台指令,-i是指定使用哪个网络接口,双网卡的时候一定要使用它来指定eth0 or eth1
-c, --clientid=CLIENTID Client identifier -H, --hostname=HOSTNAME Client hostname -h, Alias for -H -f, --foreground Do not fork after getting lease -b, --background Fork to background if lease cannot be immediately negotiated. -i, --interface=INTERFACE Interface to use (default: eth0) -n, --now Exit with failure if lease cannot be immediately negotiated. -p, --pidfile=file Store process ID of daemon in file -q, --quit Quit after obtaining lease -r, --request=IP IP address to request (default: none) -s, --script=file Run file at dhcp events (default: /usr/share/udhcpc/default.script) -v, --version Display version
调用udhcpc执行的命令:
udhcpc -i eth2 -p eth2.pid -s udhcpc.script -b -a -T 1
-s 后面是执行脚本。执行脚本里面,通过环境变量,获取我们需要的各种网络参数。linux平台下,我们通过如下方法获取网络参数。
可执行脚本通过dhcp的源码dhcpc.c 里面的udhcp_run_script函数调用,函数的第二个参数就是可执行脚本的传入参数。 传入状态有
"deconfig"、"renew"、"bound"等几种。if ($argv[1] == "renew" || $argv[1] == "bound")
{
$netmask=getenv("subnet");
$broadcast=getenv("broadcast");
$interface=getenv("interface");
$ip=getenv("ip");
$router=getenv("router");
$dns=getenv("dns");
$dns_pri="";
$dns_sec="";
}
然后,在脚本中,通过system命令,把ip,broadcast,device,via等信息加入系统中。这样就可以通过dhcp上网了。
关闭udhcpc相关的服务:
从eth*.pid中读出进程号,然后调用kill命令,杀死该进程。 kill *;
二、设备作为dhcp服务器:
(1)、创建dhcpd.conf文件;
(2)、启动dhcp服务时:
首先:rm dhcpd.pid;
然后,开启dhcp服务: dhcpd -4 -cf dhcpd.conf -lf dhcpd.leases -pf dhcpd.pid; 开启dhcpd还可以用service或/etc/init.d/dhcpd命令。
(3)、关闭dhcp服务,killall dhcpd;
以上这种做法是针对单lan的dhcp服务。如果要改成多lan的,可以在禁用某个lan的dhcp服务时,删除全部dhcp.conf文件,并重写conf文件,然后,再重新启动dhcp服务;
另一种做法,熟悉dhcpd的传入参数,针对某个lan口,做对应的dhcp操作(较复杂)。