IP 地址的可用性 - Python

发布于 2024-10-17 22:38:40 字数 328 浏览 2 评论 0原文

如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终止放荡 2024-10-24 22:38:40

这在很多方面都很糟糕,我什至无法解释这有多糟糕。

你为什么想要这个?您能否告诉我们这一点,我们可以想出一个比这个完全丑陋的“解决方案”更好的答案吗?

如果您有 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?

忆依然 2024-10-24 22:38:40

您可以使用 socket.gethostbyaddr() 来查找 IP 地址是否正在使用。

import sys, os, socket

# Stores the IP Address
ip_address = sys.argv[1]

try:
    socket.gethostbyaddr(ip_address)
    # If previous line doesn't throw exception, IP address is being used by someone
    print "No"
except socket.herror:
    # socket.gethostbyaddr() throws error, so IP is not being used at present
    # You can write os.system() and give it ifconfig and route commands as parameter.
    print "Yes"

这段代码的问题是,如果网络上没有使用 IP 地址,method.gethostbyaddr() 会花费大量时间来抛出 socket.herror。

如果将此脚本命名为 isIPAvailable.py,则可以通过以下方式调用它:

python isIPAvailable.py 192.168.112.226

You can use socket.gethostbyaddr() to find if IP Address is being in use or not.

import sys, os, socket

# Stores the IP Address
ip_address = sys.argv[1]

try:
    socket.gethostbyaddr(ip_address)
    # If previous line doesn't throw exception, IP address is being used by someone
    print "No"
except socket.herror:
    # socket.gethostbyaddr() throws error, so IP is not being used at present
    # You can write os.system() and give it ifconfig and route commands as parameter.
    print "Yes"

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:

python isIPAvailable.py 192.168.112.226
苦行僧 2024-10-24 22:38:40

好吧,如果你想使用bash,你可以导入os模块或subprocess模块​​。

例如:

import os
command = os.system('pint 192.168.112.226')
if command == 0: #Sucess
    #write os.system() and give it ifconfig and route commands as parameter.
else: print "This IP is used by another person in your network."

您可以通过导入它们并编写 help(subprocess) 来阅读有关 python 中的 os.system 和 subprocess 的更多信息。

Okay, if you want to use bash, you can import os module or subprocess module.

for example:

import os
command = os.system('pint 192.168.112.226')
if command == 0: #Sucess
    #write os.system() and give it ifconfig and route commands as parameter.
else: print "This IP is used by another person in your network."

you can read more about os.system and subprocess in python, by importing them and writing help(subprocess) for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文