一、設備作爲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操作(較複雜)。