RELEASE: Version 1.0 #3
@ -1,4 +1,4 @@
|
|||||||
#!/usr/local/bin/python3
|
#!/usr/local/env python3
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ import requests
|
|||||||
|
|
||||||
def get_ip_information(ipv4_address: ipaddress.IPv4Address) -> dict:
|
def get_ip_information(ipv4_address: ipaddress.IPv4Address) -> dict:
|
||||||
"""Retrieves information about a given IPv4 address from IP-API.com."""
|
"""Retrieves information about a given IPv4 address from IP-API.com."""
|
||||||
api_endpoint = "http://ip-api.com/json/{}".format(ipv4_address)
|
api_endpoint = f"http://ip-api.com/json/{ipv4_address}"
|
||||||
resp = requests.get(api_endpoint).json()
|
resp = requests.get(api_endpoint).json()
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
@ -19,9 +19,7 @@ def get_autonomous_system_number(as_info: str) -> str:
|
|||||||
|
|
||||||
def get_prefix_information(autonomous_system: int) -> list:
|
def get_prefix_information(autonomous_system: int) -> list:
|
||||||
"""Retrieves prefix information about a given autonomous system."""
|
"""Retrieves prefix information about a given autonomous system."""
|
||||||
api_endpoint = "https://api.hackertarget.com/aslookup/?q={}".format(
|
api_endpoint = f"https://api.hackertarget.com/aslookup/?q={str(autonomous_system)}"
|
||||||
str(autonomous_system)
|
|
||||||
)
|
|
||||||
resp = requests.get(api_endpoint).text
|
resp = requests.get(api_endpoint).text
|
||||||
prefixes = resp.split("\n")
|
prefixes = resp.split("\n")
|
||||||
prefixes.pop(0)
|
prefixes.pop(0)
|
||||||
|
16
app/main.py
16
app/main.py
@ -1,9 +1,11 @@
|
|||||||
#!/usr/local/bin/python3
|
#!/usr/local/bin/python3
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
from app.args import parse_args
|
from app.args import parse_args
|
||||||
from app.print_table import print_table, generate_prefix_string
|
from app.print_table import print_table, generate_prefix_string
|
||||||
from app.query_normalisation import is_ip_address, resolve_domain_name
|
from app.query_normalisation import is_ip_address, resolve_domain_name
|
||||||
from app.ip_info import (
|
from app.ip_info import ( # pragma: no cover
|
||||||
get_ip_information,
|
get_ip_information,
|
||||||
get_autonomous_system_number,
|
get_autonomous_system_number,
|
||||||
get_prefix_information,
|
get_prefix_information,
|
||||||
@ -23,17 +25,17 @@ def main():
|
|||||||
print(HEADER)
|
print(HEADER)
|
||||||
|
|
||||||
# Set IP to passed IP address, or resolve passed domain name to IPv4
|
# Set IP to passed IP address, or resolve passed domain name to IPv4
|
||||||
ip = (
|
ip_address = (
|
||||||
resolve_domain_name(args.query) if not is_ip_address(args.query) else args.query
|
resolve_domain_name(args.query) if not is_ip_address(args.query) else args.query
|
||||||
)
|
)
|
||||||
|
|
||||||
# If not given an IPv4, and can't resolve to IPv4, then throw error and exit
|
# If not given an IPv4, and can't resolve to IPv4, then throw error and exit
|
||||||
if not ip:
|
if not ip_address:
|
||||||
print("ERROR: could not resolve query to IPv4 address.")
|
print("ERROR: could not resolve query to IPv4 address.")
|
||||||
exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Get information from API
|
# Get information from API
|
||||||
ip_info = get_ip_information(ip)
|
ip_info = get_ip_information(ip_address)
|
||||||
as_number = get_autonomous_system_number(ip_info.get("as"))
|
as_number = get_autonomous_system_number(ip_info.get("as"))
|
||||||
|
|
||||||
# Assemble list for table generation
|
# Assemble list for table generation
|
||||||
@ -42,9 +44,7 @@ def main():
|
|||||||
["Organization", ip_info.get("org")],
|
["Organization", ip_info.get("org")],
|
||||||
[
|
[
|
||||||
"Location",
|
"Location",
|
||||||
"{}/{}/{}".format(
|
f"{ip_info.get('country')}/{ip_info.get('regionName')}/{ip_info.get('city')}",
|
||||||
ip_info.get("country"), ip_info.get("regionName"), ip_info.get("city")
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
["Timezone", ip_info.get("timezone")],
|
["Timezone", ip_info.get("timezone")],
|
||||||
["Internet Service Provider", ip_info.get("isp")],
|
["Internet Service Provider", ip_info.get("isp")],
|
||||||
|
@ -4,12 +4,12 @@ from tabulate import tabulate
|
|||||||
|
|
||||||
|
|
||||||
def generate_prefix_string(prefixes: list) -> str:
|
def generate_prefix_string(prefixes: list) -> str:
|
||||||
"""Generate a string that spilts prefixes into rows of 5."""
|
"""Generate a string that spilts prefixes into rows of 4."""
|
||||||
n = 4
|
num_per_row = 4
|
||||||
try:
|
try:
|
||||||
ret = ""
|
ret = ""
|
||||||
for i in range(0, len(prefixes), n):
|
for i in range(0, len(prefixes), num_per_row):
|
||||||
ret += ", ".join(prefixes[i : i + n]) + "\n"
|
ret += ", ".join(prefixes[i : i + num_per_row]) + "\n"
|
||||||
return ret
|
return ret
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return None
|
return None
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import socket
|
||||||
import ipaddress
|
import ipaddress
|
||||||
import requests
|
import requests
|
||||||
import socket
|
|
||||||
|
|
||||||
|
|
||||||
def is_ip_address(query: str) -> bool:
|
def is_ip_address(query: str) -> bool:
|
||||||
@ -17,13 +17,13 @@ def is_ip_address(query: str) -> bool:
|
|||||||
def resolve_domain_name(domain_name: str) -> ipaddress.IPv4Address:
|
def resolve_domain_name(domain_name: str) -> ipaddress.IPv4Address:
|
||||||
"""Resolve a domain name via DNS or return None."""
|
"""Resolve a domain name via DNS or return None."""
|
||||||
try:
|
try:
|
||||||
ip = socket.gethostbyname(domain_name)
|
ip_address = socket.gethostbyname(domain_name)
|
||||||
except socket.gaierror:
|
except socket.gaierror:
|
||||||
ip = None
|
ip_address = None
|
||||||
return ip
|
return ip_address
|
||||||
|
|
||||||
|
|
||||||
def get_public_ip() -> ipaddress.IPv4Address:
|
def get_public_ip() -> ipaddress.IPv4Address:
|
||||||
"""Get the user's current public IPv4 address."""
|
"""Get the user's current public IPv4 address."""
|
||||||
ip = requests.get("https://api.ipify.org").text
|
ip_address = requests.get("https://api.ipify.org").text
|
||||||
return ip
|
return ip_address
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from app.ip_info import (
|
from app.ip_info import ( # pragma: no cover
|
||||||
get_ip_information,
|
get_ip_information,
|
||||||
get_autonomous_system_number,
|
get_autonomous_system_number,
|
||||||
get_prefix_information,
|
get_prefix_information,
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from app.query_normalisation import is_ip_address, resolve_domain_name, get_public_ip
|
from app.query_normalisation import ( # pragma: no cover
|
||||||
|
is_ip_address,
|
||||||
|
resolve_domain_name,
|
||||||
|
get_public_ip,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_is_ip_address_true() -> None:
|
def test_is_ip_address_true() -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user