Linux C:如何知道Internet访问的默认界面? [英] Linux C: how to know the default interface for internet access?

查看:145
本文介绍了Linux C:如何知道Internet访问的默认界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找出正在使用的默认网络,我当前的方法是查找所有IP地址,并将其与默认网关IP地址进行比较,但这听起来很愚蠢,这是正确的做法吗?

I want to find out the default network in use , my current method was to find out all ip addresses , and compare it to the default gateway ip address , but that sounds silly , that's the correct way of doing it ?

UPDATE

我想使用C程序,而不是命令......

I want to use a C program , not by commands ...

推荐答案

你可以尝试稍微肮脏但更容易的方法:

You can try a slightly dirtier but infinitely easier approach:

cnicutar@lemon:~$ ip route show to 0.0.0.0/0
default via X.Y.Z.T dev eth0  proto static
                        ^^^^

所以你可以尝试:

FILE *cmd = popen("ip route show", "r");
fgets(str, LEN, cmd);

然后你可以使用 strtok strstr 等。

这篇关于Linux C:如何知道Internet访问的默认界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆