简体   繁体   中英

How can I use C# to determine which IP addresses on my local network are static/dynamic?

I am scanning for IPs on my local network, and one of the pieces of information I wish to receive when an IP is found is whether that IP is static or dynamic. So far, the closest question to mine that I have found only addresses checking your own machine. Here is my function:

    private bool isDynamic()
    {
        NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
        int currentSocketIndex = (new IPPacketInformation()).Interface;

        bool isDynamic = adapters[currentSocketIndex].GetIPProperties().GetIPv4Properties().IsDhcpEnabled;
        return isDynamic;
    }

I would like to modify this so that it can take an IP address, connect to the device that the IP belongs to (if it exists and does not block the connection), and determine whether that machine's IP is static/dynamic. At the moment, my function only checks my machine.

Could anyone point me in the right direction? I can't find references for doing something like this, but I doubt that it's impossible.

You can't find anything about it because that is impossible if you do not have access to the router. You can only find that by asking the router, which will most likely not tell you or require a router-specific request/protocol to tell you. Or you can ask the device directly which will not tell you whether its IP is static or dynamic unless you install an application on that machine that will answer your request. Some operating systems might provide this kind of service (like WMI on windows) already but you will never be able to tell for every network device.

BUT:

If you have a lot of time you can listen to the DHCP/BootP traffic and find you dynamic devices like this because dynamic devices will frequently refresh their session

A DIFFERENT APPROACH:

New idea but dangerous to do in important networks and I do not recommend it at all: Start your own DHCP server and start giving away addresses and see who accepts them then set them back. Please note that this could cause a HUGE amount of damage in company networks for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM