如何在 C# 中实现控制台命令 ping - a 10.10.10.1
how to implement console command ping - a 10.10.10.1 in C#
我能够使用 C# 实现 ping(以检查机器是否可用)。
但我无法获得控制台命令 ping [-a] targetipaddress 的等价物,它也将地址解析为主机名。
我需要在非域场景(例如工作组)的情况下使用。
示例:(a) 从命令控制台 ping 10.10.10.1 使用 ping
(b) ping -a 10.10.10.1
我用于 (a) 的代码
static string pingmachine(string ipaddress)
{
Ping ping = new Ping();
PingReply pingresult = ping.Send(ipaddress);
return pingresult.Status.ToString();
}
请帮我完成与示例 (b) 等效的操作。谢谢。
非常简单。
来自 msdn:
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
// args[0] can be an IPaddress or host name.
public static void Main (string[] args)
{
Ping pingSender = new Ping ();
PingOptions options = new PingOptions ();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
int timeout = 120;
PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
IPAddress ip;
if(IPAddress.TryParse(args[0],out ip))
{
var host = System.Net.Dns.GetHostEntry(ip);
Console.WriteLine("Host {0}",host.HostName);
}
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);
}
}
}
}
https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping(v=vs.110).aspx
我能够使用 C# 实现 ping(以检查机器是否可用)。
但我无法获得控制台命令 ping [-a] targetipaddress 的等价物,它也将地址解析为主机名。
我需要在非域场景(例如工作组)的情况下使用。
示例:(a) 从命令控制台 ping 10.10.10.1 使用 ping (b) ping -a 10.10.10.1
我用于 (a) 的代码
static string pingmachine(string ipaddress)
{
Ping ping = new Ping();
PingReply pingresult = ping.Send(ipaddress);
return pingresult.Status.ToString();
}
请帮我完成与示例 (b) 等效的操作。谢谢。
非常简单。
来自 msdn:
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
// args[0] can be an IPaddress or host name.
public static void Main (string[] args)
{
Ping pingSender = new Ping ();
PingOptions options = new PingOptions ();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
int timeout = 120;
PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
IPAddress ip;
if(IPAddress.TryParse(args[0],out ip))
{
var host = System.Net.Dns.GetHostEntry(ip);
Console.WriteLine("Host {0}",host.HostName);
}
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);
}
}
}
}
https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping(v=vs.110).aspx