How to find out if the eth0 mode is static or dhcp?

后端 未结 3 1004
梦毁少年i
梦毁少年i 2020-12-20 19:51

I want to use a C program to get if the ip of the network interface is set manually or via dhcp.

I\'ve tried to use the following code and it has worked in Debian, b

3条回答
  •  余生分开走
    2020-12-20 20:04

    There is AFAIK no definitive way.

    Reading the interfaces file would be a hint only: there is no guarantee that the current seup came from there.

    You could look at 'asking' the DBUS interface if there is one. You could check for a dhclient process running. You could check other files in /etc that specify network setup on different distros.

    I think the most reliable option would be a multi-layered thing: check a whole host of hints to come up with the answer.

    Another option: send a DHCP check packet to the dhcp server to verify the address.. if you don't get an answer though it could be that the network is down but was up when the address was allocated.

提交回复
热议问题