Python 端口掃描(全連接,無多線程)
阿新 • • 發佈:2018-03-31
Python 端口掃描
‘‘‘這是一個端口全連接掃描的腳本,掃描結果會比較準確,但是比較費時間‘‘‘
‘‘‘運行環境 Python3 ‘‘‘
from socket import *
def portScanner(host,port):
try:
s = socket(AF_INET,SOCK_STREAM) #註意參數
s.connect((host,port)) #註意括號 (host,port)
print(‘[+] %d open‘ % port)
s.close()
except: #如果端口連接失敗,則輸出$port close
print(‘[-] %d close‘ % port)
def main():
setdefaulttimeout(1)
ports = [20, 22, 23, 80, 111, 3306] #定義要掃描的端口,也可以在下面定義 for p in range(1,1024):
for p in ports:
portScanner(‘192.168.60.130‘,p) #設置要掃描的主機為192.168.60.130
if __name__ == ‘__main__‘: #“Make a script both importable and executable”
main() # 如果這文件中的代碼被外部的python文件調用是不會被執行的
ps: 關於Python中的 if __name__ == ‘__main__‘
的解釋可以參考:https://www.cnblogs.com/kex1n/p/5975575.html
Python 端口掃描(全連接,無多線程)