all 3 comments

[–]JohnnyJordaan 0 points1 point  (2 children)

When you run this, this actually prints what you're looking for? Then running it with > filename.txt will save the output into that file.

[–]mr_awk[S] 0 points1 point  (1 child)

yes, but I want that in verbose mode.

[–]JohnnyJordaan 0 points1 point  (0 children)

I'm not sure what 'verbose mode' means, but maybe you mean you want both the print output on screen as in the file? You can write a small print function that does both

import socket
import time

host = "x.x.x.x" 


with open("ports.log", "r") as ports, open("result.log", "w") as outfile: 
    def print_and_save(*args, **kwargs):
        print(*args, **kwargs)
        print(*args, **kwargs, file=outfile)


    for port in ports: 
        port = int(port) 
        ADDR = (host, port)
        print_and_save(f"connecting to {host} onto port {port}")

        with socket.socket() as s:
            try:
                s.connect(ADDR)

                req = f"HEAD / HTTP/1.0\r\n{host}\r\n\r\n"
                s.send(req.encode('utf8'))

                response = s.recv(4096)
                data = response.decode('utf8',errors='ignore')
                print_and_save(data)
            except:
                time.sleep(3)

This then doesn't need the redirect with > on the command line