forked from g-rd/snortparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
71 lines (63 loc) · 2.04 KB
/
utils.py
File metadata and controls
71 lines (63 loc) · 2.04 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import re
import socket
from uuid import UUID
class Utils():
def __ipv4(self, ip):
rex = re.compile(r'^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}){1}$|'
'^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,2})$')
if not re.match(rex, ip):
print "error"
return ValueError("Not an ipv4 IP")
if "/" in ip:
iprange = ip.split("/")
ip = iprange[0]
subnet_start = ip.split(".")
size = iprange[-1]
try:
int(size)
except:
return ValueError("not int")
if int(subnet_start[-1]) > 0 and (int(size) > 32 or int(size) < 0):
return ValueError("subnet start is not correct")
if int(size) > 32 or int(size) < 0:
return ValueError("out of range")
try:
socket.inet_pton(socket.AF_INET, ip)
except socket.error as e:
print "error"
return ValueError(e)
else:
return True
def __ipv6(self, ip):
spechial_ipv6 = {"unspecfified": "::/128",
"default route": "::/0",
"loopback": "::1/128"
}
if ip in spechial_ipv6.values():
return True
if "/" in ip:
iprange = ip.split("/")
ip = iprange[0]
size = iprange[-1]
try:
int(size)
except:
return ValueError("not int")
if int(size) == 127:
return True
if int(size) > 64 or int(size) < 0:
return ValueError("out of range")
try:
socket.inet_pton(socket.AF_INET6, ip)
except socket.error:
return False
else:
return True
def valid_ip(self, ip):
tests = {}
tests["ipv4"] = self.__ipv4(ip)
tests["ipv6"] = self.__ipv6(ip)
if True not in tests.values():
return False
else:
return True