|
[root@133 systeminformation]# vim devname_2.py
#!/usr/bin/env python
from subprocess import Popen,PIPE
def getIP():
p = Popen(['ifconfig'], stdout=PIPE, stderr=PIPE)
stdout,stderr = p.communicate()
data = [i for i in stdout.split('\n') if i]
return data
def genIP(data): #每个大的列表是每个网卡的信息
new_line = ''
lines = []
tuple_addr= ('lo','vir','vnet','em3','em4')
for line in data:
if line[0].strip(): #顶格,例如:em1,将网卡信息加到new_line
lines.append(new_line)
new_line = line + '\n' #最后加一个\n
else:
new_line +=line + '\n' #把之后未顶格的都加到这个网卡列表中
lines.append(new_line)
#print [i for i in lines if i and not i.startswith(tuple_addr)]
return [i for i in lines 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
dic [devname] = [ipaddr, macaddr]
return dic
if __name__ == '__main__':
data = getIP()
data_list = genIP(data)
parseIfconfig(data_list)
[root@133 systeminformation]# python devname_2.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 |
|
|