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