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




本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/383281.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/383281.shtml
英文地址,請注明出處:http://en.pswp.cn/news/383281.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c++的STL--1概念通述

STL的概念 什么是STL? STL(standard template libaray-標準模板庫)&#xff1a;是C標準庫的重要組成部分&#xff0c;不僅是一個可復用的組件庫&#xff0c;而且 是一個包羅數據結構與算法的軟件框架。 STL從廣義上分為&#xff1a;容器(container)&#xff0c;算法(algorit…

socket通信和異常處理札記

Linux socket通信出現CLOSE_WAIT狀態的原因與解決方法 這個問題之前沒有怎么留意過&#xff0c;是最近在面試過程中遇到的一個問題&#xff0c;面了兩家公司&#xff0c;兩家公司竟然都面到到了這個問題&#xff0c;不得不使我開始關注這個問題。說起CLOSE_WAIT狀態&#xff0c…

mac 下使用wireshark監聽網絡上的數據

分三個步驟&#xff1a; 1.wireshark安裝 wireshark運行需要mac上安裝X11&#xff0c;mac 10.8的系統上默認是沒有X11的。先去http://xquartz.macosforge.org/landing/下載最新的 xquartz安裝&#xff0c;安裝好就有X11了。 wireshark的下載&#xff0c;網…

c++的vector容器

vector容器概念 vector是表示可變大小數組的序列容器。就像數組一樣&#xff0c;vector也采用的連續存儲空間來存儲元素。也就是意味著可以采用下標對vector的元素 進行訪問&#xff0c;和數組一樣高效。但是又不像數組&#xff0c;它的大小是可以動態改變的&#xff0c;而且它…

嵌入式Linux下3G USB Modem的使用

busybox中需打開&#xff1a;wc&#xff0c;pidof&#xff1b; busybox中shell下打開getopts 百度搜索“Serial connection established. using channel 1”包含大量問題解答 2013-12-22 0個評論 收藏 我要投稿 一.ARM-Linux ARM-Linux-2.6.17 3G USB Modem:hu…

新一代數據庫技術

新一代非關系型數據庫有以下5個主要類型&#xff1a; 面向文件存儲&#xff1a;適用于存儲海量文件&#xff0c;代表產品MongoDb 列存儲(wide column store/column-family)數據庫&#xff1a;快速查找相關數據&#xff0c;相關數據被放在同一列中&#xff0c;代表產品Cassandra…

c++中stack容器

Stack 簡介 stack 是堆棧容器&#xff0c;是一種“先進后出”的容器。stack 是簡單地裝飾 deque 容器而成為另外的一種容器。#include stack沒有迭代器 Stack所有元素的進出都必須符合“先進后出”的條件&#xff0c;只有stack頂端的元素&#xff0c;才有機會被外界取用&am…

詳解udev

如果你使用Linux比較長時間了&#xff0c;那你就知道&#xff0c;在對待設備文件這塊&#xff0c;Linux改變了幾次策略。在Linux早期&#xff0c;設備文件僅僅是是一些帶有適當的屬性集的普通文件&#xff0c;它由mknod命令創建&#xff0c;文件存放在/dev目錄下。后來&#xf…

c++中的queue容器

queue容器 隊列是一種容器適配器&#xff0c;專門用于在FIFO上下文(先進先出)中操作&#xff0c;其中從容器一端插入元素&#xff0c;另一端 提取元素。 隊列作為容器適配器實現&#xff0c;容器適配器即將特定容器類封裝作為其底層容器類&#xff0c;queue提供一組特定的 成員…

NAU8810相關問題

1.ADC和DAC有什么區別&#xff1f; 不&#xff0c;這不是一個“愚弄人的”問題或腦筋急轉彎&#xff0c;并且我認為我們的讀者都非常清楚模數轉換器(ADC)及數模轉換器(DAC)的基本功能。 但在如何使用這些轉換器以及人們的認知度上也存在著哲理性區別。用最簡單的話講&#xff0…

c++中list容器

list概念 list是可以在常數范圍內在任意位置進行插入和刪除的序列式容器&#xff0c;并且該容器可以前后雙向迭代。list的底層是雙向鏈表結構&#xff0c;雙向鏈表中每個元素存儲在互不相關的獨立節點中&#xff0c;在節點中通過指針指向 其前一個元素和后一個元素。list與for…

Linux中rc的含義

在Linux中&#xff0c;最為常用的縮略語也許是“rc”&#xff0c;它是“runcomm”的縮寫――即名詞“run command”(運行命令)的簡寫。rc”是任何腳本類文件的后綴&#xff0c;這些腳本通常在程序的啟動階段被調用&#xff0c;通常是Linux系統啟動時。如/etc/rc&#xff08;連接…

c++中的set容器和multiset容器

set容器基本概念 set的特性是&#xff0c;所有元素都會根據元素的鍵值自動被排序。set的元素不像map那樣可以同時擁有實值和鍵值&#xff0c;set的元素即是鍵值又是實值。set不允許兩個元素又相同的鍵值。我們不可以通過set的迭代器改變set元素的值&#xff0c;因為set元素值就…

linux下的僵尸進程處理SIGCHLD信號

什么是僵尸進程&#xff1f; 首先內核會釋放終止進程(調用了exit系統調用)所使用的所有存儲區&#xff0c;關閉所有打開的文件等&#xff0c;但內核為每一個終止子進程保存了一定量的信息。這些信息至少包括進程ID&#xff0c;進程的終止狀態&#xff0c;以及該進程使用的CPU時…

c++中的map容器

map/multimap基本概念 Map的特性是&#xff0c;所有元素都會根據元素的鍵值自動排序。Map所有的元素都是pair&#xff0c;同時擁有實值和鍵值&#xff0c;pair的第一元素被視為鍵值&#xff0c;第二元素被視為實值&#xff0c;map不允許兩個元素有相同的鍵值我們可以通過map的…

mknod指令詳解

mknod - make block or character special files mknod [OPTION]... NAME TYPE [MAJOR MINOR] option 有用的就是 -m 了 name 自定義 type 有 b 和 c 還有 p 主設備號 次設備號 主設備號是由linux/major.h定義的&#xff0c;如下定義了一個DOC設備&am…

c++中容器(STL)的共性與使用的時機

容器的共通能力 C模板是容器的概念 理論提高&#xff1a;所有容器提供的都是值&#xff08;value&#xff09;語意&#xff0c;而非引用&#xff08;reference&#xff09;語意。容器執 行插入元素的操作時&#xff0c;內部實施拷貝動作。所以 STL 容器內存儲的元素必須能夠被…

Qt Creator 窗體控件自適應窗口大小布局

常見的軟件窗口大小改變&#xff08;最大化、手動改變時&#xff09;需要窗口的部件能夠自適應布局&#xff0c;而在Qt的應用程序界面設計中&#xff0c;對于像我一樣的初學者如何實現窗口自適應調整還是要繞點彎路的。網上百度了很多&#xff0c;多數說的很含糊&#xff0c;還…

c++中利用STL實現公司員工分組問題

#include<iostream>using namespace std; #include<vector> #include<string> #include<map> #include<ctime> /* 公司招聘了5個員工&#xff0c;5名員工進入公司之后&#xff0c;需要指派員工在那個部門工作 的人員信息&#xff1a;姓名 年齡 電…

c++中STL的常用算法--1(函數對象,謂詞,內建函數對象)

函數對象 重載函數調用操作符的類&#xff0c;其對象常稱為函數對象&#xff08;function object&#xff09;&#xff0c;即它們是行為類似函數的對象&#xff0c;也叫仿函數&#xff08;functor&#xff09;&#xff0c;其實就是重載"()"操作符&#xff0c;使得類…