zoukankan      html  css  js  c++  java
  • ifcfg-eth0文件参数PREFIX 和 NETMASK的配置不一致问题

    ifcfg-eth0文件参数PREFIX 和 NETMASK的配置不一致问题

    摘自:https://blog.csdn.net/aikui0621/article/details/9148997
    阅读数:11549

    2013年06月21日 23:57:26

      
      
      
    搭建一个简单的网络测试环境,现有服务器ip配置为10.131.4.33 掩码为255.255.254.0网关为 10.131.5.254, 需要访问 
    ip为10.1.7.110 的服务器,需要手动配置ifcfg-eth0文件,系统中自带了个PREFIX参数,由于不知道什么用就没有删除,
    结果在修改NETMASK的过程中悲剧就发生了。
    原始系统自带配置文件(为接手此服务器时的配置文件)如下:
    由于不太了解PREFIX参数具体作用,就没有删除,仅修改IPADDR、NETMASK和GATEWAY,修改后如下:
    执行 $services network restart 后 执行 $ ifconfig eth0 会发现netmask 项值并没有更改,而是255.0.0.0
    一直找不到原因,进入图形查看网络设备信息发现 掩码项 为配置值 255.255.254.0 (此处未截图)
    百思不得其解,不知道为什么配置正常后,掩码没有修改,反复检查配置文件后依然无果,无奈之下,写了脚本,
    在重启网卡后,手动键入命令 $ ifconfig eth0 netmask 255.255.254.0 up   发现 $ ifconfig eth0 显示掩码配置正常,
    但是依然ping不通 10.1.7.110的服务器,进入了死胡同,网上查找不到相关问题,最后尝试将配置文件精简到最简,版本如下:
    发现奇迹般的正常了,可以ping同10.1.7.110服务器,问题解决,但是此时一肚子的疑惑, 开始查找原因!
    首先对比发现前后配置文件缺失一个关键选项 PREFIX。回过头来注意此选项怀疑跟掩码有关,经验证果然如此,
    在PREFIX设置8-23之间的值时,$ ifconfig eth0发现 netmask会根据此值相应改变,配置项NETMASK并未生效。但是
    在将PREFIX项配置错误(即NETMASK为255.255.254.0时,PREFIX值在24-32)时,执行 $services network restart 时报错,
    出错信息如下:
    查找到一个相关的帖子,地址如下:
    不过其中有个问题是仅配置PREFIX项不配置NETMASK会出现不稳定问题,未进行测试。现在就剩下当配置PREFIX时,
    NETMASK选项会不生效的问题,先查看 /etc/sysconfig/network-scripts/network-functions 脚本发现如下代码:
    1. 133 expand_config ()
    2. 134 {
    3. 135 if [ -z "${NETMASK}" ]; then
    4. 136 eval `/bin/ipcalc --netmask ${IPADDR}`
    5. 137 fi
    6. 138
    7. 139 if [ -z "${PREFIX}" ]; then
    8. 140 eval `/bin/ipcalc --prefix ${IPADDR} ${NETMASK}`
    9. 141 fi
    10. 142
    11. 143 if [ -z "${BROADCAST}" ]; then
    12. 144 eval `/bin/ipcalc --broadcast ${IPADDR} ${NETMASK}`
    13. 145 fi
    14. 146
    15. 147 if [ -z "${NETWORK}" ]; then
    16. 148 eval `/bin/ipcalc --network ${IPADDR} ${NETMASK}`
    17. 149 fi
    18. 150 }
    发现脚本会读取ifcfg-eth0中配置项,作为ipcalc工具的参数进行配置,接下来查找ipcalc工具源码,发现有prel脚本编写的也有c编写的,
    由于对prel不是很熟悉,贴出C源码如下:
    1. /* vi: set sw=4 ts=4: */
    2. /*
    3. * Mini ipcalc implementation for busybox
    4. *
    5. * By Jordan Crouse <jordan@cosmicpenguin.net>
    6. * Stephan Linz <linz@li-pro.net>
    7. *
    8. * This is a complete reimplementation of the ipcalc program
    9. * from Red Hat. I didn't look at their source code, but there
    10. * is no denying that this is a loving reimplementation
    11. *
    12. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
    13. */
    14. #include "busybox.h"
    15. #include <ctype.h>
    16. #include <getopt.h>
    17. #include <sys/socket.h>
    18. #include <arpa/inet.h>
    19. #define CLASS_A_NETMASK ntohl(0xFF000000)
    20. #define CLASS_B_NETMASK ntohl(0xFFFF0000)
    21. #define CLASS_C_NETMASK ntohl(0xFFFFFF00)
    22. static unsigned long get_netmask(unsigned long ipaddr)
    23. {
    24. ipaddr = htonl(ipaddr);
    25. if ((ipaddr & 0xC0000000) == 0xC0000000)
    26. return CLASS_C_NETMASK;
    27. else if ((ipaddr & 0x80000000) == 0x80000000)
    28. return CLASS_B_NETMASK;
    29. else if ((ipaddr & 0x80000000) == 0)
    30. return CLASS_A_NETMASK;
    31. else
    32. return 0;
    33. }
    34. #ifdef CONFIG_FEATURE_IPCALC_FANCY
    35. static int get_prefix(unsigned long netmask)
    36. {
    37. unsigned long msk = 0x80000000;
    38. int ret = 0;
    39. netmask = htonl(netmask);
    40. while (msk) {
    41. if (netmask & msk)
    42. ret++;
    43. msk >>= 1;
    44. }
    45. return ret;
    46. }
    47. #else
    48. int get_prefix(unsigned long netmask);
    49. #endif
    50. #define NETMASK 0x01
    51. #define BROADCAST 0x02
    52. #define NETWORK 0x04
    53. #define NETPREFIX 0x08
    54. #define HOSTNAME 0x10
    55. #define SILENT 0x20
    56. #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
    57. static const struct option long_options[] = {
    58. { "netmask", no_argument, NULL, 'm' },
    59. { "broadcast", no_argument, NULL, 'b' },
    60. { "network", no_argument, NULL, 'n' },
    61. # if ENABLE_FEATURE_IPCALC_FANCY
    62. { "prefix", no_argument, NULL, 'p' },
    63. { "hostname", no_argument, NULL, 'h' },
    64. { "silent", no_argument, NULL, 's' },
    65. # endif
    66. { NULL, 0, NULL, 0 }
    67. };
    68. #endif
    69. int ipcalc_main(int argc, char **argv)
    70. {
    71. unsigned opt;
    72. int have_netmask = 0;
    73. in_addr_t netmask, broadcast, network, ipaddr;
    74. struct in_addr a;
    75. char *ipstr;
    76. #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
    77. applet_long_options = long_options;
    78. #endif
    79. opt = getopt32(argc, argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs"));
    80. argc -= optind;
    81. argv += optind;
    82. if (opt & (BROADCAST | NETWORK | NETPREFIX)) {
    83. if (argc > 2 || argc <= 0)
    84. bb_show_usage();
    85. } else {
    86. if (argc != 1)
    87. bb_show_usage();
    88. }
    89. if (opt & SILENT)
    90. logmode = LOGMODE_NONE; /* Suppress error_msg() output */
    91. ipstr = argv[0];
    92. if (ENABLE_FEATURE_IPCALC_FANCY) {
    93. unsigned long netprefix = 0;
    94. char *prefixstr;
    95. prefixstr = ipstr;
    96. while (*prefixstr) {
    97. if (*prefixstr == '/') {
    98. *prefixstr = (char)0;
    99. prefixstr++;
    100. if (*prefixstr) {
    101. unsigned msk;
    102. netprefix = xatoul_range(prefixstr, 0, 32);
    103. netmask = 0;
    104. msk = 0x80000000;
    105. while (netprefix > 0) {
    106. netmask |= msk;
    107. msk >>= 1;
    108. netprefix--;
    109. }
    110. netmask = htonl(netmask);
    111. /* Even if it was 0, we will signify that we have a netmask. This allows */
    112. /* for specification of default routes, etc which have a 0 netmask/prefix */
    113. have_netmask = 1;
    114. }
    115. break;
    116. }
    117. prefixstr++;
    118. }
    119. }
    120. ipaddr = inet_aton(ipstr, &a);
    121. if (ipaddr == 0) {
    122. bb_error_msg_and_die("bad IP address: %s", argv[0]);
    123. }
    124. ipaddr = a.s_addr;
    125. if (argc == 2) {
    126. if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
    127. bb_error_msg_and_die("use prefix or netmask, not both");
    128. }
    129. netmask = inet_aton(argv[1], &a);
    130. if (netmask == 0) {
    131. bb_error_msg_and_die("bad netmask: %s", argv[1]);
    132. }
    133. netmask = a.s_addr;
    134. } else {
    135. /* JHC - If the netmask wasn't provided then calculate it */
    136. if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
    137. netmask = get_netmask(ipaddr);
    138. }
    139. if (opt & NETMASK) {
    140. printf("NETMASK=%sn", inet_ntoa((*(struct in_addr *) &netmask)));
    141. }
    142. if (opt & BROADCAST) {
    143. broadcast = (ipaddr & netmask) | ~netmask;
    144. printf("BROADCAST=%sn", inet_ntoa((*(struct in_addr *) &broadcast)));
    145. }
    146. if (opt & NETWORK) {
    147. network = ipaddr & netmask;
    148. printf("NETWORK=%sn", inet_ntoa((*(struct in_addr *) &network)));
    149. }
    150. if (ENABLE_FEATURE_IPCALC_FANCY) {
    151. if (opt & NETPREFIX) {
    152. printf("PREFIX=%in", get_prefix(netmask));
    153. }
    154. if (opt & HOSTNAME) {
    155. struct hostent *hostinfo;
    156. int x;
    157. hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
    158. if (!hostinfo) {
    159. bb_herror_msg_and_die("cannot find hostname for %s", argv[0]);
    160. }
    161. for (x = 0; hostinfo->h_name[x]; x++) {
    162. hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
    163. }
    164. printf("HOSTNAME=%sn", hostinfo->h_name);
    165. }
    166. }
    167. return EXIT_SUCCESS;
    168. }

    阅读源码后发现,选项PREFIX的配置值在此处并未生效,此工具完全根据输入的ip地址和掩码进行分析,应该不会出现ifcfg-eth0中的掩码配置不生效的问题。
    测试环境暂时搭建完成,NETMASK和PREFIX配置冲突问题还未找到合理的解释!!!
  • 相关阅读:
    CUP的MESI协议
    synchronized 锁的升级
    BigDecimal/Long 前后端交互失去精度解决办法
    nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
    根据文件原始名称,文件根路径按照日期生成存储路径
    异步处理MultipartFile -- No such file or directory
    select下拉框相关操作(更新中。。。)
    input清空和重置select下拉框
    sql多字段分组排序显示全部数据
    ajax发送请求下载字节流形式的excel文件
  • 原文地址:https://www.cnblogs.com/LiuYanYGZ/p/9495531.html
Copyright © 2011-2022 走看看