小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

BusyBox 應(yīng)用 – udhcpc | Focus

 xsx2008 2014-07-24

Light-Weight DHCP Client

BusyBox 這套超強(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 來參考:

udhcpc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/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 "$broadcast" ] && BROADCAST="broadcast $broadcast"
[ -n "$subnet" ] && NETMASK="netmask $subnet"
case "$1" in
  deconfig)
    /sbin/ifconfig $interface 0.0.0.0
    ;;
  renew|bound)
    /sbin/ifconfig $interface $ip $BROADCAST $NETMASK
    if [ -n "$router" ] ; then
      echo "deleting routers"
      while route del default gw 0.0.0.0 dev $interface ; do
        :
      done
      for i in $router ; do
        route add default gw $i dev $interface
      done
    fi
    echo -n > $RESOLV_CONF
    [ -n "$domain" ] && echo search $domain >> $RESOLV_CONF
    for i in $dns ; do
      echo adding dns $i
      echo nameserver $i >> $RESOLV_CONF
    done
    ;;
esac
exit 0

當(dāng) udhcpc 取得 IP 時(shí),就會(huì)執(zhí)行這個(gè) script 並帶入?yún)?shù)
bound – 程式啟動(dòng)後第一次向 DHCP Server 取得設(shè)定
renew – 更新 DHCP lease (租約)
deconfig – 釋放租約

與部份環(huán)境變數(shù)(會(huì)因 DHCP Server 的設(shè)定而有所不同)
$router – gateway 位址
$interface – 界面名稱
$ip – 配發(fā)到的 IP 位址
$subnet – 子網(wǎng)域遮罩
$broadcast – 廣播封包位置
$metric – metric 值
$dns – DNS Server 可能會(huì)包含不只一個(gè)
$domain – 網(wǎng)域名稱

聰明的你應(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 `

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多