Python Learning: Collecting IP information using regular

Source: Internet
Author: User

Collecting host information using regular expressions #!/usr/bin/env python from subprocess import Popen, Pipeimport redef getifconfig ():p = Popen ([' ifconfig '], stdout=pipe)data = P.stdout.read (). Split (' \ n ')return [i-I in data if I and not i.startswith (' lo ')]def parseifconfig (data):re_devname = re.compile (R ' br|eth|em|virbr|lo|bond[\d:]+ ', re. M) using regular expressions to collect IP informationRe_mac = re.compile (R ' HWaddr ([0-9a-f:]{17}) ', Re. M)re_ip = re.compile (R ' inet addr: ([\d\.] {7,15}) ', Re. M)devname = re_devname.search (data)If Devname:devname = Devname.group () else:devname = "Mac = Re_mac.search (data) if mac:m AC = Mac.group (1) else:mac = "IP = re_ip.search (data) If Ip:ip = Ip.group (1) else:ip = ' return {devname: [Ip,mac]}if __name__ = = ' __main__ ':dic = {}data = Getifconfig ()For i in data:dic.update (Parseifconfig (i))Print dic[[email protected] day03]# python 13_ip.py{' eth ': [' 192.168.1.200 ', ' 00:0c:29:b7:57:8f ']}

Python Learning: Collecting IP information using regular

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.