谷动谷力

 找回密码
 立即注册
谷动谷力 首页 电脑IT 查看内容

【Shell】Linux udhcpc动态获取IP命令详解

2023-8-1 15:22| 发布者: sunsili| 查看: 11| 评论: 0|原作者: sunsili

摘要: 【Shell】Linux udhcpc动态获取IP命令详解 前言 由于要使用网络通讯,所以不可避免的要用到dhcp。理想的网络通讯方式是下面3种都要支持:  1,接入已有网络。这便要求可以作为dhcp客户端。  2,作为DHCP服务器, .. ...
【Shell】Linux udhcpc动态获取IP命令详解


前言

由于要使用网络通讯,所以不可避免的要用到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: option requires an argument -- h
BusyBox v1.23.2 (2023-07-24 16:54:35 CST) multi-call binary.

Usage: udhcpc [-fbqRB] [-t N] [-T SEC] [-A SEC/-n]
        [-i IFACE] [-s PROG] [-p PIDFILE]
        [-oC] [-r IP] [-V VENDOR] [-F NAME] [-x OPT:VAL]... [-O OPT]...

        -i,--interface IFACE    Interface to use (default eth0)
        -s,--script PROG        Run PROG at DHCP events (default /usr/share/udhcpc/default.script)
        -p,--pidfile FILE       Create pidfile
        -B,--broadcast          Request broadcast replies
        -t,--retries N          Send up to N discover packets (default 3)
        -T,--timeout SEC        Pause between packets (default 3)
        -A,--tryagain SEC       Wait if lease is not obtained (default 20)
        -n,--now                Exit if lease is not obtained
        -q,--quit               Exit after obtaining lease
        -R,--release            Release IP on exit
        -f,--foreground         Run in foreground
        -b,--background         Background if lease is not obtained
        -S,--syslog             Log to syslog too
        -r,--request IP         Request this IP address
        -o,--no-default-options Don't request any options (unless -O is given)
        -O,--request-option OPT Request option OPT from server (cumulative)
        -x OPT:VAL              Include option OPT in sent packets (cumulative)
                                Examples of string, numeric, and hex byte opts:
                                -x hostname:bbox - option 12
                                -x lease:3600 - option 51 (lease time)
                                -x 0x3d:0100BEEFC0FFEE - option 61 (client id)
        -F,--fqdn NAME          Ask server to update DNS mapping for NAME
        -V,--vendorclass VENDOR Vendor identifier (default 'udhcp VERSION')
        -C,--clientid-none      Don't send MAC as client identifier
Signals:
        USR1    Renew lease
        USR2    Release lease


应用


一、udhcpc -i
udhcpc是一款用于获取DHCP地址的工具。其中,“-i”选项用于指定需要获取DHCP地址的网络接口名称。
sudo udhcpc -i eth0
以上命令表示使用eth0接口获取DHCP地址。

二、udhcpc一直发送discover

在DHCP协议中,客户端首先需要发送discover消息,请求获取ip地址。
udhcpc工具在执行时,会一直发送discover消息,直至获取成功或失败。以下是udhcpc发送discover消息的代码片段。
while (!(udhcp_send_raw(discover_pkt, sizeof(discover_pkt), NULL, PEER_PORT)))
{
    if (no_background) {
        bb_error_msg("sending discover");
    } else {
        bb_info_msg("sending discover");
    }
    sleep(config_timeouts[timeout++]);
    if (timeout >= 7) {
        bb_error_msg("no lease, failing");
        /* Typically, client immediately sends DHCPDECLINE here,
         * and shuts down after that. We don't support it */
        return EXIT_FAILURE;
    }
}

三、udhcpc命令

udhcpc命令可用于在命令行中手动获取DHCP地址。以下是具体的命令使用方法:
sudo udhcpc -i eth0

四、udhcpc参数详解

udhcpc命令还可以使用其他参数来执行特定的功能:
  • -b: 后台执行udhcpc命令。
  • -s: 指定脚本文件,在DHCP成功获取地址后执行该脚本文件。
  • -H: 指定客户端主机名,会在DHCP消息中发送。
  • -p: 指定pid文件,记录udhcpc进程的id。
  • -t: 指定超时时间,单位为秒。

五、dhcpc工具向DHCP服务器发送discover消息


udhcpc sending discover

当udhcpc工具向DHCP服务器发送discover消息时,会进行日志记录。以下是发送discover消息时的日志输出。
...udhcpc: sent discoverudhcpc: sending discoverudhcpc: lease of xxx.xxx.xxx.xxx obtained, lease time xxx...

六、udhcpc无法获取DHCP地址

udhcpc no lease failing

如果udhcpc无法获取DHCP地址,会输出“no lease, failing”的错误信息,并返回EXIT_FAILURE。
udhcpc: no lease, failing
以上就是对udhcpc的详细阐述,并提供相应的命令和代码示例。


鲜花

握手

雷人

路过

鸡蛋

相关阅读

发表评论

最新评论

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2023-12-7 22:24 , Processed in 0.083182 second(s), 32 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

返回顶部