有没有人有C#代码方便ping和traceroute到目标计算机?我正在寻找一个纯代码解决方案,而不是我现在正在做的,它调用ping.exe和tracert.exe程序并解析输出.我想要一些更强大的东西.
鉴于我今天必须写一个TraceRoute类,我想我也可以分享源代码.
using System.Collections.Generic; using System.Net.NetworkInformation; using System.Text; using System.Net; namespace Answer { public class TraceRoute { private const string Data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; public static IEnumerableGetTraceRoute(string hostNameOrAddress) { return GetTraceRoute(hostNameOrAddress, 1); } private static IEnumerable GetTraceRoute(string hostNameOrAddress, int ttl) { Ping pinger = new Ping(); PingOptions pingerOptions = new PingOptions(ttl, true); int timeout = 10000; byte[] buffer = Encoding.ASCII.GetBytes(Data); PingReply reply = default(PingReply); reply = pinger.Send(hostNameOrAddress, timeout, buffer, pingerOptions); List result = new List (); if (reply.Status == IPStatus.Success) { result.Add(reply.Address); } else if (reply.Status == IPStatus.TtlExpired || reply.Status == IPStatus.TimedOut) { //add the currently returned address if an address was found with this TTL if (reply.Status == IPStatus.TtlExpired) result.Add(reply.Address); //recurse to get the next address... IEnumerable tempResult = default(IEnumerable ); tempResult = GetTraceRoute(hostNameOrAddress, ttl + 1); result.AddRange(tempResult); } else { //failure } return result; } } }
对于想要/需要它的人来说,这是一个VB版本
Public Class TraceRoute Private Const Data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" Public Shared Function GetTraceRoute(ByVal hostNameOrAddress As String) As IEnumerable(Of IPAddress) Return GetTraceRoute(hostNameOrAddress, 1) End Function Private Shared Function GetTraceRoute(ByVal hostNameOrAddress As String, ByVal ttl As Integer) As IEnumerable(Of IPAddress) Dim pinger As Ping = New Ping Dim pingerOptions As PingOptions = New PingOptions(ttl, True) Dim timeout As Integer = 10000 Dim buffer() As Byte = Encoding.ASCII.GetBytes(Data) Dim reply As PingReply reply = pinger.Send(hostNameOrAddress, timeout, buffer, pingerOptions) Dim result As List(Of IPAddress) = New List(Of IPAddress) If reply.Status = IPStatus.Success Then result.Add(reply.Address) ElseIf reply.Status = IPStatus.TtlExpired Then 'add the currently returned address result.Add(reply.Address) 'recurse to get the next address... Dim tempResult As IEnumerable(Of IPAddress) tempResult = GetTraceRoute(hostNameOrAddress, ttl + 1) result.AddRange(tempResult) Else 'failure End If Return result End Function End Class
虽然基类库包含Ping,但BCL不包含任何tracert功能.
但是,快速搜索显示两个开源尝试,第一个在C#中是C++中的第二个:
http://www.codeproject.com/KB/IP/tracert.aspx
http://www.codeguru.com/Cpp/IN/network/basicnetworkoperations/article.php/c5457/
接下来是tracert
迄今为止比其他答案中存在的明显更好的C#实现.
public static IEnumerableGetTraceRoute(string hostname) { // following are the defaults for the "traceroute" command in unix. const int timeout = 10000; const int maxTTL = 30; const int bufferSize = 32; byte[] buffer = new byte[bufferSize]; new Random().NextBytes(buffer); Ping pinger = new Ping(); for (int ttl = 1; ttl <= maxTTL; ttl++) { PingOptions options = new PingOptions(ttl, true); PingReply reply = pinger.Send(hostname, timeout, buffer, options); if (reply.Status == IPStatus.TtlExpired) { // TtlExpired means we've found an address, but there are more addresses yield return reply.Address; continue; } if (reply.Status == IPStatus.TimedOut) { // TimedOut means this ttl is no good, we should continue searching continue; } if (reply.Status == IPStatus.Success) { // Success means the tracert has completed yield return reply.Address; } // if we ever reach here, we're finished, so break break; } }
其他答案中出现的陷阱包括:
这很懒惰.例如:它正确使用了枚举/迭代器,所以不必计算整个树,你可以通过打破你自己的消费循环来随时停止.
maxTTL
实现所以功能不会永远旋转.
bufferSize
与其他tracert实现一致的选项.
它超级简洁干净.它包含在一个方法中,比这里的其他选项要短得多.
对于ping部分,请查看MSDN 上的Ping类.