Skip to content

Instantly share code, notes, and snippets.

@641i130
Created November 3, 2023 16:10
Show Gist options
  • Select an option

  • Save 641i130/6526b00f9f60fdb4df88f2bc2abdb719 to your computer and use it in GitHub Desktop.

Select an option

Save 641i130/6526b00f9f60fdb4df88f2bc2abdb719 to your computer and use it in GitHub Desktop.

Revisions

  1. 641i130 created this gist Nov 3, 2023.
    48 changes: 48 additions & 0 deletions run.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    import re
    import subprocess
    from os.path import exists
    # Ask the user for the CIDR range to scan
    cidr_range = input("Enter the CIDR range to scan (e.g., '10.10.4.0/22'): ")
    if cidr_range == "":
    cidr_range = "10.10.4.0/22"

    if not exists("./scan.txt"):
    try:
    # Run the nmap command to perform a ping scan and capture the output
    nmap_command = f'nmap -sn {cidr_range} -oN scan.txt'
    nmap_output = subprocess.check_output(nmap_command, shell=True, universal_newlines=True)
    except subprocess.CalledProcessError:
    print("Nmap command failed. Make sure you have Nmap installed and try again.")
    exit(1)

    with open('./scan.txt', 'r') as file:
    nmap_output = file.read()


    # Split the nmap output into individual sections for each device
    device_sections = re.split(r'Nmap scan report for ', nmap_output)
    # Remove the empty string resulting from the split
    device_sections = [section for section in device_sections if section.strip()]

    # Ask the user for filter keywords (comma-separated)
    filter_keywords = "Netgear,Dell".split(",")

    # Initialize a list to store the IP addresses that match the filter
    filtered_ips = []

    # Iterate through the device sections and filter based on the keywords
    for section in device_sections:
    print(section)
    should_include = all(keyword.lower() not in section.lower() for keyword in filter_keywords)
    if should_include:
    ip_match = re.search(r'(\d+\.\d+\.\d+\.\d+)', section)
    mac_match = re.search(r'MAC Address: ([\w:]+)', section)
    if ip_match:
    ip_address = ip_match.group(1)
    if mac_match:
    mac_address = mac_match.group(1)
    filtered_ips.append(f"{ip_address}")

    # Join the filtered IP addresses with commas and print the result
    result = ', '.join(filtered_ips)
    print("Filtered IP addresses: " + result)