-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip2as_cli.py
executable file
·84 lines (66 loc) · 2.01 KB
/
ip2as_cli.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
__author__ = 'Georges Toth'
__email__ = '[email protected]'
__copyright__ = 'Copyright 2013, Georges Toth'
__license__ = 'GPL v3+'
#
# mail feature extractor
# submits results to a remote mailm0n server via REST API
#
import sys
import os
try:
import ConfigParser as configparser
except ImportError:
import configparser
from optparse import OptionParser
import json
import ip2as.api
def main(api_url, api_key, proxies):
api = gclu_pdns.api.PDNSApi(api_url, api_key, verify_ssl=False, proxies=proxies)
j = api.get_asn('16876')
j = ip2as.api.json_pretty_print(j)
print(j)
if __name__ == '__main__':
config_file = os.path.expanduser('~/.ip2as_cli.conf')
if not os.path.isfile(config_file):
print('Fatal error: config file not found!')
sys.exit(1)
config = configparser.ConfigParser()
config.read(config_file)
try:
api_url = config.get('api', 'url')
api_key = config.get('api', 'key')
http_proxy = config.get('api', 'http_proxy')
https_proxy = config.get('api', 'https_proxy')
except:
print('Fatal error: invalid config file!')
sys.exit(1)
proxies = {}
if not http_proxy == '':
proxies['http'] = http_proxy
if not https_proxy == '':
proxies['https'] = https_proxy
api = ip2as.api.IP2ASApi(api_url, api_key, verify_ssl=False, proxies=proxies)
parser = OptionParser()
parser.add_option('-a', dest='asn', type='string', default='',
help='AS number')
parser.add_option('-i', dest='ip', type='string', default='',
help='IP')
(options, args) = parser.parse_args()
if options.asn == '' and options.ip == '':
parser.print_help()
exit(1)
if not options.asn == '':
j = api.get_asn(options.asn)
elif not options.ip == '':
j = api.get_ip(options.ip)
else:
print('Fatal error: nothing to do O_o, check parameters!')
print()
parser.print_help()
exit(1)
j = ip2as.api.json_pretty_print(j)
print(j)