-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcidr_search.py
executable file
·133 lines (108 loc) · 3.33 KB
/
cidr_search.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
#
# IP Address Search Tool
#
#
import os
import sys
import sqlite3
import ipaddress
import argparse
import traceback
import cidr_ipattr
class EvalIpException(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return self.message
def eval_ipaddr(ipaddr, cursor, city_mode=False):
try:
ip = ipaddress.ip_address(ipaddr)
if ip.is_private is True:
raise EvalIpException("Private IP address")
if ip.is_multicast is True:
raise EvalIpException("Multicast IP address")
except ValueError:
raise EvalIpException("Not IP address")
attr = cidr_ipattr.IpAttribute(ip.version)
def get_ip_record(stmt):
param = attr.bin_addr(ip)
cursor.execute(
stmt % ip.version, tuple([param[: attr.matches] + "%", param, "%"])
)
row = cursor.fetchone()
return row
country = get_ip_record(
"""
select country, cidr
from (select addr, country, cidr, prefixlen from ipaddr_v%d where addr like ?)
where addr like substr(?,1,prefixlen) || ?
"""
)
provider = get_ip_record(
"""
select provider,asn
from (select addr, provider, asn, prefixlen from asn_v%d where addr like ?)
where addr like substr(?,1,prefixlen) || ?
"""
)
if country:
if city_mode:
city = get_ip_record(
"""
select city
from (select addr, city, prefixlen from city_v%d where addr like ?)
where addr like substr(?,1,prefixlen) || ?
"""
)
return format("%s,%s,%s,AS%s,%s" % (country + provider + city))
else:
return format("%s,%s,%s,AS%s" % (country + provider))
else:
raise EvalIpException("Not Found")
def do_eval_ipaddr(ipaddr, cursor, city_mode):
try:
result = eval_ipaddr(ipaddr, cursor, city_mode)
print(result)
except EvalIpException as e:
print(e)
def get_city_mode(cursor):
cursor.execute(
"select count(*) from sqlite_master where type='table' and name like 'city_v%'"
)
return 2 == cursor.fetchone()[0]
def repl(cursor):
print("######## Please Input IP Adress #######\n")
city_mode = get_city_mode(cursor)
while True:
try:
ipaddr = input("<cidr-lite> ")
except KeyboardInterrupt:
print("")
continue
ipaddr = ipaddr.strip()
if ipaddr == "":
continue
if ipaddr == "quit":
break
do_eval_ipaddr(ipaddr, cursor, city_mode)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--ip", type=str, dest="ipaddr", required=False)
args = parser.parse_args(sys.argv[1:])
dbpath = os.path.join(os.environ.get("HOME"), "database.cidr")
try:
conn = sqlite3.connect(dbpath)
cursor = conn.cursor()
cursor.execute("PRAGMA case_sensitive_like=ON;")
if args.ipaddr:
do_eval_ipaddr(args.ipaddr, cursor)
else:
repl(cursor)
conn.close()
except EOFError:
sys.exit(0)
except Exception as e:
print(e, traceback.format_exc(), file=sys.stderr)
sys.exit(1)