使用python收集IP信息
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用python收集IP信息相关的知识,希望对你有一定的参考价值。
使用python收集IP信息
[[email protected] systeminformation]# vim devname_1.py #!/usr/bin/env python from subprocess import Popen,PIPE def getIfconfig(): tuple_addr = (‘lo‘,‘vir‘,‘vnet‘,‘em3‘,‘em4‘) #有些网卡以这些字符开头,排除在外 p = Popen([‘ifconfig‘], stdout=PIPE) data = p.stdout.read().split(‘\n\n‘) return [i for i in data if i and not i.startswith(tuple_addr)]#排除在外 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] print devname, macaddr, ipaddr #打印出来网卡名字,mac,ip dic [devname] = [ipaddr, macaddr] return dic if __name__ == ‘__main__‘: data = getIfconfig() print parseIfconfig(data) [[email protected] systeminformation]# python devname_1.py br1 A4:BA:DB:20:93:23 112.65.140.133 docker0 00:00:00:00:00:00 172.17.42.1 em1 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‘]}
本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1886312
以上是关于使用python收集IP信息的主要内容,如果未能解决你的问题,请参考以下文章