-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipshow
More file actions
executable file
·48 lines (40 loc) · 1.05 KB
/
ipshow
File metadata and controls
executable file
·48 lines (40 loc) · 1.05 KB
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
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
import subprocess
import re
output = subprocess.Popen(['ip', 'addr'], stdout=subprocess.PIPE).communicate()[0]
interfaces = []
for line in output.decode("utf-8").split("\n"):
match = re.match("[0-9]+: ([a-z0-9]+):.*", line)
if match:
interfaces.append({
"name" : match.group(1),
"ip" : []
})
continue
#endif
match = re.match("\s+link/([a-z]+)\s+([0-9a-f:]+) .*", line)
if match:
interfaces[len(interfaces)-1]['type'] = match.group(1)
interfaces[len(interfaces)-1]['mac'] = match.group(2)
continue
#endif
match = re.match("\s+inet ([0-9\./]+) .*", line)
if match:
interfaces[len(interfaces)-1]['ip'].append({
"type" : "ipv4",
"address" : match.group(1)
})
#endif
match = re.match("\s+inet6 ([0-9a-f:/]+) .*", line)
if match:
interfaces[len(interfaces)-1]['ip'].append({
"type" : "ipv6",
"address" : match.group(1)
})
#endif
for i in interfaces:
print("%s\ttype %s\tmac %s" % (i['name'], i['type'], i['mac']))
for ip in i['ip']:
print("\t%s %s" % (ip['type'], ip['address']))
#endfor
#endfor