Collecting IP information using python
[[email protected] systeminformation]# vim devname_1.py #!/usr/bin/env Pythonfrom subprocess import popen,pipedef getifconfig (): tuple_ addr = (' lo ', ' vir ', ' vnet ', ' em3 ', ' em4 ') #有些网卡以这些字符开头, excluding p = Popen ([' ifconfig '], stdout=pipe) data = p.stdout.read (). Split (' \ n ') return [i for i in data if i and not i.startswith (tuple_addr)]# excluded Def parseifconfig (data): dic = { } for lines in data: line_list = lines.split (' \ n ') devname = line_list[0].split () [0] macaddr = line_list[0]. Split () [ -1] &Nbsp; ipaddr = line_list[1].split () [1].split (': ') [1] print devname, macaddr, ipaddr # print out the NIC name,mac,ip dic [devname] = [ipaddr, macaddr] return dicif __name__ == ' __main__ ': data = getifconfig () print parseifconfig (data) [[email protected] systeminformation]# python devname_1.py br1 a4:ba:db:20:93:23 112.65.140.133docker0 00:00:00:00:00:00 172.17.42.1em1 a4:ba:db:20:93:23 em2 a4:ba:db:20:93:25 192.168.101.237{' Docker0 ': [' 172.17.42.1 ', ' 00:00:00:00:00:00 '], ' em1 ': [', ' a4:ba:db:20:93:23 '], ' br1 ': [' 112.65.140.133 ', ' a4:ba:db:20:93:23 '], ' em2 ': [' 192.168.101.237 ', ' a4:ba:db:20:93:25 ']}
This article comes from "Plum blossom fragrance from bitter cold!" "Blog, be sure to keep this provenance http://daixuan.blog.51cto.com/5426657/1886312
Collecting IP information using python