简体   繁体   English

如何确定我的端口扫描仪是否正常工作?

[英]How Can I tell if my Port Scanner is Working?

I'm making a port scanner that checks if ports are open or closed but I am convinced that it does not work as it lists every port as being closed, even ports I've specifically opened just to check if it is working. 我正在做一个端口扫描程序,检查端口是否打开或关闭,但是我确信它不起作用,因为它列出了每个端口都处于关闭状态,即使我专门打开的端口也只是为了检查端口是否正常工作。 Can anyone see anything wrong with my code? 有人可以看到我的代码有什么问题吗?

    if  userChoice == "1":
    # code for option 1
    print("You selected Port Scan Tool")
    loop = 0
    subprocess.call('cls', shell=True)
    remoteServer = input("Enter a remote host to scan: ")
    start=input("Enter starting port number: ")
    start = int(start)
    end=input("Enter ending port number: ")
    end = int(end)
    remoteServerIP = socket.gethostbyname(remoteServer)

    # Print a nice banner with information on which host we are about to scan
    print ("-" * 60)
    print("Please wait, scanning remote host", remoteServerIP)
    print("-" * 60)

    # Check what time the scan started
    t1 = datetime.now()
    timestr = time.strftime("%d.%m.%Y-%H.%M.%S")# creates time stamp on text file

    try:
        textFileLocation = timestr + " -  Port Scan Results.txt"# creates and names text file
        for port in range(start, end):  # lets user select range
            sock = (socket.socket(socket.AF_INET, socket.SOCK_STREAM))
            result = sock.connect_ex((remoteServerIP, port))
            if result == 0:
                print("Port {}: \t Open".format(port))
                #print("Port {}: \t Closed".format(port))
                #print("Port {} \t Closed".format(port))
                textFileLocation = timestr + " - Port Scan Results.txt"
                textFile = open(textFileLocation, "a")
                textToWrite = "Open: Port %d\n" % port
                textFile.write(textToWrite)
                textFile.close()
            else:
                print("Port {}: \t Closed".format(port))
                textFileLocation = timestr + " - Port Scan Results.txt"
                textFile = open(textFileLocation, "a")
                textToWrite = "Closed: Port %d\n" % port
                textFile.write(textToWrite)
                textFile.close()
            sock.close()

This only tests whether there is any program listening on said port. 这仅测试是否有程序在该端口上侦听。

To see whether this works or not, first remove try block to see which error is returned. 若要查看是否可行,请首先删除try块以查看返回了哪个错误。 Then use correct error in exception handling, ie if your machine is not on the network try will fail as well as when being unable to connect. 然后在异常处理中使用正确的错误,即,如果您的计算机不在网络上,则尝试将失败以及无法连接时也会失败。 Also you will have to introduce timeouts so that socket doesn't hang trying to connect. 另外,您还必须引入超时,以便套接字在尝试连接时不会挂起。

To see if your code is doing anything to the target machine, activate firewall there and set it up to notify you if anyone is doing just what you did. 要查看您的代码是否对目标计算机执行任何操作,请在该计算机上激活防火墙,并将其设置为在有人执行您的操作时通知您。 Your code might also fail if your router/switcher is preventing port scanning on your network. 如果您的路由器/切换器阻止了网络上的端口扫描,则您的代码也可能会失败。 You should check its firewall settings too. 您也应该检查其防火墙设置。

You are also missing the except block in your code, and try is in wrong place anyway. 您还缺少代码中的except块,并且无论如何尝试都在错误的位置。 You have to test each connection: 您必须测试每个连接:

for x in range(...):
    try:
        s = socket.socket(...)
        s.connect(...)
        s.close()
    except: pass

Although you should use for instance: 尽管您应该使用例如:

except socket.SocketError as error:

and then check for error number etc. in variable error where exception will be stored. 然后在变量error中检查错误号等,该变量将存储异常。

Oh, BTW, socket.socket.connect() returns None, so your check would always be False. 哦,顺便说一句,socket.socket.connect()返回None,所以您的检查将始终为False。 This is not C, its Python. 这不是C,而是Python。

>>> ...
>>> result = sock.connect(...)
>>> print result
None

Try-except will tell you whether connection passed or failed with a lot more info. Try-except会告诉您连接是通过还是失败,并提供更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM