Python實現區域網路指定連接埠掃描

來源:互聯網
上載者:User

標籤:Python   區域網路指定連接埠掃描   

import socketimport threading# 建立接收路由列表routers = []# 建立互斥鎖lock = threading.Lock()# 設定需要掃描的連接埠號碼列表port_list = [‘3389‘, ‘2425‘,‘139‘]# 定義查詢路由函數def search_routers(): # 擷取本地ip地址清單 local_ips = socket.gethostbyname_ex(socket.gethostname())[2] # print(local_ips) # 存放線程列表池 all_threads = [] # 迴圈本地網卡IP列表 for ip in local_ips: for i in range(1, 255): # 把網卡IP"."進行分割,產生每一個可用地址的列表 array = ip.split(‘.‘) # 擷取分割後的第四位元字,產生該網段所有可用IP地址 array[3] = str(i) # 把分割後的每一可用地址清單,用"."串連起來,產生新的ip new_ip = ‘.‘.join(array) # print(new_ip) # 遍曆需要掃描的連接埠號碼列表 for port in port_list: dst_port = int(port) # 迴圈建立線程去連結該地址 t = threading.Thread(target=check_ip, args=(new_ip, dst_port) ) t.start() # 把建立的線程放到線程池 all_threads.append(t) # 迴圈阻塞主線程,等待每一字子線程執行完,程式再退出 for t in all_threads: t.join()# 建立訪問IP列表方法def check_ip(new_ip, port): # 建立TCP通訊端,連結新的ip列表 scan_link = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 設定連結逾時時間 scan_link.settimeout(2) # 連結地址(通過指定我們 構造的主機地址,和掃描指定連接埠) result = scan_link.connect_ex((new_ip, port)) # scan_link.close() # print(result) # 判斷連結結果 if result == 0: # 加鎖 lock.acquire() print(new_ip, ‘\t\t連接埠號碼%s開放‘ % port) routers.append((new_ip, port)) # 釋放鎖 lock.release() # print(routers)print("正在掃描..., 請稍等...")# 啟動程式入口if __name__ == ‘__main__‘: # 啟動掃描程式 search_routers()

Python實現區域網路指定連接埠掃描

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.