【问题标题】:Using a GateWay IP to get all devices IP on a network使用网关 IP 获取网络上的所有设备 IP
【发布时间】:2015-02-17 01:20:03
【问题描述】:

我必须获取路由器/网关 IP,然后每次都增加最后一个八位字节以扫描所有设备。我使用函数 NetworkGateway() 获取网关 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 也没有返回。

任何好主意。

【问题讨论】:

  • 相关代码在哪里? IPAddress.Parse 不是一种新方法,如果它不起作用,人们会在十年前注意到它。还有xxx 是什么意思? `GetAddressBytes 返回一个字节数组,而不是字符串。
  • 更新了上面的代码
  • GetAddressBytes() 给19216801 传递给它一个参数192.168.0.1 。我可以做什么19216801 来获得要求
  • 正如我所说,GetAddressBytes 返回一个字节数组。 19216801 不是字节数组。您还没有发布相关代码,所以帮助您非常困难。您可以轻松地将最后一个字节的值从 1 更改为 255,并从数组中创建一个新的 IPAddress
  • 好的,我做到了并作为答案发布

标签: c# ip-address


【解决方案1】:

这是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

            }

【讨论】:

  • 您可以从字节数组中创建IPAddress,无需将其转换为字符串
  • 我得到了我需要的东西。我只想要最后一个元素来增加它,这可以使用 Split 。
猜你喜欢
  • 2015-02-11
  • 1970-01-01
  • 2012-11-04
  • 2016-11-02
  • 2013-03-31
  • 1970-01-01
  • 2018-09-01
  • 1970-01-01
  • 2023-04-01
相关资源
最近更新 更多