忘记了实验室的ip,但是知道网段。Ps.速度有点慢

#! /usr/bin/env python
#-*- coding=utf-8 -*-
import socket
def Scan(IpAddr,port):
    if len(port)<1:
        port=3389#默认端口
    s=socket.socket()
    for p in range(253,2,-1):
        addr=IpAddr+"."+str(p)
        try:
            s.connect((addr,port))
            print addr
        except socket.error,e:
            pass
if __name__=='__main__':
    IpAddr=raw_input("please input the Ip segment:")
    port=raw_input("please input the port:")
    Scan(IpAddr,port)

输入:

192.168.0

80

这是开启多线程进行扫描:

#! /usr/bin/env python
#-*- coding=utf-8 -*-
import threading , time , Queue , socket
port = [139,445,80,135,137,1433,3389,8080,9015,137,21,23,25,1502]#扫的常用端口。

class ScanThread(threading.Thread):
    def run(self):
        global queue#,Q
        ip = queue.get()
        for p in port:#其实这里可以in range() 就可以添加大点的端口范围了。
            s = socket.socket()
            s.settimeout(3)
            try:
                s.connect((ip,int(p)))
                print '\n========================================================='
                print "IP:%s , Port:%d , Open" %( ip , int(p) )
                print '=========================================================\n'
                sp = "IP:%s , Port:%d , Open" %( ip , int(p) )
                #Q.put("%s %d",ip,int(p))
            except:
                pass
            s.close()
        queue.task_done()

target = raw_input("输入网段(192.168.12):")
x=target.split(".")
if len(x)>3:
    target=x[0]+"."+x[1]+"."+x[2]
#Q=Queue.Queue()

queue = Queue.Queue()
for ip in range(254,2,-1):
    queue.put(target+".%d" % ip)
for p in range(254,2,-1):
    ScanThread().start()
queue.join()


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐