Python gets all of the NIC IP, mask and broadcast address of the machine

Source: Internet
Author: User

Search a day, unexpectedly did not find a suitable code to obtain the machine all the network card IP, mask and broadcast address, most of the socket is used, but the socket is usually returned to the intranet address, or the public network address, can not find all addresses, really too worried Mulberry, Decide yourself to filter by ifconfig or Ipconfig's return information. This time the code mainly uses the regular expression and the Subprocess module, and in order to be compatible with all platforms (Win,linux and Mac), also uses the platform to judge the system type, does not say too many, 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 platfor m = = "Linux": Ip_pattern = Re.compile (' (inet addr:%s) '% ipstr) pattern = Re.compile (IPSTR) Iplis            t = [] 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 = Subproc Ess. Popen ("Ifconfig", stdout=subprocess. PIPE) output = Ipconfig_process.stdout.read () Mask_pattern = Re.compile (' (netmask%s) '% MASKSTR) Pat            Tern = 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 (o utput)): Mask = Pattern.search (Maskaddr.group ()) if Mask.group ()! = ' 255.0.0.0 ': Mask List.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 &am P 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 PLA Tform = = "Linux": Broad_pattern = Re.compile (R ' bcast:%s '% ipstr) pattern = re.compile (ipstr) bro Adlist = [] 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 (Fin D_ALL_IP (System)) print (Find_all_mask (System)) print (Find_all_broad (System))

Hope to help you:)

Python gets all of the NIC IP, mask and broadcast address of the machine

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.