Python获取本机所有网卡ip,掩码和广播地址实例代码

作者:davelam1991 时间:2021-06-26 20:32:17 

本文主要研究的是使用Python获取本机所有网卡ip,掩码和广播地址,分享了相关的实例代码,具体介绍如下。

搜了一天,竟然没找到一段合适的代码来获取机器中所有网卡的ip,掩码和广播地址,大部分都是用socket,但是socket通常返回的要不就是内网地址,要不就是公网地址,不能够找到所有地址,真的太忧桑了,决定自己通过ifconfig或ipconfig的返回信息,一步步地过滤了。这次的代码主要用到了正则表达式和subprocess模块,而且为了兼容所有平台(win,linux和mac),也用到了platform来判断系统类型,不说太多,代码如下:


import subprocess
import re
import platform

def find_all_ip(platform):
ipstr = '([0-9]{1,3}\.){3}[0-9]{1,3}'
if platform == "Darwin" or platform == "Linux":
 ipconfig_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE)
 output = ipconfig_process.stdout.read()
 ip_pattern = re.compile('(inet %s)' % ipstr)
 if platform == "Linux":
  ip_pattern = re.compile('(inet addr:%s)' % ipstr)
 pattern = re.compile(ipstr)
 iplist = []
 for ipaddr in re.finditer(ip_pattern, str(output)):
  ip = pattern.search(ipaddr.group())
  if ip.group() != "127.0.0.1":
   iplist.append(ip.group())
 return iplist
elif platform == "Windows":
 ipconfig_process = subprocess.Popen("ipconfig", stdout=subprocess.PIPE)
 output = ipconfig_process.stdout.read()
 ip_pattern = re.compile("IPv4 Address(\. )*: %s" % ipstr)
 pattern = re.compile(ipstr)
 iplist = []
 for ipaddr in re.finditer(ip_pattern, str(output)):
  ip = pattern.search(ipaddr.group())
  if ip.group() != "127.0.0.1":
   iplist.append(ip.group())
 return iplist

def find_all_mask(platform):
ipstr = '([0-9]{1,3}\.){3}[0-9]{1,3}'
maskstr = '0x([0-9a-f]{8})'
if platform == "Darwin" or platform == "Linux":
 ipconfig_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE)
 output = ipconfig_process.stdout.read()
 mask_pattern = re.compile('(netmask %s)' % maskstr)
 pattern = re.compile(maskstr)
 if platform == "Linux":
  mask_pattern = re.compile(r'Mask:%s' % ipstr)
  pattern = re.compile(ipstr)
 masklist = []
 for maskaddr in mask_pattern.finditer(str(output)):
  mask = pattern.search(maskaddr.group())
  if mask.group() != '0xff000000' and mask.group() != '255.0.0.0':
   masklist.append(mask.group())
 return masklist
elif platform == "Windows":
 ipconfig_process = subprocess.Popen("ipconfig", stdout=subprocess.PIPE)
 output = ipconfig_process.stdout.read()
 mask_pattern = re.compile(r"Subnet Mask (\. )*: %s" % ipstr)
 pattern = re.compile(ipstr)
 masklist = []
 for maskaddr in mask_pattern.finditer(str(output)):
  mask = pattern.search(maskaddr.group())
  if mask.group() != '255.0.0.0':
   masklist.append(mask.group())
 return masklist

def get_broad_addr(ipstr, maskstr):
iptokens = map(int, ipstr.split("."))
masktokens = map(int, maskstr.split("."))
broadlist = []
for i in range(len(iptokens)):
 ip = iptokens[i]
 mask = masktokens[i]
 broad = ip & mask | (~mask & 255)
 broadlist.append(broad)
return '.'.join(map(str, broadlist))

def find_all_broad(platform):
ipstr = '([0-9]{1,3}\.){3}[0-9]{1,3}'
if platform == "Darwin" or platform == "Linux":
 ipconfig_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE)
 output = (ipconfig_process.stdout.read())
 broad_pattern = re.compile('(broadcast %s)' % ipstr)
 if platform == "Linux":
  broad_pattern = re.compile(r'Bcast:%s' % ipstr)
 pattern = re.compile(ipstr)
 broadlist = []
 for broadaddr in broad_pattern.finditer(str(output)):
  broad = pattern.search(broadaddr.group())
  broadlist.append(broad.group())
 return broadlist
elif platform == "Windows":
 iplist = find_all_ip(platform)
 masklist = find_all_mask(platform)
 broadlist = []
 for i in range(len(iplist)):
  broadlist.append(get_broad_addr(iplist[i], masklist[i]))
 return broadlist

system = platform.system()
print(find_all_ip(system))
print(find_all_mask(system))
print(find_all_broad(system))

来源:http://blog.csdn.net/xanxus46/article/details/44682073

标签:python,ip,掩码
0
投稿

猜你喜欢

  • python中的标准库html

    2022-01-09 22:04:25
  • Java面试题冲刺第十二天--数据库(2)

    2024-01-21 18:34:27
  • 使用Django启动命令行及执行脚本的方法

    2022-03-08 22:10:16
  • 浅谈Python响应式类库RxPy

    2021-12-24 12:44:26
  • Python实现对二维码数据进行压缩

    2022-10-22 12:51:59
  • python实现控制COM口的示例

    2023-01-01 03:57:05
  • python3.x实现base64加密和解密

    2023-12-17 00:30:33
  • 通过屏蔽IP来防止采集

    2007-08-19 15:28:00
  • python中的路径拼接问题

    2021-02-06 11:28:56
  • Flask框架实现的前端RSA加密与后端Python解密功能详解

    2021-07-26 16:28:13
  • 如何用 Python 子进程关闭 Excel 自动化中的弹窗

    2023-10-16 15:47:32
  • 用jquery写的自动提示效果

    2008-06-17 14:27:00
  • 原生JS实现图片轮播 JS实现小广告插件

    2024-04-29 13:55:25
  • javascript 深拷贝

    2024-05-25 15:18:49
  • jQuery 选择器的使用

    2008-01-21 13:07:00
  • 原生JS实现九宫格抽奖效果

    2024-04-17 10:33:38
  • 静态页面分页的AJAX实现

    2008-01-19 16:19:00
  • js动态生成form 并用ajax方式提交的实现方法

    2024-04-17 09:59:54
  • python实现一组典型数据格式转换

    2023-07-14 22:19:04
  • jQuery 让人恋恋不舍的秘密

    2010-01-20 10:09:00
  • asp之家 网络编程 m.aspxhome.com