Python obtains all Nic ip addresses, mask and broadcast address instance code, and pythonip

Source: Internet
Author: User

Python obtains all Nic ip addresses, mask and broadcast address instance code, and pythonip

This article mainly studies how to use Python to obtain the ip addresses, masks, and broadcast addresses of all NICs on the local machine, and shares the related instance code. The details are as follows.

After searching for a day, I did not find a proper piece of code to obtain the ip addresses, masks, and broadcast addresses of all network adapters on the machine. Most of them use socket, but the socket usually returns an intranet address, if you cannot find all the addresses in the public IP address, you can filter them step by step through the returned information of ifconfig or ipconfig. This code mainly uses regular expressions and subprocess modules. In order to be compatible with all platforms (win, linux, and mac), it also uses platform to judge the system type. The Code is as follows:

import subprocessimport reimport platformdef 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 iplistdef 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 masklistdef 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 broadlistsystem = platform.system()print(find_all_ip(system))print(find_all_mask(system))print(find_all_broad(system))

Summary

The above is all about Python getting the ip addresses, masks, and broadcast addresses of all NICs on the local machine. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.