linux下C编程_获取指定网卡IP信息

自己实现的,不一定通用,其实就是从 ifconfig 结果里面提取信息

# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:03:AD:BC:CC:5A
          inet addr:192.168.100.238  Bcast:192.168.100.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5089 errors:0 dropped:0 overruns:0 frame:0
          TX packets:604 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1226762 (1.1 MiB)  TX bytes:60168 (58.7 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:275 errors:0 dropped:0 overruns:0 frame:0
          TX packets:275 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:12172 (11.8 KiB)  TX bytes:12172 (11.8 KiB)


  23 #define NET_NAME        "eth0"
  24 #define NET_INFO_LEN    20
  25 #define IFCONFIG_CMD    "ifconfig"
  26 #define NET_INFO_BUFF_SIZE 350

  93 /*
  94 *       本地网络信息
  95 */
  96 struct Network {
  97         char ipaddr[NET_INFO_LEN];      /* IP地址 */
  98         char mask[NET_INFO_LEN];        /* 子网掩码 */
  99         char gateway[NET_INFO_LEN];     /* 网关 */
 100         char mac[NET_INFO_LEN];         /* MAC地址*/
 101 };

1721
1722 /*
1723 *       获取本机IP地址 eth0网卡
1724 */
1725 int get_net_info(struct Network *netinfo)
1726 {
1727         /* 待搜索网卡名称 */
1728         char network_name[READ_BUFF_SIZE] = NET_NAME;
1729         char net_info[NET_INFO_BUFF_SIZE] = {0};
1730         char temp[READ_BUFF_SIZE] = {0};
1731         char read_tmp[READ_BUFF_SIZE] = {0};
1732         FILE *fp = NULL;
1733         int i = 0;
1734         int j = 0;
1735 #if 1
1736         fp = popen(IFCONFIG_CMD, "r");
1737         if (fp == NULL) {
1738                 dbg_out(DBG_ERR, "popen failed\n");
1739                 return -1;
1740         } else {
1741                 /* 开始解析 */
1742                 if (fread(net_info, sizeof(char), sizeof(net_info), fp) != sizeof(net_info)) {
1743                         dbg_out(DBG_ERR, "fread %s failed\n", net_info);
1744                         pclose(fp);
1745                         return -1;
1746                 }
1747                 pclose(fp);
1748
1749                 /* 匹配网卡名称 */
1750                 strncpy(temp, net_info, strlen(network_name));
1751                 if (strcmp(temp, network_name) != 0) {
1752                         return -1;
1753                 }
1754                 i = i + strlen(network_name);
1755
1756                 /* 物理地址 HWaddr */
1757                 memset(read_tmp, 0, sizeof(read_tmp));
1758                 memset(temp, 0, sizeof(temp));
1759                 strcat(temp, "HWaddr ");
1760                 while (net_info[i] != '\0') {
1761                         /* 查找"HWaddr "*/
1762                         if (net_info[i] != 'H') {
1763                                 i++;
1764                                 continue;
1765                         }
1766                         for (j=0; j<strlen(temp); j++) {
1767                                 read_tmp[j] = net_info[i];
1768                                 i++;
1769                         }
1770
1771                         /* 相等则拷贝 */
1772                         if (!strcmp(temp, read_tmp)) {
1773                                 for (j=0; j<17; j++) {  /* mac 固定长度 */
1774                                         netinfo->mac[j] = net_info[i];
1775                                         i++;
1776                                 }
1777                                 netinfo->mac[j] = '\0';
1778                                 break;
1779                         } else {
1780                                 dbg_out(DBG_INFO, "find \"HWADDR \" failed\n");
1781                                 return -1;
1782                         }
1783                 }
1784                 dbg_out(DBG_INFO, "MAC: %s\n", netinfo->mac);
1785                 dbg_out(DBG_INFO, "MAC len: %d\n", strlen(netinfo->mac));
1786
1787                 /* ip地址 inet addr: */
1788                 memset(read_tmp, 0, sizeof(read_tmp));
1789                 memset(temp, 0, sizeof(temp));
1790                 strcat(temp, "inet addr:");
1791                 while (net_info[i] != '\0') {
1792                         /* 查找 i */
1793                         if (net_info[i] != 'i') {
1794                                 i++;
1795                                 continue;
1796                         }
1797                         for (j=0; j<strlen(temp); j++) {
1798                                 read_tmp[j] = net_info[i];
1799                                 i++;
1800                         }
1801
1802                         /* 相等则拷贝 */
1803                         if (!strcmp(temp, read_tmp)) {
1804                                 j = 0;
1805                                 while (net_info[i] != ' ') {
1806                                         netinfo->ipaddr[j] = net_info[i];
1807                                         i++;
1808                                         j++;
1809                                 }
1810                                 netinfo->ipaddr[j] = '\0';
1811                                 break;
1812                         } else {
1813                                 dbg_out(DBG_INFO, "find \"ipaddr\" failed\n");
1814                                 return -1;
1815                         }
1816                 }
1817                 dbg_out(DBG_INFO, "ip: %s\n", netinfo->ipaddr);
1818                 dbg_out(DBG_INFO, "ip len: %d\n", strlen(netinfo->ipaddr));
1819
1820                 /* Bcast: */
1821                 memset(read_tmp, 0, sizeof(read_tmp));
1822                 memset(temp, 0, sizeof(temp));
1823                 strcat(temp, "Bcast:");
1824                 while (net_info[i] != '\0') {
1825                         /* 查找 B */
1826                         if (net_info[i] != 'B') {
1827                                 i++;
1828                                 continue;
1829                         }
1830                         for (j=0; j<strlen(temp); j++) {
1831                                 read_tmp[j] = net_info[i];
1832                                 i++;
1833                         }
1834
1835                         /* 相等则拷贝 */
1836                         if (!strcmp(temp, read_tmp)) {
1837                                 j = 0;
1838                                 while (net_info[i] != ' ') {
1839                                         netinfo->gateway[j] = net_info[i];
1840                                         i++;
1841                                         j++;
1842                                 }
1843                                 netinfo->gateway[j] = '\0';
1844                                 break;
1845                         } else {
1846                                 dbg_out(DBG_INFO, "find \"gateway\" failed\n");
1847                                 return -1;
1848                         }
1849                 }
1850                 dbg_out(DBG_INFO, "Bcast: %s\n", netinfo->gateway);
1851                 dbg_out(DBG_INFO, "Bcast len: %d\n", strlen(netinfo->gateway));
1852
1853                 /* 子网掩码 Mask: */
1854                 memset(read_tmp, 0, sizeof(read_tmp));
1855                 memset(temp, 0, sizeof(temp));
1856                 strcat(temp, "Mask:");
1857                 while (net_info[i] != '\0') {
1858                         /* 查找M */
1859                         if (net_info[i] != 'M') {
1860                                 i++;
1861                                 continue;
1862                         }
1863                         for (j=0; j<strlen(temp); j++) {
1864                                 read_tmp[j] = net_info[i];
1865                                 i++;
1866                         }
1867
1868                         /* 相等则拷贝 */
1869                         if (!strcmp(temp, read_tmp)) {
1870                                 j = 0;
1871                                 while (net_info[i] != '\n') {
1872                                         netinfo->mask[j] = net_info[i];
1873                                         i++;
1874                                         j++;
1875                                 }
1876                                 netinfo->mask[j] = '\0';
1877                                 break;
1878                         } else {
1879                                 dbg_out(DBG_INFO, "find \"mask\" failed\n");
1880                                 return -1;
1881                         }
1882                 }
1883                 dbg_out(DBG_INFO, "mask: %s\n", netinfo->mask);
1884                 dbg_out(DBG_INFO, "mask len: %d\n", strlen(netinfo->mask));
1885         }
1886
1887         return 0;
1888 #endif
1889 }




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值