List of IP addresses/hostnames from local network in Python

前端 未结 10 2434
暗喜
暗喜 2020-12-01 00:05

How can I get a list of the IP addresses or host names from a local network easily in Python?

It would be best if it was multi-platform, but it needs to work on Mac

10条回答
  •  猫巷女王i
    2020-12-01 00:36

    I have done following code to get the IP of MAC known device. This can be modified accordingly to obtain all IPs with some string manipulation. Hope this will help you.

    #running windows cmd line  statement and put output into a string
    cmd_out = os.popen("arp -a").read()
    line_arr = cmd_out.split('\n')
    line_count = len(line_arr)
    
    
    #search in all lines for ip
    for i in range(0, line_count):
        y = line_arr[i]
        z = y.find(mac_address)
    
        #if mac address is found then get the ip using regex matching
        if z > 0:
            ip_out= re.search('[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+', y, re.M | re.I)
    

提交回复
热议问题