获取路由信息 /proc/net/route

1、前言

  针对4G,WIFI和有线网络,需要根据参数切换网络路由状态,本文介绍一种通过/proc/net/route 获取路由方法。

2、route命令

  静态路由表

~ # cat /proc/net/route 
Iface   Destination     Gateway         Flags   RefCnt  Use     Metric  Mask            MTU     Window  IRTT                                                       
eth0    00000000        FE00A8C0        0003    0       0       0       00000000        0       0       0                                                                               
eth0    0000A8C0        00000000        0001    0       0       0       00FFFFFF        0       0       0 


~ # route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.254   0.0.0.0         UG    0      0        0 eth0
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

3、实例

static unsigned int  GetDefaultGateway()
{

#define PROC_NET_ROUTE	 "/proc/net/route"
	unsigned int dest = 0;
	unsigned int gateway = 0;
	char buf[256] = {};	
	FILE *fp = NULL;
	char *p = NULL;

	fp = fopen(PROC_NET_ROUTE, "r");
	if(NULL == fp)
	{
		printf("GetDefaultGateway open file: %s failure\n", PROC_NET_ROUTE);
		return 0;
	}

	while(fgets(buf, 255, fp))
	{
		p = strchr(buf, '\t');
		if(NULL == p)
		{
			continue;
		}
		p++;

		sscanf(p, "%X %X", &dest, &gateway);

		if((dest != 0) || (0 == gateway))
		{
			continue;
		}

		fclose(fp);

		return gateway;
	}

	fclose(fp);

	return 0;
}

 

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`/proc/net/route` 文件中的每一行都代表着一条路由记录,包括了目的网络、网关、标志标识等信息。其中,目的网络和网关等信息都是用十六进制表示的。 如果要将 `/proc/net/route` 中的十六进制字符串转换为整数,可以使用 `strtoul` 函数。该函数可以将一个字符串转换为一个无符号长整型数,其函数原型如下: ```c unsigned long int strtoul(const char *nptr, char **endptr, int base); ``` 其中,`nptr` 表示要进行转换的字符串,`endptr` 表示该函数在转换过程中遇到非法字符时停止转换,停止转换的位置会存储在 `endptr` 中,如果 `endptr` 为 `NULL`,则不返回停止转换的位置。`base` 表示要转换的进制数,当 `base` 为 0 时,函数会根据字符串的前缀来判断进制数,例如字符串以 "0x" 开头,则将其视为十六进制数。 以下是一个将 `/proc/net/route` 中的十六进制字符串转换为整数的示例代码: ```c++ #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <cstdlib> int main() { std::ifstream routeFile("/proc/net/route"); if (!routeFile) { std::cerr << "Failed to open /proc/net/route" << std::endl; return -1; } std::vector<std::string> fields; std::string line; while (std::getline(routeFile, line)) { std::istringstream iss(line); std::string field; while (iss >> field) { fields.push_back(field); } if (fields.size() < 11) { std::cerr << "Invalid route record: " << line << std::endl; continue; } std::string destNet = fields[1]; std::string gateway = fields[2]; unsigned long int destNetInt = strtoul(destNet.c_str(), nullptr, 16); unsigned long int gatewayInt = strtoul(gateway.c_str(), nullptr, 16); std::cout << "Destination network: " << destNetInt << std::endl; std::cout << "Gateway: " << gatewayInt << std::endl; fields.clear(); } return 0; } ``` 该程序使用了 `std::ifstream` 类读取 `/proc/net/route` 文件,并使用 `std::istringstream` 类将每一行数据分割成多个字段。然后,对于每个路由记录,将目的网络和网关字段转换为无符号长整型数,并输出到标准输出流中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值