uage:
python scanner.py 192.168.0.1

#-*- coding: utf-8 -*-
#date: 2017-07-30


import platform
import sys
import os
import time
import thread
import socket
import re
def get_os():
  '''''
  get os 类型
  '''
  os = platform.system()
  if os == "Windows":
    return "n"
  else:
    return "c"

ips={}

def get_ip_file():
        p= re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])')
        file = open('host.txt','r')
        ip_file=file.read()
        ip=p.findall(ip_file)
        file.close()
        for i in ip:
                ips[i]=i
                #print type(i)

def ip_ping(ip_str):
        cmd="ping -"+get_os()+" 1 "+ip_str
        #print cmd
        output = os.popen(cmd)
        flag = False
        for line in list(output):
                if not line:
                        continue
                if str(line).upper().find("TTL") >=0:
                        flag = True
                        break
        if flag:
                ips[ip_str]=ip_str
                try:
                        name = socket.gethostbyaddr(ip_str)
                        ips[ip_str]=name[0]
                except Exception,e:
                        pass



if __name__ == "__main__":
        start = time.time()
        get_ip_file()
        print "scanning..."
        for i in range(1,255):
                ip=str(sys.argv[1]).split(".")
                ip=ip[0]+"."+ip[1]+"."+ip[2]+"."+str(i)
                thread.start_new_thread(ip_ping, (ip,))
                time.sleep(0.1)
        #print ips
        for k,v in ips.items():
                print k,'  --  ',v
        print time.time()-start

发表评论

电子邮件地址不会被公开。 必填项已用*标注