-
Notifications
You must be signed in to change notification settings - Fork 0
/
readIPinfo.py
40 lines (34 loc) · 1.07 KB
/
readIPinfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/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 = []
for line in data:
if line[0].strip(): ## No space in the front.
## "not line[0].strip()" means there is space in the front
lines.append(new_line)
new_line = line + '\n'
else:
new_line += line + '\n'
lines.append(new_line)
return [i for i in lines 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
def main():
data = getIP()
data_list = genIP(data)
return parseIfconfig(data_list)
if __name__ == '__main__':
print main()