Light-Weight DHCP ClientBusyBox 這套超強(qiáng)瑞士刀套件在嵌入式系統(tǒng)中幾乎可以說是必備,裡面也不乏網(wǎng)路 service 與 application。前陣子 review code 剛好看到 DHCP client 相關(guān)程式,就順便記錄 busybox 內(nèi)建的 udhcpc 的用法吧! DHCP(Dynamic Host Configuration Protocol) 在一般的區(qū)域網(wǎng)路是相當(dāng)常見的服務(wù),目的就是方便管理區(qū)域網(wǎng)路內(nèi)的裝置,當(dāng)一臺(tái) PC 連上區(qū)域網(wǎng)路時(shí)可向 DHCP Server 自動(dòng)獲取可用 IP 位址,並且取得 subnet mask,gateway,DNS等資訊。技術(shù)細(xì)節(jié)可以參考 wiki 或其他網(wǎng)站,在此就不多說明了。前述所說 PC 在此架構(gòu)之下就是 DHCP Client,需要 DHCP Server 來配發(fā) IP 位址。而如何使用 busybox 內(nèi)建的 DHCP Client (udhcpc) 向 DHCP Server 取得 IP呢?先來看一下 BusyBox 的 Command help: udhcpc [-Cfbnqtvo] [-c CID] [-V VCLS] [-H HOSTNAME] [-i INTERFACE]
[-p pidfile] [-r IP] [-s script] [-O dhcp-option]...
-V,--vendorclass=CLASSID Vendor class identifier
-i,--interface=INTERFACE Interface to use (default eth0)
-H,-h,--hostname=HOSTNAME Client hostname
-c,--clientid=CLIENTID Client identifier
-C,--clientid-none Suppress default client identifier
-p,--pidfile=file Create pidfile
-r,--request=IP IP address to request
-s,--script=file Run file at DHCP events (default /usr/share/udhcpc/default.script)
-t,--retries=N Send up to N request packets
-T,--timeout=N Try to get a lease for N seconds (default 3)
-A,--tryagain=N Wait N seconds (default 20) after failure
-O,--request-option=OPT Request DHCP option OPT (cumulative)
-o,--no-default-options Do not request any options (unless -O is also given)
-f,--foreground Run in foreground
-b,--background Background if lease is not immediately obtained
-S,--syslog Log to syslog too
-n,--now Exit with failure if lease is not immediately obtained
-q,--quit Quit after obtaining lease
-R,--release Release IP on quit
-a,--arping Use arping to validate offered address
忽然看到一長串說明訊息可別嚇到了,只要理解其作用挑選要使用的參數(shù)即可。 若不帶入任何參數(shù),直接執(zhí)行 udhcpc 可能會(huì)出現(xiàn)以下訊息 # udhcpc
udhcpc: ioctl 0x8933 failed: No such device
在預(yù)設(shè)的情況下,udhcpc 會(huì)對(duì) eth0 界面發(fā)送封包,如果該界面不存在,則會(huì)出現(xiàn)此錯(cuò)誤訊息。因次我們需要帶入特定界面(-i interface),同時(shí)希望把訊息也存入到 syslog 裡(-S),並且把 pid 寫入到特定檔案(-p file)。因此可以將命令改為: # udhcpc -i eth1 -S -p /var/run/udhcpc.pid
udhcpc (v1.12.1) started
udhcpc: exec /usr/share/udhcpc/default.script: No such file or directory
Sending select for 192.168.4.50...
Lease of 192.168.4.50 obtained, lease time 120
udhcpc: exec /usr/share/udhcpc/default.script: No such file or directory
udhcpc 順利啟動(dòng)了,也有出現(xiàn)在 process list (ps command)中,但是卻出現(xiàn) udhcpc: exec /usr/share/udhcpc/default.script: No such file or directory 這個(gè)錯(cuò)誤訊息,而且 ifconfig 中的 eth1 界面 IP 並沒有置換為得到的 192.168.4.50 IP,這又是為什麼? 你需要的只是 udhcpc script當(dāng) udhcpc 取得了 IP 位址之後,會(huì)執(zhí)行 script 並把從 DHCP server 得到的資訊帶入該 script 執(zhí)行。因此上面才會(huì)出現(xiàn)找不到預(yù)設(shè) script /usr/share/udhcpc/default.script 的錯(cuò)誤訊息。而這個(gè) script 的內(nèi)容又該如何撰寫呢,我們可以直接拿 buildroot 的 udhcpc script 來參考:
當(dāng) udhcpc 取得 IP 時(shí),就會(huì)執(zhí)行這個(gè) script 並帶入?yún)?shù) 與部份環(huán)境變數(shù)(會(huì)因 DHCP Server 的設(shè)定而有所不同) 聰明的你應(yīng)該已經(jīng)注意到,上面的 script 不過也就是把這些參數(shù)套用到實(shí)際的設(shè)定而已!了解此運(yùn)作方式,要修改 script 符合實(shí)際的應(yīng)用環(huán)境,也只是輕而易舉啦!把該 sctipt 存放到 /etc/udhcpc.sh(看個(gè)人習(xí)慣),重新執(zhí)行命令即可! # udhcpc -i eth1 -p /var/run/udhcpc.pid -S -s /etc/udhcpc.sh udhcpc (v1.12.1) started Sending select for 192.168.4.50... Lease of 192.168.4.50 obtained, lease time 120 deleting routers route: adding dns 192.168.5.2 再次使用 ifconfig 檢查即可發(fā)現(xiàn) IP 位址已經(jīng)被更新了! Renew/Release Current Lease如果 DHCP Server 的設(shè)定改變了,或是想要更新裝置上的網(wǎng)路設(shè)定,難道就必須把 udhcpc 給殺(kill)掉重新啟動(dòng)嗎?這倒是不必了!只要發(fā)送 signal 通知 udhcpc 就可以了! 更新 DHCP 租約(renew DHCP lease): # kill -SIGUSR1 `cat /var/run/udhcpc.pid ` 釋放 DHCP 租約(release current release): # kill -SIGUSR2 `cat /var/run/udhcpc.pid ` |
|
|