Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

[py] Automated masscan scanning for a txt full of ip ranges

I feel its stupid that masscan doesnt support file range stuff (not to what I know off anyway).

Make a file in the same dir as the py script called "ip_ranges.txt", run the script with sudo, enjoy.

Format for ranges (txt):

Code
x.0/24
x.0/23
x.0/21

Output format (txt):

Code
# Masscan 1.3.2 scan initiated Tue Apr  1 15:32:54 2025
# Ports scanned: TCP(65535;1-65535) UDP(0;) SCTP(0;) PROTOCOLS(0;)
Timestamp: 1743521583    Host: x.179 ()    Ports: 9348/open/tcp//unknown//

Code
import subprocess
import os

def read_ip_ranges(file_path):
    with open(file_path, 'r') as file:
        return [line.strip() for line in file if line.strip()]

def scan_ip_range(ip_range, output_dir):
    output_file = os.path.join(output_dir, f"{ip_range.replace('/', '_')}.txt")
    
    if os.path.exists(output_file):
        print(f"Scan for {ip_range} already exists. Skipping...")
        return
    
    print(f"Scanning {ip_range}...")
    command = ["masscan", ip_range, "-p1-65535", "--rate=100000000", "-oG", output_file]
    
    try:
        subprocess.run(command, check=True)
        print(f"Scan for {ip_range} completed and saved to {output_file}.")
    except subprocess.CalledProcessError as e:
        print(f"Error scanning {ip_range}: {e}")

def main():
    ip_ranges_file = 'ip_ranges.txt'  # Path to your IP ranges file
    output_directory = 'scans'  # Directory to save scan results

    # Create output directory if it doesn't exist
    os.makedirs(output_directory, exist_ok=True)

    # Read IP ranges
    ip_ranges = read_ip_ranges(ip_ranges_file)

    # Scan each IP range
    for ip_range in ip_ranges:
        scan_ip_range(ip_range, output_directory)

if __name__ == "__main__":
    main()
Nice. Give this a try sometime if you want to get funky. Here’s an example of your script with obfuscated variable names:

Code
import subprocess
import os

def a(b):
    with open(b, 'r') as c:
        return [d.strip() for d in c if d.strip()]

def e(f, g):
    h = os.path.join(g, f.replace('/', '_') + '.txt')
    
    if os.path.exists(h):
        print(f"Scan for {f} already exists. Skipping...")
        return
    
    # Your scanning logic here
    i = ['masscan', '-p1-65535', f, '-oX', h]  # Modify with actual masscan options
    subprocess.run(i)
    
if __name__ == "__main__":
    j = a("ip_ranges.txt")  # Input file with IP ranges
    k = os.getcwd()  # Current directory
    
    for l in j:
        e(l, k)
Masscan does allow you to have port ranges in a txt file as I've only been scanning it that way lol.
-iL ipranges.txt does this pretty much. But this makes it much easier so thank you!
(Apr 1, 2025 07:41 PM)BrothaMike Wrote:  Masscan does allow you to have port ranges in a txt file as I've only been scanning it that way lol.
-iL ipranges.txt does this pretty much. But this makes it much easier so thank you!

I have never seen the -iL arg in mine, maybe I'm either on an older version or just blind but no problem.
(Apr 1, 2025 07:41 PM)BrothaMike Wrote:  Masscan does allow you to have port ranges in a txt file as I've only been scanning it that way lol.
-iL ipranges.txt does this pretty much. But this makes it much easier so thank you!

I completely agree with you. Even a child knows this)))


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Legends] by HackersNov 2, 2025 08:32 AM 5 362 Nov 2, 2025 08:32 AM
Last Post: InspectorGadget
by Johnny5698May 28, 2017 05:56 PM 0 197 May 28, 2017 05:56 PM
Last Post: Johnny5698
by salieros9Jun 27, 2015 09:03 AM 0 460 Jun 27, 2015 09:03 AM
Last Post: salieros9