Linux udhcp client (udhcpc) get IP at anytime

简介: /*************************************************************************************** * Linux udhcp client (udhcpc) get IP...
/***************************************************************************************
 *                      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)

 

目录
打赏
0
0
0
0
12
分享
相关文章
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
49 11
Linux ip命令常用操作
Linux的 `ip`命令是一个强大且灵活的网络管理工具,能够执行从基本的网络接口配置到高级的路由和VLAN管理等多种操作。通过熟练掌握这些常用操作,用户可以更加高效地管理和配置Linux系统的网络环境。无论是在日常管理还是故障排除中,`ip`命令都是必不可少的工具。
72 2
Linux系统之部署IP工具箱MyIP
【10月更文挑战第5天】使用Docker部署Radicale日历和联系人应用Linux系统之部署IP工具箱MyIP
203 1
Linux系统之部署IP工具箱MyIP
在Linux中,如何改IP、主机名、DNS?
在Linux中,如何改IP、主机名、DNS?
在Linux中,如何将本地80端口的请求转发到8080端口,当前主机IP为192.168.16.1,其中本地网卡eth0。
在Linux中,如何将本地80端口的请求转发到8080端口,当前主机IP为192.168.16.1,其中本地网卡eth0。
在Linux中,如何查看当前系统每个 IP 的连接数?
在Linux中,如何查看当前系统每个 IP 的连接数?
在Linux中,自定义解析域名的时候,可以编辑哪个⽂件?是否可以⼀个ip对应多个域名?是否⼀个域名对应多个ip?
在Linux中,自定义解析域名的时候,可以编辑哪个⽂件?是否可以⼀个ip对应多个域名?是否⼀个域名对应多个ip?
在Linux中,如何按照该要求抓包:只过滤出访问http服务的,目标ip为192.168.0.111,一共抓1000个包,并且保存到1.cap文件中?
在Linux中,如何按照该要求抓包:只过滤出访问http服务的,目标ip为192.168.0.111,一共抓1000个包,并且保存到1.cap文件中?
在Linux中,如何使用iptables 写⼀条规则?把来源IP为192.168.1.101访问本机80端口的包直接拒绝.
在Linux中,如何使用iptables 写⼀条规则?把来源IP为192.168.1.101访问本机80端口的包直接拒绝.
在Linux中,能否给⼀个网卡配置多个IP? 如果能,怎么配置?
在Linux中,能否给⼀个网卡配置多个IP? 如果能,怎么配置?
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等