-
Notifications
You must be signed in to change notification settings - Fork 0
/
getip.py
executable file
·112 lines (85 loc) · 3.49 KB
/
getip.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import requests
import re
import netaddr
import argparse
__description__ = "This script returns ip range according to options"
def parse_arguments():
parser = argparse.ArgumentParser(description=__description__, \
add_help=False)
parser.add_argument('-l', '--list', action='store_true', dest='list', \
default=False)
parser.add_argument('-c', '--country', action='store', dest='country', \
type=str, default="Canada")
parser.add_argument('-n', '--number', action='store', dest='country_num', \
type=int, default=None)
parser.add_argument('-f', '--format', action='store', dest='format', \
type=str, choices=["cidr","nmap","simple"], default="cidr")
parser.add_argument('-h', '--help', action='store_true', dest='help', \
default=False)
args = parser.parse_args()
if args.help == True:
print_help(parser, args)
sys.exit(0)
return args
def print_help(parser, args):
print(parser.description)
print("")
print(" -l, --list\t" + "No IPs, just list all IPs of countries you can recieve")
print(" -c, --country\t" + "Name of country to get IP range")
print(" -n, --country_num\t" + "Number of country to get IP range")
print(" -f, --format\t" + "In what format to print ips, 'cidr', 'nmap', 'simple'")
print(" --version\t" + "Show script vesion")
print(" -h, --help\t" + "Prints this message")
print("")
print("Examples:")
print(" ./" + parser.prog + " -c Canada -f cidr")
def convert_to_range(ip1, ip2):
ip_1 = re.search(r'([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})', ip1)
ip_2 = re.search(r'([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})', ip2)
iprange = ""
for i in range(1, 5):
if (int(ip_1.group(i)) < int(ip_2.group(i))):
iprange += ip_1.group(i) + "-" + ip_2.group(i)
else:
iprange += ip_1.group(i)
if i < 4: iprange += "."
return iprange
def main(args):
req = requests.get('https://www.nirsoft.net/countryip/')
main = req.text.encode('utf-8')
rawlist = re.findall(r'(<td><a\ href=")([a-z]{2,}\.html)">([A-Z,\�\'\.\(\)\ a-z\-]*)<\/a>', main)
if args.list == True:
for i, data in enumerate(rawlist):
print(str(i) + " - " + data[2])
return 0
# get url by country name
if args.country_num == None:
url_c = ""
for k, data in enumerate(rawlist):
if args.country in data:
url_c = data[1]
break
req = requests.get('https://www.nirsoft.net/countryip/' + url_c)
else:
req = requests.get('https://www.nirsoft.net/countryip/' + rawlist[args.country_num][1])
iphtml = req.text.encode('utf-8')
iplistraw = re.findall(r'(<tr> <td>)([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})(\ <td>)([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})', iphtml)
for iprange in iplistraw:
if args.format == 'cidr':
cidr = netaddr.iprange_to_cidrs(iprange[1], iprange[3])
print(cidr[0])
elif args.format == 'nmap':
ip = convert_to_range(iprange[1], iprange[3])
print(ip)
elif args.format == 'simple':
print(iprange[1] + " - " + iprange[3])
return 0
if __name__ == "__main__":
args = parse_arguments()
err = main(args)
if err != 0:
print("[-] Script failed with error code %s" % str(err))
sys.exit(0)