对于ARM产品,之前在想如何能够做到像PC机那样,当网线插入RJ45座的时候,就能自动获取到IP,这样就无需另外去设置IP等相关信息,主要考虑到嵌入式产品有时候可能根本没有图形界面,如果不能自动连接网络,只能通过调试口去设置网络,不合情理,直觉认为可能需要依赖udhcpc来做这件事,于是发现了udhcpc命令的-b参数。
/***************************************************************************************
* 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)