IP 地址的可用性 - Python
如何在Python中检查IP地址的可用性?
例如,我不想将系统的 IP 地址更改为 192.168.112.226,静态覆盖 dhcp 提供的地址。默认网关是 192.168.112.1。但我不想在分配给自己之前检查是否有人在使用 192.168.112.226。
通常在 bash 命令行中执行此操作。我用 ping 192.168.112.226 检查。如果主机无法访问,我会使用“ifconfig”和“route”将其分配给自己。
如何使用 python 实现自动化?
PS:我更喜欢python,这样无论成功还是失败,我都可以使用python-notify来美化输出。
How to check the availability of an IP address in python?
For example, I wan't to change my system's IP address to 192.168.112.226 statically overriding the dhcp provided address. The default gateway is 192.168.112.1. But I wan't to check before if anyone is using 192.168.112.226 before assigning to myself.
Usually do this in command line from bash. I check with ping 192.168.112.226. If host is unreachable, I use 'ifconfig' and 'route' to assign it to myself.
How to automate this using python?
PS: I prefer python so that I can use python-notify to beautify the output whether success or failure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这在很多方面都很糟糕,我什至无法解释这有多糟糕。
你为什么想要这个?您能否告诉我们这一点,我们可以想出一个比这个完全丑陋的“解决方案”更好的答案吗?
如果您有 Linux/Unix 系统,您可以让 DHCP 客户端请求 DHCP 服务器为您提供特定的 IP 地址(如果 DHCP 服务器知道该地址是空闲的)。如何执行此操作取决于发行版。
我发现您将用您的“解决方案”创建两个问题。
正如其他人所写的,您可以检查该 IP 现在是否“空闲”,但拥有该 IP 地址的计算机可能会在您测试后立即启动。使用您绑架的 IP 地址。
如果 DHCP 服务器不知道您已经绑架了 IP 地址,它可能会将其提供给其他人。
无论什么都会破坏该计算机和您的网络,产生大量工作,并可能对网络管理员产生愤怒。你不希望这样,是吗?
This is so bad in so many ways I can't even explain how awfull this is.
Why do you want this? Could you please tell us that, and we could come up with a much better answer than this utterly uggly "sollution"?
If you have a Linux/Unix system, you can make your DHCP client to request the DHCP-server to give you a specific IP address if the DHCP server know it's free. How to do this depends on the distribution.
There are two problems I see that you will create with your "sollution".
As some other has written, you could check to see that the IP is "free" right now, but the machine that own that IP address might start right after your test. Using its IP address, wich you have kidnapped.
If the DHCP server don't know that you have kidnapped an IP address, it could give it out to someone else.
Whatever it will break the network for that computer and yours, generating lots of work, and possible anger for/to the network administrator. And you don't want that, do you?
您可以使用 socket.gethostbyaddr() 来查找 IP 地址是否正在使用。
这段代码的问题是,如果网络上没有使用 IP 地址,method.gethostbyaddr() 会花费大量时间来抛出 socket.herror。
如果将此脚本命名为 isIPAvailable.py,则可以通过以下方式调用它:
You can use socket.gethostbyaddr() to find if IP Address is being in use or not.
The problem with this code is that method.gethostbyaddr() takes lot of time to throw socket.herror if IP address is not in use on the network.
If you name this script as isIPAvailable.py, then it can be called by:
好吧,如果你想使用bash,你可以导入os模块或subprocess模块。
例如:
您可以通过导入它们并编写
help(subprocess)
来阅读有关 python 中的 os.system 和 subprocess 的更多信息。Okay, if you want to use bash, you can import os module or subprocess module.
for example:
you can read more about os.system and subprocess in python, by importing them and writing
help(subprocess)
for example.