forked from nikozhangwj/FLASK_WOL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nettools.py
53 lines (46 loc) · 1.46 KB
/
nettools.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
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# python net_tools module
import os
import re
import subprocess
import platform
def legal_ip(ip):
compile_ip = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
try:
if compile_ip.match(ip):
return True
else:
return False
except TypeError:
# print ip
return False
def search_mac():
macs = []
system = platform.system()
if system == "Windows":
cmd = "arp -a"
output = subprocess.getstatusoutput(cmd)
if output[0] == 0:
res_lines = output[1].split("\n")
for line in res_lines:
line = line.split(" ")
if len(line) < 5:
continue
line = [i.strip() for i in line if i.strip() != '']
if len(line) == 3:
if line[2] == "动态":
macs.append({"mac": line[1], "ip": line[0]})
elif system == "Linux":
cmd = "arp"
res = subprocess.getstatusoutput(cmd)
for line in res[1].split('\n'):
if len(line) == 80:
continue
line = line.split(" ")
line = [i.strip() for i in line if i.strip() != '']
if legal_ip(line[0]) and line[2] != "(incomplete)":
macs.append({"mac": line[2], "ip": line[0]})
return macs
if __name__ == "__main__":
print(search_mac())