使用cocket模块配合多线程对端口进行扫描,后续功能正在思考ing.
import socket from multiprocessing.dummy import Pool as ThreadPool def main(): global host_ip host = input("Enter a host to scan:") startPort = int(input("Enter the start port:")) endPort = int(input("Enter the end port:")) processes_num = int(input("Enter processes num :")) host_ip = socket.gethostbyname(host) ports = [] print(‘-‘ * 60) print(‘Please wait,scanning host‘,host_ip) print(‘-‘ * 60) socket.setdefaulttimeout(0.2) for port in range(startPort, endPort): ports.append(port) pool = ThreadPool(processes = processes_num) results = pool.map(get_ip_status,ports) pool.close()#关闭进程池,不在接收新的进程 pool.join()#主线程阻塞等在子进程的退出 print(‘port scan end‘) def get_ip_status(port): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: server.connect((host_ip, port)) print(‘[+] {0} port {1} is open‘.format(host_ip, port)) except Exception as err: pass finally: server.close() if __name__ == ‘__main__‘: main()
大佬们,这是我的菜鸟程序,有什么问题及好的建议,希望随时交流,联系方式:2422535603@qq.com
原文:https://www.cnblogs.com/xiaomingsBlog/p/13498181.html