0

Im currently writing a programm which scans the internet for port:25565. When it finds a open port a function as dramatiq acotr is called which check the adress(ip + port) for deatails. But dramatiq uses more than the function.

import json
import masscan
import socket
import dramatiq
import redis
import random
import pymongo
from colorama import Fore,init

init()
red = Fore.RED
white = Fore.WHITE
r = redis.Redis()
queue = "mc_check_queue"
client = pymongo.MongoClient("192.168.188.32:27017")
if client.server_info():
    print("Connected to MongoDB successfully!")
else:
    print("Could not connect to MongoDB.")
db = client["treffer"]
collection = db["ips"]

@dramatiq.actor
def mc_and_check_task(ip):
    ip = JavaServer.lookup(ip)
    try:
        status = ip.status()
        print(status)
        mc_status = {"ip": ip, "status": status}
        collection.insert_one(mc_status)
    except socket.timeout:
        return "Fehler socket.timeout"
    except BrokenPipeError:
        return "Fehler BrokenPipeError"
    except Exception as e:
        return f"An error occurred: + {e}"



A = list(range(1,255))
B = list(range(1,255))
random.shuffle(A)
random.shuffle(B)

ip_ranges = []
for a in A:
    for b in B:
        ip_range = f"{a}.{b}.0.0/16"
        ip_ranges.append(ip_range)

for ip_range in ip_ranges:
    print(ip_range)
    try:
        mas = masscan.PortScanner()
        mas.scan(ip_range, ports="25565", arguments="--max-rate 1000000")
        x = json.loads(mas.scan_result)
        len_result = len(x["scan"])
        if len_result > 0:
            print(f"Results: {red}{len_result}")
        else:
            print(f"Results: {white}{len_result}")
        for ip in x["scan"]:
            adresse = ip + ":" + "25565"
            mc_and_check_task.send(adresse)
            for document in db.collection.find():
                print(document)
    except masscan.NetworkConnectionError:
        print(f"{ip_range}masscan connection error")
    print("done scanning")

The dramatiq terminal output looks like this:

165.227.0.0/16
Results:
0
done scanning
81.222.0.0/16
Results:
0
done scanning
243.155.0.0/16
Results:
0
done scanning

The output of the main terminal looks identical except that in the main window it takes like 10 sec for a new ip range to get printed due to scanning. The dramatiq windows on the other hand just prints out random ip ranges. Why does the decorator include more than just the function. What I want is that the status of the adress is printed in the dramatiq window and the ip range + results + done scanning in the main window.

Thanks.

0 Answers0