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):
Output format (txt):
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/21Output 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()



