服务器端口检测端口扫描小工具

简介

服务器端口检测端口扫描小工具,用于检测服务器哪些端口开放了,方便去关闭没使用的端口

截图

端口扫描截图 端口扫描截图

程序源码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
import socket
import threading
import tkinter
from concurrent.futures import ProcessPoolExecutor
from tkinter import *
from tkinter import messagebox
from tkinter.ttk import *


class Win:
    def __init__(self):
        self.root = self.__win()
        self.tk_label_l4xhy6eq = self.__tk_label_l4xhy6eq()
        self.tk_label_l4xhyz3o = self.__tk_label_l4xhyz3o()
        self.tk_label_l4xhzg7d = self.__tk_label_l4xhzg7d()
        self.tk_input_ip = self.__tk_input_ip()
        self.tk_input_port_start = self.__tk_input_port_start()
        self.tk_input_port_end = self.__tk_input_port_end()
        self.tk_list_box_list = self.__tk_list_box_list()
        self.tk_label_l4xi7p8e = self.__tk_label_l4xi7p8e()
        self.tk_label_progress = self.__tk_label_progress()
        self.tk_button_scan = self.__tk_button_scan()
        self.tk_button_copy = self.__tk_button_copy()

    def __win(self):
        root = Tk()
        root.title("端口开放检测 ~  TkinterHelper")
        # 设置大小 居中展示
        width = 441
        height = 167
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        root.geometry(geometry)
        root.resizable(width=False, height=False)
        return root

    def show(self):
        self.root.mainloop()

    def __tk_label_l4xhy6eq(self):
        label = Label(self.root, text="ip/域名")
        label.place(x=10, y=10, width=70, height=25)
        return label

    def __tk_label_l4xhyz3o(self):
        label = Label(self.root, text="开始端口")
        label.place(x=10, y=50, width=70, height=25)
        return label

    def __tk_label_l4xhzg7d(self):
        label = Label(self.root, text="结束端口")
        label.place(x=10, y=90, width=70, height=25)
        return label

    def __tk_input_ip(self):
        ipt = Entry(self.root)
        ipt.place(x=100, y=10, width=150, height=24)
        return ipt

    def __tk_input_port_start(self):
        ipt = Entry(self.root)
        ipt.place(x=100, y=50, width=150, height=24)
        return ipt

    def __tk_input_port_end(self):
        ipt = Entry(self.root)
        ipt.place(x=100, y=90, width=150, height=24)
        return ipt

    def __tk_list_box_list(self):
        lb = Listbox(self.root)
        lb.place(x=270, y=10, width=160, height=105)
        return lb

    def __tk_label_l4xi7p8e(self):
        label = Label(self.root, text="检测进度")
        label.place(x=10, y=130, width=70, height=25)
        return label

    def __tk_label_progress(self):
        label = Label(self.root, text="0/1")
        label.place(x=100, y=130, width=150, height=24)
        return label

    def __tk_button_scan(self):
        btn = Button(self.root, text="开始检测")
        btn.place(x=270, y=130, width=80, height=25)
        return btn

    def __tk_button_copy(self):
        btn = Button(self.root, text="复制结果")
        btn.place(x=355, y=130, width=80, height=25)
        return btn


def is_open(ip, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(1)
    try:
        s.connect((ip, port))
        s.shutdown(socket.SHUT_RDWR)
        return True, port
    except Exception as e:
        pass

    return False, port


def scan():
    global total_port
    global prog_val
    global progress

    prog_val = 0
    total_port = 0

    ip = win.tk_input_ip.get()
    start_port = win.tk_input_port_start.get()
    end_port = win.tk_input_port_end.get()

    if not ip:
        messagebox.showerror("参数错误", "ip/域名 不能为空")
        return

    if not start_port:
        messagebox.showerror("参数错误", "开始端口 不能为空")
        return

    if not end_port:
        messagebox.showerror("参数错误", "结束端口 不能为空")
        return

    ports = range(int(start_port), int(end_port) + 1)
    total_port = len(ports)
    progress.set(str(prog_val) + "/" + str(total_port))

    def callback(future):
        is_open, port = future.result()
        global prog_val
        global progress
        global port_list
        if t_lock.acquire():
            prog_val += 1
            progress.set(str(prog_val) + "/" + str(total_port))
            if is_open:
                port_list.insert(END, port)
            t_lock.release()

    for port in ports:
        pool.submit(is_open, ip=ip, port=port).add_done_callback(callback)


if __name__ == "__main__":
    win = Win()
    pool = ProcessPoolExecutor(max_workers=os.cpu_count())
    port_list = win.tk_list_box_list
    progress = tkinter.StringVar()
    prog_val = 0
    total_port = 0
    t_lock = threading.Lock()
    win.tk_label_progress.config(textvariable=progress)
    win.tk_button_scan.config(command=scan)
    win.show()
站长微信
请备注来意
二维码