问题
How can I programmingly (C) know if given interface has static address or one provided by dhcp? I can look at /var/db/dhclient.leases.<interface_name>
. Any better/cleaner way? any ioctl
I can use?
回答1:
Read /etc/rc.conf
. Look for lines starting with ifconfig
and see which of those contain the text DHCP
.
This will not catch interfaces that were re-configured by running dhclient
manually. Parsing the leases file would work better in that respect, assuming its permessions allow your program to read it.
回答2:
The short answer is no, there isn't. The dhcp client sets the interface address etc. using the same mechanisms that ifconfig uses. There is no special flag or other indication that it leaves to tell you that the interface was dynamically configured.
I'd check the config files in /etc/ rather than the leases, but yes, you're going to have to do something hackish to find out the information.
回答3:
in addition to roland and perry's astute advice, i would add this: if rc.conf is NOT configured to use dhcp, thats doesn't mean it wasn't called from the command line.
if you see that it's running, there's a good chance that's where the interface is getting its address.
pgrep dhclient
来源:https://stackoverflow.com/questions/9489916/freebsd-network-interface-address-dhcp-or-static