udhcpc命令

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

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

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

  3,指定固定IP

  第3种情况没有什么好说的,简单说下前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是获取wlan0ip地址,如果想获取之后直接退出,添加 -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







 实现DHCP自动获取IP地址 

分类: LINUX


前提:系统已经实现DNS(即使用pingwww.baidu.com测试时能ping通)。

1 在内核中添加以下选项:

Networking  --->

   [*] Networking support  

      Networking options  ---> 

          <* > Packet socket                 //添加.配置CONFIG_PACKET

          [ * ]     IP: DHCP support         //添加

          [ * ] Network packet filtering (replaces ipchains)  --->    //添加,后面子选项可不选,配置CONFIG_NETFILTER

说明:若没选<* > Packet socket, [ * ] Network packet filtering (replaces ipchains)  --->选项,在执行udhcpc命令时出现如下错误:

~ # udhcpc

udhcpc (v0.9.9-pre) started

udhcpc[208]: udhcpc (v0.9.9-pre) started

 

FATAL: couldn't listen on socket, Address family not supported by protocol

udhcpc[208]: FATAL: couldn't listen on socket, Address family not supported by protocol

2  Busybox中添加以下选项:

Networking Utilities  --->

   udhcp Server/Client  --->

        [] udhcp Server (udhcpd)       //在此不作服务端,故不选。生成udhcpd命令                             

        [*] udhcp Client (udhcpc)       //生成udhcpc命令                               

        [ ] Lease display utility (dumpleases)                        

        [ ]   Log udhcp messages to syslog (instead of stdout)        

        [ ]   Compile udhcp with noisy debugging messages

busybox没编译相应选项,也可从网上下载相应文件,用arm-linux交叉编译得到udhcpd,udhcpc命令copyusr/sbin下就可以了。

我从 网上  下的udhcp_0.9.8cvs20050303.orig.tar.gz文件

解压后修改Makefile文件

19行添加CROSS_COMPILE=arm-linux-  

注释12行的COMBINED_BINARY=1,否则不生成udhcpc命令。

3 建相关配置文件

busyboxexamples/udhcp/copy  simple.script文件到开发板/usr/share/udhcpc/下,并重命名为default.scriptudhcp_0.9.8cvs20050303.orig.tar.gz中也有这样的文件。

[root@localhost root]# vi usr/share/udhcpc/default.script

#!/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

 

4 重启开发板,执行udhcpc就可自动获取IP地址了,以下是执行udhcpc的输出信息:

 

~ # udhcpc

udhcpc (v0.9.9-pre) started

udhcpc[228]: udhcpc (v0.9.9-pre) started

 

Sending discover...

udhcpc[228]: Sending discover...

 

Sending select for 192.168.1.109...

udhcpc[228]: Sending select for 192.168.1.109...

 

Lease of 192.168.1.109 obtained, lease time 86400

udhcpc[228]: Lease of 192.168.1.109 obtained, lease time 86400

 

deleting routers

route: SIOC[ADD|DEL]RT: No such process

adding dns 192.168.0.1

~ # ping www.baidu.com

PING www.a.shifen.com (220.181.38.4): 56 data bytes

64 bytes from 220.181.38.4: icmp_seq=0 ttl=52 time=1219.0 ms

[1] + Stopped                    ping www.baidu.com

 

5 如果是双网卡必须用参数指明

例:udhcpc  -i  eth1 






 Linuxhostshost.confresolv.conf的区别

分类: LINUX


这三个文件都位于 /etc/目录下,在这里区分一下

1.hosts文件.

Windows系统下的host的文件相类似,就是一个文本文件,里面存放一些IP和域名的对应关系。

2.host.conf. #解析器查询顺序配置文件

例:vi /etc/host.conf

order hosts bind 表示先查询本地hosts文件,如果没有结果,再尝试查找BIND dns服务器。

3. resolv.conf

配置Linux系统DNS服务器的配置文件:

vi /etc/resolv.conf

nameserver 192.168.0.1

nameserver 202.99.96.68




  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值