python 中的端口扫描器工作不正常

Port scanner in python is not working properly

大家好,我正在尝试使用 python3 制作端口扫描器。我之前试过一个,它起作用了,但它没有直接接受参数,而是我不得不 运行 程序,然后给它所有的细节。所以我决定想出一个可以接受参数的端口扫描器。这是我的代码:

#!/usr/bin/env python3

import argparse
import os
import subprocess
import socket

#parsing arguments
parser=argparse.ArgumentParser()
#taking arguments
parser.add_argument("ip_addr", help="IPv4 address of the target")
parser.add_argument("-v", "--verbose", help="give output in verbosity (show even the closed ports)", action="store_true")

args=parser.parse_args()
print(args.ip_addr)


def __port__(port):
    try:
        s=socket.socket()
        s.settimeout(0.5)
        s.connect((args.ip_addr,port))
        print(args.ip_addr)
        return True
    except:
        return False
    finally:
        s.close()




if args.verbose:
    for x in range (0,1025):
        if __port__:
            print("[+] {}:{} is open".format(args.ip_addr,x))
        else:
            print("[+] {}:{} is closed".format(args.ip_addr,x))

else:
    for x in range (0,1025):
        if __port__:
            print("[+] {}:{} is open".format(args.ip_addr,x))

问题是每次我在任何 ip 地址上 运行 这个脚本时,我都会打开所有端口。 这是我的脚本的结果: 192.168.1.1

[+] 192.168.1.1:1 is open
[+] 192.168.1.1:2 is open
[+] 192.168.1.1:3 is open
[+] 192.168.1.1:4 is open
[+] 192.168.1.1:5 is open
[+] 192.168.1.1:6 is open
[+] 192.168.1.1:7 is open
[+] 192.168.1.1:8 is open
[+] 192.168.1.1:9 is open
[+] 192.168.1.1:10 is open
[+] 192.168.1.1:11 is open
[+] 192.168.1.1:12 is open
[+] 192.168.1.1:13 is open
[+] 192.168.1.1:14 is open
[+] 192.168.1.1:15 is open
[+] 192.168.1.1:16 is open
[+] 192.168.1.1:17 is open
[+] 192.168.1.1:18 is open
[+] 192.168.1.1:19 is open
[+] 192.168.1.1:20 is open
[+] 192.168.1.1:21 is open
[+] 192.168.1.1:22 is open
[+] 192.168.1.1:23 is open
[+] 192.168.1.1:24 is open
[+] 192.168.1.1:25 is open
[+] 192.168.1.1:26 is open
[+] 192.168.1.1:27 is open
[+] 192.168.1.1:28 is open
[+] 192.168.1.1:29 is open
...
...
...
[+] 192.168.1.1:1002 is open
[+] 192.168.1.1:1003 is open
[+] 192.168.1.1:1004 is open
[+] 192.168.1.1:1005 is open
[+] 192.168.1.1:1006 is open
[+] 192.168.1.1:1007 is open
[+] 192.168.1.1:1008 is open
[+] 192.168.1.1:1009 is open
[+] 192.168.1.1:1010 is open
[+] 192.168.1.1:1011 is open
[+] 192.168.1.1:1012 is open
[+] 192.168.1.1:1013 is open
[+] 192.168.1.1:1014 is open
[+] 192.168.1.1:1015 is open
[+] 192.168.1.1:1016 is open
[+] 192.168.1.1:1017 is open
[+] 192.168.1.1:1018 is open
[+] 192.168.1.1:1019 is open
[+] 192.168.1.1:1020 is open
[+] 192.168.1.1:1021 is open
[+] 192.168.1.1:1022 is open
[+] 192.168.1.1:1023 is open
[+] 192.168.1.1:1024 is open

我的nmap结果显示只有80端口是开放的。这是我的 nmap 输出:

Starting Nmap 7.91 ( https://nmap.org ) at 2020-10-18 11:54 IST
Nmap scan report for 192.168.1.1
Host is up (0.022s latency).
Not shown: 999 closed ports
PORT   STATE SERVICE
80/tcp open  http

Nmap done: 1 IP address (1 host up) scanned in 0.45 seconds

您需要将端口的值提供给函数 __port__ 调用(否则 if __port__ 始终为真,因为 __port__ 是函数的名称):

if args.verbose:
    for x in range (0,1025):
        if __port__(x):
            print("[+] {}:{} is open".format(args.ip_addr,x))
        else:
            print("[+] {}:{} is closed".format(args.ip_addr,x))

else:
    for x in range (0,1025):
        if __port__(x):
            print("[+] {}:{} is open".format(args.ip_addr,x))