[英]Retrieving network mask in Python
如何检索网络设备的网络掩码(最好在 Linux 中,但如果它是跨平台的,那就太酷了)? 我知道如何在 Linux 上使用 C,但我在 Python 中找不到方法——也许减去 ctypes。 那或解析ifconfig。 还有什么办法吗?
ioctl(socknr, SIOCGIFNETMASK, &ifreq) // C version
这在 Linux 上的 Python 2.2 中对我有用:
iface = "eth0"
socket.inet_ntoa(fcntl.ioctl(socket.socket(socket.AF_INET, socket.SOCK_DGRAM), 35099, struct.pack('256s', iface))[20:24])
netifaces
模块在这里值得一提。 直接来自文档:
>>> netifaces.interfaces()
['lo0', 'gif0', 'stf0', 'en0', 'en1', 'fw0']
>>> addrs = netifaces.ifaddresses('en0')
>>> addrs[netifaces.AF_INET]
[{'broadcast': '10.15.255.255', 'netmask': '255.240.0.0', 'addr': '10.0.1.4'}, {'broadcast': '192.168.0.255', 'addr': '192.168.0.47'}]
适用于 Windows、Linux、OS X 和其他 UNIX。
你看这里了吗?
http://docs.python.org/library/fcntl.html
这在 Linux 上的 python 2.5.2 中对我有用。 在完成它的时候了奔前方,但仍这里有云(悲伤浪费精力:-)):
vinko@parrot:~$ more get_netmask.py
# get_netmask.py by Vinko Vrsalovic 2009
# Inspired by http://code.activestate.com/recipes/439093/
# and http://code.activestate.com/recipes/439094/
# Code: 0x891b SIOCGIFNETMASK
import socket
import fcntl
import struct
import sys
def get_netmask(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x891b, struct.pack('256
s',ifname))[20:24])
if len(sys.argv) == 2:
print get_netmask(sys.argv[1])
vinko@parrot:~$ python get_netmask.py lo
255.0.0.0
vinko@parrot:~$ python get_netmask.py eth0
255.255.255.0
你可以使用这个库: http : //github.com/rlisagor/pynetlinux 。 注意:我是图书馆的作者。
我的想法是依靠子进程使用简单的 ifconfig (Linux) 或 ipconfig (windows) 请求来检索信息(如果 ip 已知)。 欢迎评论:
视窗
ip = '192.168.1.10' #Example
proc = subprocess.Popen('ipconfig',stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if ip.encode() in line:
break
mask = proc.stdout.readline().rstrip().split(b':')[-1].replace(b' ',b'').decode()
类 UNIX
ip = '192.168.1.10' #Example
proc = subprocess.Popen('ifconfig',stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if ip.encode() in line:
break
mask = line.rstrip().split(b':')[-1].replace(b' ',b'').decode()
使用套接字连接到 Web 并使用getsockname()[0]
检索 IP
在 Windows 中,这段代码可能很有用:
import os
import sys
import _winreg
def main():
adapter_list_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards')
adapter_count = _winreg.QueryInfoKey(adapter_list_key)[0]
for i in xrange(adapter_count):
sub_key_name = _winreg.EnumKey(adapter_list_key, i)
adapter_key = _winreg.OpenKey(adapter_list_key, sub_key_name)
(adapter_service_name, _) = _winreg.QueryValueEx(adapter_key, "ServiceName")
(description, _) = _winreg.QueryValueEx(adapter_key, "Description")
adapter_registry_path = os.path.join(r'SYSTEM\ControlSet001\Services',
adapter_service_name, "Parameters", "Tcpip")
adapter_service_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
adapter_registry_path)
(subnet_mask, _) = _winreg.QueryValueEx(adapter_service_key, "SubnetMask")
(ip_address, _) = _winreg.QueryValueEx(adapter_service_key, "IpAddress")
sys.stdout.write("Name: %s\n" % adapter_service_name)
sys.stdout.write("Description: %s\n" % description)
sys.stdout.write("SubnetMask: %s\n" % subnet_mask)
sys.stdout.write("IpAdress: %s\n" % ip_address)
if __name__ == "__main__":
main()
从HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards
注册表项中获取网络适配器列表,然后从HKLM\\SYSTEM\\ControlSet001\\Services\\{adapter_guid}\\Parameters\\Tcpip
项中提取有关每个适配器的更多信息。
我在带有 2 个虚拟适配器的 Windows XP 上对其进行了测试,它运行良好。 应该也适用于 2000、2003 和 Vista。
使用 pythonpyrute2 库,您可以获得所有网络元素属性:
from pyroute2 import IPRoute
ip = IPRoute()
info = [{'iface': x['index'], 'addr': x.get_attr('IFA_ADDRESS'), 'mask': x['prefixlen']} for x in ip.get_addr()]
此处提供更多信息: http : //pyroute2.org/pyroute2-0.3.14p4/iproute.html
我将原始答案的更新作为原始代码:
struct.pack('256s', iface)
在 Python 3.9 中引发错误,所以这里是一个重新访问的版本:
SIOCGIFADDR = 0x8915
def get_iface_ip(iface: str):
"""
Get network interface IP using the network interface name
:param iface: Interface name (like eth0, enp2s0, etc.)
:return IP address in the form XX.XX.XX.XX
"""
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
iface_bin = struct.pack('256s', bytes(iface, 'utf-8'))
packet_ip = fcntl.ioctl(s.fileno(), SIOCGIFADDR, iface_bin)[20:24]
return socket.inet_ntoa(packet_ip)
您可以通过在 CMD 上运行命令并提取相关信息来实现此目的。
我的实现:
import subprocess
p = subprocess.Popen('ipconfig', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
if b'Subnet Mask' in line:
print(line.decode().split(' ')[-1].strip('\r\n'))
从文档
ipaddress提供了在 IPv4 和 IPv6 地址和网络上创建、操作和操作的功能。
该模块中的函数和类使处理与 IP 地址相关的各种任务变得简单......
>>> import socket
>>> import ipaddress
>>>
>>> ip_addr = socket.gethostbyname(socket.gethostname())
>>> netmask = ipaddress.IPv4Network(ip_addr).netmask
>>> print(netmask)
255.255.255.255
>>> ...
这个可能的问题可以在这里回答。 这不是同一个问题,但也为您的问题提供了解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.