Linux udhcp client (udhcpc) get IP at anytime


/***************************************************************************************
 *                      Linux udhcp client (udhcpc) get IP at anytime
 * 聲明:
 *     對於ARM產品,之前在想如何能夠做到像PC機那樣,當網線插入RJ45座的時候,就能自動獲
 * 取到IP,這樣就無需另外去設置IP等相關信息,主要考慮到嵌入式產品有時候可能根本沒有圖形
 * 界面,如果不能自動連接網絡,只能通過調試口去設置網絡,不合情理,直覺認為可能需要依賴
 * udhcpc來做這件事,於是發現了udhcpc命令的-b參數。
 *
 *                                                    2015-10-21 晴 深圳 南山平山村 曾劍鋒
 **************************************************************************************/

一、參考文章:
    1. Busybox Command Help
        http://busybox.net/downloads/BusyBox.html
    2. udhcpc 后台運行的方法【總結】
        http://www.360doc.com/content/12/0323/09/9106558_196861856.shtml
    3. udhcpc running in the background [Summary]
        http://www.programmershare.com/2365338/

二、busybox udhcpc 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

三、udhcpc -b test:
    root@freescale ~$ ifconfig
    eth0      Link encap:Ethernet  HWaddr 1E:ED:19:27:1A:B3  
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    
    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:124 errors:0 dropped:0 overruns:0 frame:0
              TX packets:124 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:400286 (390.9 KiB)  TX bytes:400286 (390.9 KiB)
    root@freescale ~$ udhcpc -b (這里我並沒有用udhcpc -b -i eth0 -p /var/run/udhcpc.pid -R &)
    udhcpc (v1.20.2) started
    Sending discover...
    Sending discover...
    Sending discover...
    No lease, forking to background
    root@freescale ~$ PHY: 1:01 - Link is Up - 100/Full
    root@freescale ~$ ifconfig   (插入網線之后,最好等上2-4秒才能獲取到IP)
    eth0      Link encap:Ethernet  HWaddr 1E:ED:19:27:1A:B3  
              inet addr:10.0.1.52  Bcast:10.0.1.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:4 errors:0 dropped:0 overruns:0 frame:0
              TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:1316 (1.2 KiB)  TX bytes:656 (656.0 B)
    
    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:124 errors:0 dropped:0 overruns:0 frame:0
              TX packets:124 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:400286 (390.9 KiB)  TX bytes:400286 (390.9 KiB)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM