Linux C : How to know the default interface for internet access?… here is a solution to the problem.
Linux C : How to know the default interface for internet access?
I want to find out the default network that is being used. My current approach is to find all IP addresses and compare them to the default gateway IP address, but that sounds silly. What is the right thing to do?
Update
I want to use C programs, not by command….
Solution
You can try a dirtier but simpler method:
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