1 #!/usr/bin/env python 2 3 from subprocess import PIPE,Popen 4 import re 5 6 def getIfconfig(): 7 p = Popen(['ifconfig'],stdout=PIPE) 8 data = p.stdout.read().split('\n\n') 9 return [i for i in data if i and not i.startswith('lo')] 10 11 def parseIfconfig(data): 12 re_devname = re.compile(r'(br|eth|lo|em|virbr|bond)[\d:]+') 13 re_mac = re.compile(r'HWaddr ([0-9A-F:]{17})',re.M) 14 re_ip = re.compile(r'inet addr:([\d\.]{7,15})',re.M) 15 devname = re_devname.search(data) 16 if devname: 17 devname = devname.group() 18 else: 19 devname = '' 20 mac = re_mac.search(data) 21 if mac: 22 mac = mac.group(1) 23 else: 24 mac = '' 25 ip =re_ip.search(data) 26 if ip: 27 ip = ip.group(1) 28 else: 29 ip = '' 30 return {devname : [ip,mac]} 31 32 if __name__ == '__main__': 33 data = getIfconfig() 34 for i in data: 35 print parseIfconfig(i)
来源:https://www.cnblogs.com/Nyan-Workflow-FC/p/5690663.html