List of IP addresses/hostnames from local network in Python

前端 未结 10 2416
暗喜
暗喜 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条回答
  • 2020-12-01 00:42

    If by "local" you mean on the same network segment, then you have to perform the following steps:

    1. Determine your own IP address
    2. Determine your own netmask
    3. Determine the network range
    4. Scan all the addresses (except the lowest, which is your network address and the highest, which is your broadcast address).
    5. Use your DNS's reverse lookup to determine the hostname for IP addresses which respond to your scan.

    Or you can just let Python execute nmap externally and pipe the results back into your program.

    0 讨论(0)
  • 2020-12-01 00:48

    Here is a small tool scanip that will help you to get all ip addresses and their corresponding mac addresses in the network (Works on Linux).

    https://github.com/vivkv/scanip

    0 讨论(0)
  • 2020-12-01 00:53

    One of the answers in this question might help you. There seems to be a platform agnostic version for python, but I haven't tried it yet.

    0 讨论(0)
  • 2020-12-01 00:56

    Try:

    import socket
    
    print ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1])
    
    0 讨论(0)
提交回复
热议问题