IP解析计算机名称

 

#-*- coding: UTF-8 -*-
import subprocess,sys,threading
reload(sys)
sys.setdefaultencoding('utf-8')

def ip2name(mutex,ip,fp,kw):
    nbtstatchild = subprocess.Popen(['nbtstat','-A',ip],shell=True,stdout=subprocess.PIPE)
    while nbtstatchild.poll() == None:
        rsts = nbtstatchild.stdout.readlines()
        if len(rsts) <> 0:
            for i in rsts:
                ii = i.strip().decode('GBK')
                if ii.find(kw) <> -1:
                    if mutex.acquire(1):
                        with open(fp,'ab') as fw:
                            fw.write(ip)
                            for line in rsts:
                                fw.write(line.strip().decode('GBK'))
                        mutex.release()
        else:
            pass


if __name__ =='__main__':
    lst=[]
    bb=range(4,6) #IP第三位
    bb.extend([69,71,74]) #IP第三位
    dd=range(2,255) #IP第四位
    for b in bb:
        for d in dd:
            ip = '192.168.' + str(b) + '.' + str(d)
            lst.append(ip)

           
    fp='d:\\ip.txt' #将结果写到该文件中
    kw='XS' #要查找的关键字
    tnum=30 #线程数量

    mutex = threading.Lock()
    for i in range(0,len(lst),tnum):
            threads=[]
            for IP in lst[i:i+tnum]:
                print IP
                t = threading.Thread(target=ip2name,args=(mutex,IP,fp,kw))
                t.setDaemon(True)
                t.start()
                threads.append(t)
            for t in threads:
                t.join()

 

posted on 2016-06-13 16:07  momingliu11  阅读(508)  评论(0编辑  收藏  举报