/// <summary>
/// 尝试Ping指定IP是否能够Ping通
/// </summary>
/// <param name="strIP">指定IP</param>
/// <returns>true 是 false 否</returns>
public static bool IsPingIP(string strIP)
{
try
{
//创建Ping对象
Ping ping = new Ping();
//接受Ping返回值
PingReply reply = ping.Send(strIP, 1000);
//Ping通
return true;
}
catch
{
//Ping失败
return false;
}
}
//得到网关地址
public static string GetGateway()
{
//网关地址
string strGateway = "";
//获取所有网卡
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
//遍历数组
foreach (var netWork in nics)
{
//单个网卡的IP对象
IPInterfaceProperties ip = netWork.GetIPProperties();
//获取该IP对象的网关
GatewayIPAddressInformationCollection gateways = ip.GatewayAddresses;
foreach (var gateWay in gateways)
{
//如果能够Ping通网关
if (IsPingIP(gateWay.Address.ToString()))
{
//WriteLog("Gateway:" + gateWay.Address.ToString());
//得到网关地址
if (gateWay.Address.AddressFamily == AddressFamily.InterNetwork)
{
if (!gateWay.Address.ToString().Contains("10"))
{
strGateway = gateWay.Address.ToString();
//WriteLog("realGateway:" + strGateway);
//跳出循环
break;
}
}
}
}
//如果已经得到网关地址
if (strGateway.Length > 0)
{
//跳出循环
break;
}
}
//返回网关地址
return strGateway;
}
private static string GetTunnelIP()//获取虚拟网卡IP信息
{
try
{
string Info = "";
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
if (adapter.Description == "RSpeedoVPN")
{
//获取以太网卡网络接口信息
IPInterfaceProperties ip = adapter.GetIPProperties();
//获取单播地址集
UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
foreach (UnicastIPAddressInformation ipadd in ipCollection)
{
//InterNetwork IPV4地址 InterNetworkV6 IPV6地址
if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
{
Info = ipadd.Address.ToString();//获取ip
Console.WriteLine(Info[0]);
break;
}
}
break;
}
}
tunnelserverip = Info;
return Info;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return null;
}
}