using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Text;
namespace Common
{
public class NetTools
{
public static string PingIPAddresses(List<string> ipAddresses)//无返回值void改成string返回ping的结果描述
{
StringBuilder sb = new StringBuilder();
string res = string.Empty;
foreach (var ipAddress in ipAddresses)
{
try
{
Ping pingSender = new Ping();
PingReply reply = pingSender.Send(ipAddress);
if (reply.Status == IPStatus.Success)
{
//Console.WriteLine($"Address: {ipAddress} is reachable. Round trip time: {reply.RoundtripTime}ms");
res= $"Address: {ipAddress} is 可达. 去+回耗时: {reply.RoundtripTime}ms";
}
else
{
//Console.WriteLine($"Address: {ipAddress} is not reachable. Status: {reply.Status}");
res= $"Address: {ipAddress} is 不可达. 问题描述: {reply.Status}";
}
}
catch (PingException)
{
//Console.WriteLine($"Ping operation could not be performed on address: {ipAddress}");
res= $"此IP不能执行ping操作: {ipAddress}";
}
sb.Append(res+"\r\n");
}
return sb.ToString();
}
}
}