[英]c# ping between two ip addresses
How can i check if two servers are connected from a third server in c#? 如何检查C#中的第三台服务器是否连接了两台服务器? I am in server A and i want to know if Server B and Server C are connected.
我在服务器A中,我想知道服务器B和服务器C是否已连接。 I only have the code to check if I am connected to server B or C. What I have:
我只有用于检查是否连接到服务器B或C的代码。我拥有:
public bool AreConnected(string ip)
{
bool connected= false;
Ping p = new Ping();
try
{
PingReply reply = p.Send(ip);
connected = reply.Status == IPStatus.Success;
}
catch (PingException)
{
// Discard PingExceptions and return false;
}
return connected;
}
This might not be the best approach, and it requires admin privileges on machine B, but it works. 这可能不是最佳方法,它需要计算机B上的管理员权限,但它可以工作。
Use PsExec . 使用PsExec 。 This tool allows you to run a command on a remote machine.
该工具允许您在远程计算机上运行命令。
Create a command line program that take the ip address as a command line parameter, pings the ip address and outputs the result. 创建一个以ip地址作为命令行参数的命令行程序,ping ip地址并输出结果。
Then run PsExec (from C# code) to execute such program on machine B and collect the result (from code also). 然后运行PsExec(从C#代码开始)在计算机B上执行此类程序并收集结果(也从代码开始)。
You will need to use Process.Start to be able to execute the PsExec command from C# code. 您将需要使用Process.Start才能从C#代码执行PsExec命令。
I used PsExec and works fine, below my code maybe could help someone else, 我使用了PsExec并运行良好,在我的代码下方可能可以帮助其他人,
public bool IsPingable(string servA, string servB)
{
string path = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "\\Resources\\PsExec.exe";
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = path;
p.StartInfo.Arguments = @"\\" + servA + " ping " + servB + " -n 1";
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
if (!output.Contains("100% loss"))
{
return true;
}
else
{
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.