?。穑簦瑁铮顠呙柚鳈C開放的端口

!/usr/bin/python

-- coding: utf-8 --

"""
@version: ??
@author: Guanjie Zhou
@license: Apache Licence
@file: dis_port_threading.py
@time: 17-5-27 上午8:21
"""

import socket
import time
import threading

socket.setdefaulttimeout(1)

建立存放開放端口的列表

open_port = []

lock = threading._allocate_lock()

線程列表

threads = []

計數(shù)器

openNum = 0

端口掃描函數(shù)

def socket_port(ip, port):
global openNum
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 返回連接結(jié)果,成功的話返回為0,失敗返回errno的值
# 這里要注意的是下邊的函數(shù)里邊的為(ip, port)一個整體
result = s.connect_ex((ip, port))
if result == 0:
lock.acquire()
print '[+] %d open' % port
open_port.append(port)
openNum += 1
lock.release()
except:
print u"端口掃描異常"

def ip_scan(ip, port_range=65535):
# 輸入ip地址后,掃描在范圍內(nèi)的端口占用情況
try:
start_time = time.time()
for port in range(0, port_range):
t = threading.Thread(target=socket_port, args=(ip, int(port)))
# 將線程加入線程列表
threads.append(t)
t.start()
# 線程加join
for t in threads:
t.join()
end_time = time.time()
print u'[] %s掃描完成,總共用時: %.2f' % (ip, (end_time - start_time))
print u'[
] 一共開放%d端口,具體如下: ' % (openNum)
print open_port
except:
print u"端口掃描出錯"

if name == 'main':
# 輸入要掃描的端口號
url = raw_input('Input the ip whitch you want to scan: ')
# 輸入要掃描的范圍
port_range = raw_input(u"輸入要掃描的最大端口: ")
# 判斷為空的時候,設置一個默認值
if not port_range:
port_range = 65535
ip_scan(url, int(port_range))

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容