Linux C: How to know the default interface for internet access?

有些话、适合烂在心里 提交于 2020-02-22 07:43:05

问题


I want to find out the default network in use. My current method was to find all IP addresses and compare it to the default gateway IP address, but that sounds silly. What is the correct way of doing it ?

UPDATE

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


回答1:


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
                        ^^^^

So you can try:

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

Then you can use strtok, strstr etc.



来源:https://stackoverflow.com/questions/10513673/linux-c-how-to-know-the-default-interface-for-internet-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!