首頁  >  文章  >  後端開發  >  利用Python實作簡易埠掃描器方法介紹

利用Python實作簡易埠掃描器方法介紹

高洛峰
高洛峰原創
2017-03-15 13:37:421371瀏覽

在網路上的一些資料的基礎上自己又添了些新內容,算是Python socket程式練手吧。

#coding=utf-8
import socket
import time
import sys
import struct
import threading
from threading import Thread,activeCount

results=[]
def portScanner(ip,port):
    server = (ip,port)
    sockfd = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sockfd.settimeout(0.1)          #设置阻塞模式下socket的超时时间
    ret = sockfd.connect_ex(server)  #成功返回0,失败返回error的值。
    if not ret:
        sockfd.close()
        results.append([ip,port])
        #print '%s:%s is opened...' % (ip,port)
    else:
        sockfd.close()
        pass
    return ''
    

def ip2num(ip):         #将ip地址转换成数字
    lp = [int(x) for x in ip.split('.')]
    return lp[0] << 24 | lp[1] << 16 | lp[2] << 8 |lp[3]

def num2ip(num):
    ip = [&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;]
    ip[3] = (num & 0xff)
    ip[2] = (num & 0xff00) >> 8
    ip[1] = (num & 0xff0000) >> 16
    ip[0] = (num & 0xff000000) >> 24
    return &#39;%s.%s.%s.%s&#39; % (ip[0],ip[1],ip[2],ip[3])

def iprange(ip1,ip2):
    num1 =socket.ntohl(struct.unpack("I",socket.inet_aton(str(ip1)))[0])
    num2 =socket.ntohl(struct.unpack("I",socket.inet_aton(str(ip2)))[0])
    tmp = num2 - num1
    if tmp < 0:
        return None
    else:
        return num1,num2,tmp


if name == &#39;main&#39;:

    if((len(sys.argv)!= 4)&(len(sys.argv)!= 2)):   #用法说明
        print &#39;Usage:\n\tscanner.py startip endip port&#39;
        print &#39;\tscanner.py ip&#39;
        sys.exit()

    if len(sys.argv)==4:            #对某一IP段的扫描
        time_start=time.time()        #起始时间
        startip = sys.argv[1]        #起始IP
        endip = sys.argv[2]            #结束IP
        port = int(sys.argv[3])        #端口号

        res = iprange(startip,endip)
        if not res:
            print &#39;endip must be bigger than startone&#39;
            sys.exit()
        elif res[2] == 0:
            portScanner(startip,port)
        else:
            for x in xrange(int(res[2])+1):        #IP地址依次递增
                startipnum = ip2num(startip)
                startipnum = startipnum + x
                if activeCount() <=1000:                      
                    Thread(target=portScanner,args=(num2ip(startipnum),port)).start()
                
        print "There are %d hosts." %len(results)
        results.sort()
        for ip,port in results:
            print "%s:%d is opened..." %(ip,port)
        times=time.time()-time_start            #用时
        print &#39;use time : %s&#39; % times
                
    if len(sys.argv)==2:
        time_start=time.time()
        port=0
        ip=sys.argv[1]
        while(port<2000):
            if activeCount() <= 40:     #设置40线程扫描
                Thread(target = portScanner, args = (ip, port)).start()
                port=port+1
        results.sort()
        for ip,port in results:
            print "%s:%d is opened..." %(ip,port)
        times=time.time()-time_start
        print &#39;use time : %s&#39; % times

使用效果如下:

利用Python實作簡易埠掃描器方法介紹

利用Python實作簡易埠掃描器方法介紹

#

以上是利用Python實作簡易埠掃描器方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn