2016-12-06 60 views
-3

我想测试,如果我的机器可以通过其他网络实体ping通:如何知道一台机器是否可以用c#ping?

Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); 
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType); 
string allowed = mgr.LocalPolicy.CurrentProfile.IcmpSettings.AllowInboundEchoRequest; 
+6

为什么不使用['System.Net.Networkinformation.Ping'(https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping%28v=vs。 110%29.aspx?f = 255&MSPPError = -2147217396)class? –

+0

@WaiHaLee可能会发布这个答案,因为它是答案? ;) – nozzleman

回答

0

为了解某台机器是否可以被钉死;需要允许ICMP类型8 in和type 0 out。

public Boolean IsPingable() 
    { 
     Boolean icmpAllowed = false; 
     INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType); 
     Object allowedin8 = null; 
     Object restrictedin8 = null; 
     Object allowedout0 = null; 
     Object restrictedout0 = null; 
     mgr.IsIcmpTypeAllowed(NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V4,"127.0.0.1", 0, out allowedin0, out restrictedin0); 
     mgr.IsIcmpTypeAllowed(NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V4, "8.8.8.8", 8, out allowedout8, out restrictedout8); 
     if ((Boolean)allowedin0 && (Boolean)allowedout8) 
     { 
      icmpAllowed = true; 
     } 
     return icmpAllowed; 
    } 
0

您可以发送ping命令检查是否可以Ping通:

public static bool CanBePinged(string hostname) 
{ 
    if (string.IsNullOrWhiteSpace(hostname)) throw new ArgumentException("hostname"); 

    Ping p1 = new Ping(); 
    try 
    { 
     PingReply PR = p1.Send(hostname); 
     return PR.Status == IPStatus.Success; 
    }catch(Exception e) 
    { 
     return false; 
    } 
} 
0

使用System.Net.Networkinformation.Ping.Send(...)

您可以在几个方面,包括指定主机名,在这种情况下the MSDN sample是使用Ping

public static void SimplePing() 
{ 
    Ping pingSender = new Ping(); 
    PingReply reply = pingSender.Send ("www.contoso.com"); 

    if (reply.Status == IPStatus.Success) 
    { 
     Console.WriteLine ("Address: {0}", reply.Address.ToString()); 
     Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime); 
     Console.WriteLine ("Time to live: {0}", reply.Options.Ttl); 
     Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment); 
     Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length); 
    } 
    else 
    { 
     Console.WriteLine (reply.Status); 
    } 
} 

,或者你可以指定一个IPAddress,在这种情况下the MSDN sample是:

public static void LocalPing() 
{ 
    // Ping's the local machine. 
    Ping pingSender = new Ping(); 
    IPAddress address = IPAddress.Loopback; 
    PingReply reply = pingSender.Send (address); 

    if (reply.Status == IPStatus.Success) 
    { 
     Console.WriteLine ("Address: {0}", reply.Address.ToString()); 
     Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime); 
     Console.WriteLine ("Time to live: {0}", reply.Options.Ttl); 
     Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment); 
     Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length); 
    } 
    else 
    { 
     Console.WriteLine (reply.Status); 
    } 
} 
+0

我不想看看是否可以ping远程机器,但是我的机器是否可以ping通? –

+0

在第一个示例中,您可以将'“www.contoso.com”'更改为'localhost'并删除''生存时间''和''不要碎片''行(它们会抛出'NullReferenceException's ),它会让你知道你的机器是否响应ping。第二个示例**已经**,使用['IPAddress.Loopback'](https://msdn.microsoft.com/en-us/library/system.net.ipaddress.loopback),它将ping你的(本地)机。 –

+0

当机器无法ping通时,它会返回成功状态。 –

相关问题