user3795022
user3795022

Reputation: 5

Port Scanner Script Not Functioning

I'm totally confused as to why my script isn't working.

This script basically scans for servers with port 19 open (CHARGEN).

You enter a list of ips in the format:

1.1.1.1
2.2.2.2
3.3.3.3
4.4.4.4
5.5.5.5

and the script scans every ip in the list to check if port 19 is open, and if it is, it writes the ip to a file.

Here is my code:

#!/usr/bin/env python

#CHARGEN Scanner
#Written by Expedient

import sys
import Queue
import socket
import threading

queue = Queue.Queue()

def check_ip(host, output_file, timeout):
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(timeout)
        result = sock.connect_ex((host, 19))
        if result == 0:
            print "Found: %s" % host
            file = open(output_file, "a")
            file.write(host+"\n")
            file.close()
    except:
        pass

def add_to_queue(queue, host, output_file, timeout):
    queue.put(check_ip(host, output_file, timeout))

if len(sys.argv) < 4:
    print "Usage: %s <ip list> <output file> <timeout>" % sys.argv[0]
    sys.exit()

try:
    open(sys.argv[1])
except:
    print "Unable to open ip list."
    sys.exit()

print "Starting Expedient's CHARGEN Scanner..."

with open(sys.argv[1]) as ip_list:
    for ip in ip_list:
        thread = threading.Thread(target=add_to_queue, args=(queue, ip, sys.argv[2], float(sys.argv[3])))
        thread.start()

Whenever I run the script on a list of CHARGEN enabled servers that I got from an nmap scan

(I double checked, every server has port 19 open), the script does not write any of the ips

to the output file, which is should, because every ip in the list has port 19 open.

I honestly have no idea why this isn't working and it would be wonderful if someone could

help me out/tell me what I'm doing wrong. Thank you.

Upvotes: 0

Views: 264

Answers (1)

James
James

Reputation: 88

Your example as posted is catching all exceptions in your check_ip function without telling you (except: pass). You could have any number of issues causing exceptions to be raised in this function, and if an exception is raising in every call of the function then you will get no results from your script while also not getting any feedback to log/console on the nature of the failure.

For the purposes of debugging, you should modify your exception handling to explicitly handle any exceptions that you want to pass over, and allow other exceptions to raise unhandled so that you can determine what your error conditions are.

Upvotes: 1

Related Questions