使用GateWay IP获取网络上的所有设备IP

时间:2014-12-18 09:42:54

标签: c# ip-address

我必须得到Router / GateWay IP,然后每次递增最后一个八位字节来扫描所有设备。我使用函数NetworkGateway()获取GateWay IP。

        //gets the GateWay IP
        string gate_ip= NetworkGateway();

        //mechanism to parse ,get the last octet and increment it everytime goes here so that    ping_var = "192.168.0" + "." + i; have a fixed "192.168.0"

            for (int i = 2; i <= 255; i++)
            {
                 //changes i for every unique device
                string ping_var = "192.168.0" + "." + i;
            }




     IPAddress address = IPAddress.Parse(ipAddress);


  // gives 192.168.0.1 when passed "192.168.0.1 "
  Console.WriteLine("Parsing your input string: " + "\"" + ipAddress + "\"" + " produces this address (shown in its standard notation): "+ address.ToString());


//Returns  the Bytes of IP
     Byte[] bytes = address.GetAddressBytes();
    for (int i = 0; i < bytes.Length; i++) 
       {
         Console.Write(bytes[i]);
       }  

为此目的,我先使用IPAddress.Parse(ipAddress)然后使用GetAddressBytes(),但它刚刚返回xxxxxxxx,但无法用于递增。

建议的AddOne方法here at SO也没有返回。

任何好主意。

1 个答案:

答案 0 :(得分:0)

这是Split()的一个简单用法,我做到了。

    string gate_ip= NetworkGateway();
        string[] array = gate_ip.Split('.');

            for (int i = 2; i <= 255; i++)
            {

                string ping_var = array[0] +"."+array[1]+"."+array[2]+ "." + i;
                // do scanning here

            }
相关问题