Linux ioctl->如何判断当前IP是否是通过dhcp获取的
我正在摆弄套接字 ioctl 来获取当前的接口设置,并且我已经可以获得 IP、接口名称、网络掩码并检查接口是打开还是关闭(我只是对 SIOCGIFCONF、SIOCGIFNETMASK 和 SIOCGIFFLAGS 执行 IOCTl)。
我正在寻找一种方法来判断我当前的 IP 地址是通过 dhcp 获取的还是静态的。
我可以检查 /etc/network/interfaces 中的所有内容,但我正在寻找一种以编程方式执行此操作的方法(这个词存在吗?)。
有人对此有任何见解吗?
另一件事是,我正在 Linux 上工作(目前)。
干杯
I'm fiddling with the sockets ioctl's to get the current interfaces setup and I can already get the IP, interface name, netmask and check if the interface is up or down, (I just do IOCTl to SIOCGIFCONF, SIOCGIFNETMASK and SIOCGIFFLAGS).
I am looking for a way to tell if my current IP address was obtained through dhcp or if it was static.
I can check /etc/network/interfaces for everything I want, but I'm looking for a way to do it programmaticly (does this word exist?).
Does anyone have any insight into this?
One more thing, I'm working on Linux (for now).
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Linux 上有各种各样的 DHCP 客户端 - pump、dhcpcd, dhclient、udhcpc,很可能还有其他我不知道的——这个一般意义上是不可能的。
但是,如果您的目标是特定发行版(例如“Ubuntu 的默认安装”),那么您可以研究 Stefan 等解决方案。请注意,此处列出的所有四个 DHCP 客户端都可以安装在 Ubuntu 上,并且可以替换默认的 DHCP 客户端 dhclient。
With the wide variety of DHCP clients on Linux -- pump, dhcpcd, dhclient, udhcpc, and quite possibly others that I do not know of -- this isn't possible in a general sense.
However, if you are targeting a specific distribution -- say, "default install of Ubuntu" -- then you can investigate solutions such as Stefan's. Note that all four of the DHCP clients listed here can be installed on Ubuntu and can replace the default DHCP client, dhclient.
如果您运行的是 Ubuntu,租约存储在 /var/lib/dhcp3/dhclient-[interface_name].lease 中,也许这就是一个开始。
If you're running Ubuntu, the leases are stored in /var/lib/dhcp3/dhclient-[interface_name].lease, maybe that's a start.
我认为不可能通过内核接口(ioctl)判断 IP 地址是否是通过 DHCP 分配的,就像在大多数发行版中一样 DHCP 是一个用户级应用程序,它只是使用远程源提供的数据来配置内核,就好像用户有手动完成。事实上,如果您查看 ISC dhclient,它只是将从 DHCP 服务器接收到的数据传递给简单的 shell 脚本,这些脚本执行 ifconfig、route 以及您可以作为用户键入的各种其他命令。
因此,您可能必须按照 Stefan 的建议查看特定于您的 DHCP 客户端和发行版的方法。
I don't think its possible to tell via a kernel interface (ioctl) if an IP address was allocated via DHCP as in most distributions DHCP is a userland app that just configures the kernel with data provided by a remote source as if the user had done it manually. In fact, if you look at the ISC dhclient it just passes the data received from the DHCP server to simple shell scripts that do ifconfig, route and various other commands that you could type as a user.
So you'll probably have to look at methods that are specific to your DHCP client and distribution as suggested by Stefan.