Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1416076
  • 博文数量: 1334
  • 博客积分: 645
  • 博客等级: 上士
  • 技术积分: 5762
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-25 16:56
文章分类

全部博文(1334)

文章存档

2014年(108)

2013年(1059)

2012年(169)

分类:

2012-08-23 16:45:34

原文地址:实现DHCP自动获取IP地址 作者:dingzerong

  1. 实现DHCP自动获取IP地址
  2. 1. 在内核中添加以下选项:
  3. Device Drivers --->
  4. Networking support --->
  5. <*>Networking support
  6.               Networking options --->
  7.                    <*>Packet socket            //添加.配置CONFIG_PACKET
  8.                   [*] IP: DHCP support            //添加
  9.                   [*] Network packet filtering (replaces ipchains) --->
  10. //添加,后面子选项可不选,配置CONFIG_NETFILTER
  11. 说明:若没选 Packet socket,在执行udhcpc命令时出现如下错误:

  12. ~ # udhcpc
  13. udhcpc (v0.9.9-pre) started
  14. udhcpc[208]: udhcpc (v0.9.9-pre) started
  15. FATAL: couldn't listen on socket, Address family not supported by protocol
  16. udhcpc[208]: FATAL: couldn't listen on socket, Address family not supported by protocol
  17. [*] Network packet filtering (replaces ipchains) --->本人选项已测试可以不选

  18. 2. Busybox中添加以下选项:
  19. Networking Utilities --->
  20.    udhcp Server/Client --->
  21. [ ] udhcp Server (udhcpd)    //生成服务端命令udhcpd,在此不作服务端,故不选。
  22.   udhcp Client (udhcpc)        //生成udhcpc命令
  23.    [ ] Lease display utility (dumpleases)
  24.    [ ] Log udhcp messages to syslog (instead of stdout)
  25.    [ ] Compile udhcp with noisy debugging messages
  26. 若busybox没编译相应选项,也可从网上下载相应文件,用arm-linux交叉编译得到udhcpd,udhcpc命令copy到usr/sbin下就可以了。
  27. 从网上下的udhcp_0.9.8cvs20050303.orig.tar.gz文件,解压后修改Makefile文件
  28. 在19行添加CROSS_COMPILE=arm-linux-    (生成ARM版)
  29. 注释12行的COMBINED_BINARY=1        (否则不生成udhcpc命令)

  30. 3. 建相关配置文件
  31. 从busybox的examples/udhcp/下copy simple.script文件到开发板/usr/share/udhcpc/下,并重命名为default.script,udhcp_0.9.8cvs20050303.orig.tar.gz中也有这样的文件。
  32. [root@localhost root]# vi usr/share/udhcpc/default.script
  33. 说明:此步不需要,防止examples/udhcp/simple.script出错
  34. #!/bin/sh
  35. # udhcpc script edited by Tim Riker
  36. [ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1
  37. RESOLV_CONF="/etc/resolv.conf"
  38. [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
  39. [ -n "$subnet" ] && NETMASK="netmask $subnet"
  40. case "$1" in
  41.         deconfig)
  42.                 /sbin/ifconfig $interface 0.0.0.0
  43.                 ;;
  44.         renew|bound)
  45.                 /sbin/ifconfig $interface $ip $BROADCAST $NETMASK
  46.                 if [ -n "$router" ] ; then
  47.                         echo "deleting routers"
  48.                         while route del default gw 0.0.0.0 dev $interface ; do
  49.                                 :
  50.                         done
  51.                         for i in $router ; do
  52.                                 route add default gw $i dev $interface
  53.                         done
  54.                 fi
  55.                 echo -n > $RESOLV_CONF
  56.                 [ -n "$domain" ] && echo search $domain >> $RESOLV_CONF
  57.                 for i in $dns ; do
  58.                         echo adding dns $i
  59.                         echo nameserver $i >> $RESOLV_CONF
  60.                 done
  61.                 ;;
  62. esac
  63. exit 0

  64. 4. 重启开发板,执行udhcpc就可自动获取IP地址了,以下是执行udhcpc的输出信息:
  65. ~ # udhcpc
  66. udhcpc (v0.9.9-pre) started
  67. udhcpc[228]: udhcpc (v0.9.9-pre) started
  68. Sending discover...
  69. udhcpc[228]: Sending discover...
  70. Sending select for 192.168.1.109...
  71. udhcpc[228]: Sending select for 192.168.1.109...
  72. Lease of 192.168.1.109 obtained, lease time 86400
  73. udhcpc[228]: Lease of 192.168.1.109 obtained, lease time 86400
  74. deleting routers
  75. route: SIOC[ADD|DEL]RT: No such process
  76. adding dns 192.168.0.1

  77. ~ # ping www.baidu.com
  78. PING www.a.shifen.com (220.181.38.4): 56 data bytes
  79. 64 bytes from 220.181.38.4: icmp_seq=0 ttl=52 time=1219.0 ms
  80. [1] + Stopped ping www.baidu.com

  81. 5. 如果是双网卡必须用参数指明
  82. 例:udhcpc -i eth1

  83. 6. 运行时的一些注意事项
  84.        busybox中要使用DNS时,要保证/etc/nsswitch.conf[/etc/resolv.conf,/etc/hosts, /etc/host.conf,/etc/hostname]文件存在(其中[]内的内容是否需要没有经过验证,反正有了肯定不会错,呵呵);同时要求 libnss_dns,libnss_files,libresolv库存在。
  85.        busybox中使用udhcpc时,要设置配置文件/usr/share/udhcpc/default.script;把examples/udhcp/simple.script文件copy到目的文件就可以了。
阅读(440) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Linux 驱动学习 ----LED驱动

给主人留下些什么吧!~~