forked from OWASP/Nettacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip.py
106 lines (81 loc) · 2.41 KB
/
ip.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import netaddr
import requests
from netaddr import iprange_to_cidrs
from netaddr import IPNetwork
def generate_ip_range(ip_range):
"""
IP range to CIDR and IPNetwork type
Args:
ip_range: IP range
Returns:
an array with CIDRs
"""
if '/' in ip_range:
return [
ip.format() for ip in [cidr for cidr in IPNetwork(ip_range)]
]
else:
ips = []
for generator_ip_range in [cidr.iter_hosts() for cidr in iprange_to_cidrs(*ip_range.rsplit('-'))]:
for ip in generator_ip_range:
ips.append(ip.format())
return ips
def get_ip_range(ip):
"""
get IPv4 range from RIPE online database
Args:
ip: IP address
Returns:
IP Range
"""
try:
return generate_ip_range(
json.loads(
requests.get(
'https://rest.db.ripe.net/search.json?query-string={ip}&flags=no-filtering'.format(ip=ip)
).content
)['objects']['object'][0]['primary-key']['attribute'][0]['value'].replace(' ', '')
)
except Exception:
return [ip]
def is_single_ipv4(ip):
"""
to check a value if its IPv4 address
Args:
ip: the value to check if its IPv4
Returns:
True if it's IPv4 otherwise False
"""
return netaddr.valid_ipv4(str(ip))
def is_ipv4_range(ip_range):
try:
return '/' in ip_range and '.' in ip_range and '-' not in ip_range and netaddr.IPNetwork(ip_range)
except Exception:
return False
def is_ipv4_cidr(ip_range):
try:
return '/' not in ip_range and '.' in ip_range and '-' in ip_range and iprange_to_cidrs(*ip_range.split('-'))
except Exception:
return False
def is_single_ipv6(ip):
"""
to check a value if its IPv6 address
Args:
ip: the value to check if its IPv6
Returns:
True if it's IPv6 otherwise False
"""
return netaddr.valid_ipv6(str(ip))
def is_ipv6_range(ip_range):
try:
return '/' not in ip_range and ':' in ip_range and '-' in ip_range and iprange_to_cidrs(*ip_range.split('-'))
except Exception:
return False
def is_ipv6_cidr(ip_range):
try:
return '/' in ip_range and ':' in ip_range and '-' not in ip_range and netaddr.IPNetwork(ip_range)
except Exception:
return False