怎么用python扫描主机_python扫描主机开放的端口
!/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"輸入要掃描的最大端口: ")
# 判斷為空的時候,設(shè)置一個默認值
if not port_range:
port_range = 65535
ip_scan(url, int(port_range))
總結(jié)
以上是生活随笔為你收集整理的怎么用python扫描主机_python扫描主机开放的端口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bitlocker加密过程中怎么取消 如
- 下一篇: 联想一体机怎么装光盘启动 联想一体机如何