-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
53 lines (43 loc) · 1.41 KB
/
main.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
import socket
import ipinfo
from tabulate import tabulate
def get_reverse_dns(ip_address):
try:
hostnames = socket.gethostbyaddr(ip_address)
return hostnames[0]
except socket.herror:
return "Reverse DNS not found"
def get_ip_information(ip_address, access_token):
handler = ipinfo.getHandler(access_token)
details = handler.getDetails(ip_address)
is_private_ip = is_private(ip_address)
return {
'Reverse DNS': get_reverse_dns(ip_address),
'ISP': details.org,
'Country': details.country_name,
'Region': details.region,
'Is it private IP?': is_private_ip
}
def is_private(ip_address):
if ":" in ip_address:
return False
parts = ip_address.split(".")
first_part = int(parts[0])
if first_part == 10:
return True
elif first_part == 172 and 16 <= int(parts[1]) <= 31:
return True
elif first_part == 192 and int(parts[1]) == 168:
return True
else:
return False
def main():
access_token = input("Please enter your IPinfo API access token: ")
ip_address = input("Enter an IPv4 or IPv6 address: ")
ip_info = get_ip_information(ip_address, access_token)
table_data = []
for key, value in ip_info.items():
table_data.append([key, value])
print(tabulate(table_data, headers=["Attribute", "Value"]))
if __name__ == "__main__":
main()