使用GateWay IP获取网络上的所有设备IP
本文关键字:IP GateWay 获取 网络 使用 | 更新日期: 2023-09-27 18:34:31
我必须获取路由器/门路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
。
SO这里建议的AddOne
方法也没有返回它。
任何好主意。
这是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
}