FreeBSD: network interface address: dhcp or static

徘徊边缘 提交于 2019-12-30 10:34:33

问题


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

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