-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetIPdata.py
More file actions
133 lines (110 loc) · 4.1 KB
/
Copy pathgetIPdata.py
File metadata and controls
133 lines (110 loc) · 4.1 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
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
import re
import requests
import aux
from subprocess import Popen, PIPE
#################
# Main fucntion #
#################
def getIPdata(text, html=True):
ip_regex = r'[0-9]+(?:\.[0-9]+){3}'
ips = re.findall(ip_regex, text)
ips = list(set(ips))
if html:
results = '<b><u><big>Detected IP details</b></u></big><br>'
else:
results = 'Detected IP details:\n'
for ip in ips:
if ip_is_private(ip)==True:
######
# Private range message
######
if html==True:
results += '<br><b>**************** ' + ip + ' ***************</b><br>'
results += 'Private range, this attempt was made from the internal network <br>'
elif ip != "0.0.0.0":
#######
# Select your service here
#######
results = ip_api_com(ip, results, html)
#results = ipinfo_io(ip, results, html)
#results = ipstack_com(ip, results, html)
return results+'<br>'
#######################
# Private range check #
#######################
def ip_is_private(ip):
ret = False
numbers = ip.split('.')
if numbers[0]=='10':
ret=True
elif numbers[0]=='172' and int(numbers[1])>=16 and int(numbers[1])<=31:
ret=True
elif numbers[0]=='192' and numbers[1]=='168':
ret=True
elif numbers[0]=='169' and numbers[1]=='254':
ret=True
return ret
#################
# ipinfo.io #
#################
def ipinfo_io(ip, results, html=True):
url = "https://ipinfo.io/" + ip
result = requests.get(url).text
result = result.replace('\n "', '\n ')
result = result.replace('": "', ': ')
result = result.replace('",\n', '\n')
result = result.replace('{', '')
result = result.replace('}', '')
result = result.replace('"\n', '\n')
if html:
result = re.sub(r'(ip: '+ip_regex+')', r'<b>**************** \1 ***************</b>' ,result)
result = result.replace('\n', '<br>')
results = results + result
return results
#################
# ipstack.com #
#################
# API Key needed
def ipstack_com(ip, results, html=True):
url = "http://api.ipstack.com/" +ip+ "?access_key=" +aux.ipstack_com+ "&hostname=1"
result = requests.get(url)
result = result.json()
if html:
loc = str(result['latitude'])+','+str(result['longitude'])
results += '<br><b>**************** ' + result['ip'] + ' ***************</b><br>'
results += 'hostname: ' + result['hostname'] + '<br>'
results += 'city: ' + result['city'] + '<br>'
results += 'region: ' + result['region_name'] + '<br>'
results += 'country: ' + result['country_name'] + '<br>' # ' <img src="' + result['location']['country_flag'] + '"><br>' No funciona por culpa de Google
results += 'loc: ' + loc + '<br>'
results += 'postal: ' + result['zip'] + '<br>'
#results += 'isp: ' + result['connection']['isp'] + '<br>' # No la manda aunque aparece en la guia
return results
#################
# ip-api.com #
#################
def ip_api_com(ip, results, html=True):
url = "http://ip-api.com/json/" + ip
result = requests.get(url)
result = result.json()
print(result)
if html:
loc = str(result['lat'])+','+str(result['lon'])
terminal_hostname = "nslookup "+ result['query'] +" | awk '/name = / {print $4}' | head -1 | sed 's/.$//'"
hostname = console(terminal_hostname)[1].decode("utf-8")
if hostname == '':
hostname = 'No info'
results += '<br><b>**************** ' + result['query'] + ' ***************</b><br>'
results += 'hostname: ' + str(hostname) + '<br>'
results += 'city: ' + result['city'] + '<br>'
results += 'region: ' + result['regionName'] + '<br>'
results += 'country: ' + result['country'] + '<br>'
results += 'loc: ' + loc + '<br>'
results += 'postal: ' + result['zip'] + '<br>'
results += 'isp: ' + result['isp'] + '<br>'
results += 'org: ' + result['org'] + '<br>'
return results
def console(cmd):
p = Popen(cmd, shell=True, stdout=PIPE)
out, err = p.communicate()
return (p.returncode, out, err)