扫描局域网内的ip和主机名

1. 目的

今天发现我配置的一台电脑ip被人占用了,所以准备写个程序扫描一下局域网内所有正在使用的ip和主机名

2. 实现--直接上代码

import time
import threading
import socket

threads = []

def get_hostname(ip):
    try:
        (name, aliaslist, addresslist) = socket.gethostbyaddr(ip)
        print name , '  ', ip
                
    except Exception as e:
        return
    
      
def find_ip(ip_prefix):  
       
    for i in range(2,255):  
        ip = '%s.%s'%(ip_prefix,i)
        th=threading.Thread(target=get_hostname,args=(ip,))
        threads.append(th)
  
    
if __name__ == "__main__":  
    print "start time %s"%time.ctime()  
    
    find_ip('192.168.2')  
    for t in threads:
        t.start()
    
    # 主线程中等待所有子线程退出
    for t in threads:
        t.join()   

    print "end time %s"%time.ctime()  
原文地址:https://www.cnblogs.com/zjzyh/p/5610288.html