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