python学习:收集ip信息

#!/usr/bin/env python
 
from subprocess import Popen, PIPE
 
def getIfconfig():
    p = Popen(['ifconfig'], stdout=PIPE)
    data = p.stdout.read().split('\n\n')
    return [i for i in data if i and not i.startswith('lo')]
 
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]
        ipaddr = line_list[1].split()[1].split(':')[1]
        dic[devname] = [ipaddr, macaddr]
    return dic
 
if __name__ == '__main__':
    data = getIfconfig()
    print parseIfconfig(data)
 
 
[root@web10 day2]# python 13_ip.py
{'eth0': ['192.168.1.200', '00:0C:29:B7:57:8F']}
 
收集网卡的信息 ip地址,物理mac地址
posted @ 2017-09-14 16:09  Oops!#  阅读(225)  评论(0编辑  收藏  举报