要使用网络通讯,所以不可避免的要用到dhcp。理想的网络通讯方式是下面3种都要支持:

  1,接入已有网络。这便要求可以作为dhcp客户端。

  2,作为DHCP服务器,动态分配IP。

简单说下前2种情况。

  使用步骤:

  (1)在内核的网络项里面把DHCP配置上;

  (2)在busybox里面把[*]udhcp server(udhcpd)

  [*]udhcp client(udhcpc)都选上。

  udhcpd就是终端设备作为DHCP服务器

  udhcpc就是终端设备作为DHCP客户端

  busybox里面对dhcp都已经给出例子了,

  [zhh@localhost busybox-1.14.1]$ ls ./examples/udhcp/

  sample.bound  sample.deconfig  sample.nak  sample.renew  sample.script  simple.script  udhcpd.conf

  比如使用udhcpc时

  就可以直接把simple.script拿来使用,改不改名字都可以,busybox里面默认的目录文件是/usr/share/udhcpc/default.script

  可以查看下帮助

  # udhcpc --help

  BusyBox v1.14.1 (2010-01-22 10:35:16 CST) multi-call binary

  Usage: udhcpc [-Cfbnqtvo] [-c CID] [-V VCLS] [-H HOSTNAME] [-i INTERFACE]

  [-p pidfile] [-r IP] [-s script] [-O dhcp-option]... [-P N]

  -V CLASSID      Vendor class identifier

  -i INTERFACE    Interface to use (default: eth0)

  -H,-h HOSTNAME  Client hostname

  -c CLIENTID     Client identifier

  -C              Suppress default client identifier

  -p file         Create pidfile

  -r IP           IP address to request

  -s file         Run file at DHCP events (default /usr/share/udhcpc/default.script)

  -t N            Send up to N request packets

  -T N            Try to get a lease for N seconds (default 3)

  -A N            Wait N seconds (default 20) after failure

  -O OPT          Request DHCP option OPT (cumulative)

  -o              Do not request any options (unless -O is also given)

  -f              Run in foreground

  -b              Background if lease is not immediately obtained

  -S              Log to syslog too

  -n              Exit with failure if lease is not immediately obtained

  -q              Quit after obtaining lease

  -R              Release IP on quit

  -P N            Use port N instead of default 68

  -a              Use arping to validate offered address

  #

  这样就很好理解了。

  下面就说说使用udhcpd,同样可以直接把busybox自带的udhcpd.conf作为参考,怎么设置该文件就不详述了,去参考相关文档即可。看看udhcpd怎么使用

  # udhcpd --help

  BusyBox v1.14.1 (2010-01-22 10:35:16 CST) multi-call binary

  Usage: udhcpd [-fS] [-P N] [configfile]

  DHCP server

  -f      Run in foreground

  -S      Log to syslog too

  -P N    Use port N instead of default 67

  帮助一目了然。

  我使用的时候直接

  #udhcpd /etc/udhcpd.conf

  即可。

  还有一个租约文件要简要说说,使用udhcpd时会要求建立一个租约文件,按照提示创建一个就行了,它起什么作用呢?就是为了记录客户端所获得的IP,如果没有租约文件,则会重新给客户端分配一个新IP,如果有,则使用原先分配的IP。

例如:udhcpc -i wlan0 是获取wlan0的ip地址,如果想获取之后直接退出,添加 -q参数,即:udhcpc -q -i wlan0

udhcpc切换到后台

它自带参数可以实现该功能。如下:

udhcpc -b -i eth0 -p /var/run/udhcpc.pid -R

解释一下,-b就是切换到后台指令,-i是指定使用哪个网络接口,双网卡的时候一定要使用它来指定eth0  or  eth1。

对于 -R参数,其实也很重要,在关闭udhcpc时,可以向dhcpserver发送release取消租约。

udhcpc scripts

Here's some debian scripts I wrote for udhcpc 0.9.6. I wanted to make it easy to run local scripts for DHCP changes so I created /etc/udhcpc.d. All the /usr/share/udhcpc/default.* scripts have been merged into /etc/udhcpc.d/S50default. S99local is an example script that patches resolv.conf and calls a DNS update script.

/etc/init.d/udhcpc /etc/udhcpc.script /etc/udhcpc.d/S50default /etc/udhcpc.d/S99local  
  
http://udhcp.busybox.net/README.udhcpc  
udhcp client (udhcpc) --------------------   The udhcp client negotiates a lease with the DHCP server and notifies a set 
of scripts when a leases is obtained or lost. command line options ------------------- The command line options for
the udhcp client are: -c, --clientid=CLIENTID Client identifier -H, --hostname=HOSTNAME Client hostname -h, Alias for -H -f,
--foreground Do not fork after getting lease -b, --background Fork to background if lease cannot be immediately negotiated. -i,
--interface=INTERFACE Interface to use (default: eth0) -n, --now Exit with failure if lease cannot be immediately negotiated. -p,
--pidfile=file Store process ID of daemon in file -q, --quit Quit after obtaining lease -r, --request=IP IP address to request
(default: none) -s, --script=file Run file at dhcp events (default: /usr/share/udhcpc/default.script) -v, --version Display version
If the requested IP address cannot be obtained, the client accepts the address that the server offers. udhcp client scripts
------------------- When an event occurs, udhcpc calls the action script. The script by default is /usr/share/udhcpc/default.script
but this can be changed via the command line arguments. The three possible arguments to the script are: deconfig: This argument
is used when udhcpc starts, and when a leases is lost. The script should put the interface in an up, but deconfigured state, ie:
ifconfig $interface 0.0.0.0. bound: This argument is used when udhcpc moves from an unbound, to a bound state. All of the paramaters
are set in enviromental variables, The script should configure the interface, and set any other relavent parameters (default gateway,
dns server, etc). renew: This argument is used when a DHCP lease is renewed. All of the paramaters are set in enviromental variables.
This argument is used when the interface is already configured, so the IP address, will not change, however, the other DHCP paramaters,
such as the default gateway, subnet mask, and dns server may change. nak: This argument is used with udhcpc receives a NAK message.
The script with the deconfig argument will be called directly afterwards, so no changes to the network interface are neccessary.
This hook is provided for purely informational purposes (the message option may contain a reason for the NAK). The paramaters for
enviromental variables are as follows: $HOME - The set $HOME env or "/" $PATH - the set $PATH env or "/bin:/usr/bin:/sbin:/usr/sbin"
$1 - What action the script should perform interface - The interface this was obtained on ip - The obtained IP siaddr - The bootp next
server option sname - The bootp server name option boot_file - The bootp boot file option subnet - The assigend subnet mask timezone -
Offset in seconds from UTC router - A list of routers timesvr - A list of time servers namesvr - A list of IEN 116 name servers dns -
A list of DNS server logsvr - A list of MIT-LCS UDP log servers cookiesvr - A list of RFC 865 cookie servers lprsvr - A list of LPR
servers hostname - The assigned hostname bootsize - The length in 512 octect blocks of the bootfile domain - The domain name of the
network swapsvr - The IP address of the client's swap server rootpath - The path name of the client's root disk ipttl - The TTL to use
for this network mtu - The MTU to use for this network broadcast - The broadcast address for this network ntpsrv - A list of NTP
servers wins - A list of WINS servers lease - The lease time, in seconds dhcptype - DHCP message type (safely ignored) serverid -
The IP of the server message - Reason for a DHCPNAK tftp - The TFTP server name bootfile - The bootfile name additional options are
easily added in options.c. note on udhcpc's random seed --------------------------- udhcpc will seed its random number generator
(used for generating xid's) by reading /dev/urandom. If you have a lot of embedded systems on the same network, with no entropy, you can
either seed /dev/urandom by a method of your own, or doing the following on startup: ifconfig eth0 > /dev/urandom in order to seed
/dev/urandom with some data (mac address) unique to your system. If reading /dev/urandom fails, udhcpc will fall back to its old behavior
of seeding with time(0). signals accepted by udhcpc ------------------------- udhcpc also responds to SIGUSR1 and SIGUSR2.
SIGUSR1 will force a renew state, and SIGUSR2 will force a release of the current lease, and cause udhcpc to go into an inactive state
(until it is killed, or receives a SIGUSR1). You do not need to sleep between sending signals, as signals received are processed
sequencially in the order they are received. compile time options ------------------- options.c contains a set of dhcp options
for the client: name[10]: The name of the option as it will appear in scripts flags: The type of option, as well as if it will be
requested by the client (OPTION_REQ) code: The DHCP code for this option 使用busybox下的simple.script    
进入busybox目录:busybox1.19.2/example/udhcp/,拷贝文件simple.script到根文件系统的/usr/share/udhcpc/目录下,
更名为default.script,再将default.script中resolv_conf=”/etc/resolv.conf”更改为resolv_conf=”/tmp/resolv.conf”。   
(说明:由于测试使用cramfs文件系统,etc目录不可写,运行udhcpc无法在/etc目录下创建resolv.conf文件。)

udhcpc命令的更多相关文章

  1. udhcpc命令【转】

    udhcpc -i usb0 route 转自:http://blog.csdn.net/hshl1214/article/details/8684740 由于要使用网络通讯,所以不可避免的要用到dh ...

  2. 如何使用Linux命令dhclient

    如何使用Linux命令 dhclient The Internet Systems Consortium DHCP Client, dhclient, provides a means for con ...

  3. udhcpc和udhcpd移植

    实现DHCP自动获取IP地址 前提:系统已经实现DNS(即使用ping www.baidu.com测试时能ping通). 1.  在内核中添加以下选项: Networking  ---> [*] ...

  4. Linux udhcp client (udhcpc) get IP at anytime

    /*************************************************************************************** * Linux udh ...

  5. FL2440 rt3070模块station模式动态获取IP地址

    ---------------------------------------------------------------------------------------------------- ...

  6. arm linux 移植 udhcp 与 使用

    背景 在一些网络环境下,需要静态IP不够现实,需要使用DHCP进行自动获取IP地址. udhcpc是一个面向嵌入式系统的非常小的DHCP客户端,字母的缩写微μ- DHCP -客户端client(μDH ...

  7. iMX287A基于嵌入式Qt的新冠肺炎疫情监控平台

    目录 1.前言 2.数据接口的获取 3.Qt界面的实现 4.在开发板上运行Qt程序 5.最终效果 6.代码下载 @ 1.前言 之前我使用在桌面版本Qt实现了肺炎疫情监控平台:基于Qt的新冠肺炎疫情数据 ...

  8. wifi配置常用命令总结

    1:iwlist eth1 scanning 查看无线路由 2:iwconfig eth1 essid "无线路由的名称" 3: ifconfig eth1 IP 4: route ...

  9. linux下启动和关闭网卡命令及DHCP上网

    ifup.ifdown:如果在 /etc/sysconfig/network-scripts里面的ifcfg-ethx等文件存在的话,就可以通过ifdown或ifup来实现网卡的开和关,例如:ifup ...

随机推荐

  1. 【.Net】net 反射15分钟速成

    概述 什么是反射 Reflection,中文翻译为反射.        这是.Net中获取运行时类型信息的方式,.Net的应用程序由几个部分:‘程序集(Assembly)’.‘模块(Module)’. ...

  2. 【Python】第一篇:python基础_1

    本篇内容 Python介绍 安装 第一个程序(hello,world) 变量 用户输入(input) 数据类型 数据运算 if判断 break和continue的区别 while 循环 一. Pyth ...

  3. BZOJ4881 线段游戏(二分图+树状数组/动态规划+线段树)

    相当于将线段划分成两个集合使集合内线段不相交,并且可以发现线段相交等价于逆序对.也即要将原序列划分成两个单增序列.由dilworth定理,如果存在长度>=3的单减子序列,无解,可以先判掉. 这个 ...

  4. linux硬盘满了问题排查

    关键指令: df du find step1: 如果发现硬盘满了,首先要确定一下,使用df查看硬盘使用情况 df -h step2: 从第一步结果判定满了,确定哪些文件或哪个文件占了大头,使用du指令 ...

  5. CentOS 7下安装pptp服务端手记 ok

    主要配置步骤 1. 安装前检查系统支持 2. 安装必要包 3. 修改相关配置文件 4. 设置开机自动启动 pptpd, iptables 5. iptables配置网络 6. 阿里云ECS可能还需要几 ...

  6. Eclipse开发Java代码,如何添加智能提示

    选择:Window->Preferences->JAVA->Editor->Context Assist 在Auto activation triggers for Java处 ...

  7. BZOJ2216 [Poi2011]Lightning Conductor 【决策单调性dp】

    题目链接 BZOJ2216 题解 学过高中数学都应知道,我们要求\(p\)的极值,参变分离为 \[h_j + sqrt{|i - j|} - h_i \le p\] 实际上就是求\(h_j + sqr ...

  8. Error: Chromium revision is not downloaded. Failed to download Chromium

    在使用prerender-spa-plugin做前端预渲染的时候,安装puppeteer的时候因为下载Chromium 失败报错,有如下解决方法: 1.使用Chromium 国内源 npm confi ...

  9. POJ--2752

    原题链接:http://poj.org/problem?id=2752 分析:no! #include<cstdio> #include<cstring> #include&l ...

  10. 【题解】征途 SDOI 2016 BZOJ 4518

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4518 首先推式子,我们用$x_i$表示第$i$段的路程,$sum$表示总路程,根据方差和平均 ...