程式紀錄~使用python測試主機開啟的port

5 views
Skip to first unread message

win...@nyps.kh.edu.tw

unread,
Nov 14, 2018, 7:36:59 PM11/14/18
to 最新消息-健中
# -*- coding: utf-8 -*-
'''
這支程式使用multi方式,
可用來掃各個網站的port是否開放!
在個人PC執行時間:秒(看輸入的port決定快慢)
設計:Windfly
日期:2015-09-01
程式語言:Python2.7
'''
import sys
import gevent
from gevent import monkey
import socket
from datetime import datetime

monkey.patch_all()

SCHOOL_IP = [
        ['馬公國中','www.mkjh.phc.edu.tw'], ['中正國中','www.ccjh.phc.edu.tw'], ['文光國中','www.wgjh.phc.edu.tw'],
        ['澎南國中','www.pnjh.phc.edu.tw'], ['志清國中','www.jcjh.phc.edu.tw'], ['白沙國中','www.psjh.phc.edu.tw'],
        ['西嶼國中','www.hyjh.phc.edu.tw'], ['吉貝國中','www.gbjh.phc.edu.tw'], ['鳥嶼國中','www.nyjh.phc.edu.tw'],
        ['望安國中','www.wajh.phc.edu.tw'], ['將澳國中','www.jajh.phc.edu.tw'], ['七美國中','www.cmjh.phc.edu.tw'],
        ['湖西國中','www.hsjh.phc.edu.tw'], ['鎮海國中','www.pnjh.phc.edu.tw'],        
        ['馬公國小','www.mkps.phc.edu.tw'], ['中興國小','www.chps.phc.edu.tw'], ['中正國小','www.ccps.phc.edu.tw'],
        ['文澳國小','www.wops.phc.edu.tw'], ['中山國小','www.csps.phc.edu.tw'], ['東衛國小','www.dwps.phc.edu.tw'],
        ['石泉國小','www.scps.phc.edu.tw'], ['山水國小','www.ssps.phc.edu.tw'], ['風櫃國小','www.fkps.phc.edu.tw'],
        ['虎井國小','www.hjps.phc.edu.tw'], ['興仁國小','www.sjps.phc.edu.tw'], ['果葉國小','www.gyps.phc.edu.tw'],
        ['西溪國小','www.sips.phc.edu.tw'], ['龍門國小','www.lmps.phc.edu.tw'], ['隘門國小','www.amps.phc.edu.tw'],
        ['沙港國小','www.skps.phc.edu.tw'], ['中屯國小','www.ctps.phc.edu.tw'], ['講美國小','www.jmps.phc.edu.tw'],
        ['成功國小','www.cgps.phc.edu.tw'], ['赤崁國小','www.ckps.phc.edu.tw'], ['吉貝國小','www.cpps.phc.edu.tw'],
        ['鳥嶼國小','www.nyps.phc.edu.tw'], ['港子國小','www.ktps.phc.edu.tw'], ['合橫國小','www.hhps.phc.edu.tw'],
        ['大池國小','www.dtps.phc.edu.tw'], ['文光國小','www.wgps.phc.edu.tw'], ['赤馬國小','www.rmps.phc.edu.tw'],
        ['花嶼國小','www.hyps.phc.edu.tw'], ['七美國小','www.cmps.phc.edu.tw'], ['雙湖國小','www.tlps.phc.edu.tw'],
        ['將軍國小','www.jjps.phc.edu.tw'], ['竹灣國小','www.cwps.phc.edu.tw'], ['湖西國小','www.hsps.phc.edu.tw'],
        ['五德國小','www.wdps.phc.edu.tw'], ['外垵國小','www.wips.phc.edu.tw'], ['後寮國小','www.hlps.phc.edu.tw'],
        ['時理國小','www.slps.phc.edu.tw'], ['望安國小','www.waps.phc.edu.tw'], ['錯誤國小','www.aaps.phc.edu.tw'],
        ['資安大補帖','isp.phc.edu.tw'], ['教網中心','net.phc.edu.tw'], ['DNS1','ns.phc.edu.tw'], ['DNS2','ns2.phc.edu.tw'],
        ['OpenIDX','openidx.phc.edu.tw'], ['OpneID','openid.phc.edu.tw'], ['12年國教','12edu.phc.edu.tw'], ['教育處網站','www.phc.edu.tw'],
        ]

def chk_posts(remoteServer):
    try:
    	socket.gethostbyname(remoteServer[1])
        for port in range(20,500):  
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.settimeout(0.001)
            try: 
                sock.connect((remoteServer[1], port))
                print "{}: Port {}: \t Open".format(remoteServer[0].decode('utf-8').encode('big5'), port)
            except:
                pass
        sock.close()

    except KeyboardInterrupt:
        print "You pressed Ctrl+C"
        sys.exit()

    except socket.gaierror:
        print remoteServer[0].decode('utf-8').encode('big5') + ': 主機無法建立正常連線...'.decode('utf-8').encode('big5')
        #sys.exit()

    except socket.error:
        print "Couldn't connect to server"
        sys.exit()


if __name__ == '__main__':
    t1 = datetime.now()
    jobs = [gevent.spawn(chk_posts, each) for each in SCHOOL_IP]
    gevent.joinall(jobs)
    t2 = datetime.now()
    total = t2 - t1
    print '掃描結束,花費時間為: '.decode('utf-8').encode('big5'), total
Reply all
Reply to author
Forward
0 new messages