67 lines
2.0 KiB
C#
67 lines
2.0 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Net.NetworkInformation;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Epost.Common
|
||
{
|
||
public class PingHelper
|
||
{
|
||
public bool pingSender(string ip, int time)
|
||
{
|
||
Ping ping = new Ping();
|
||
PingReply reply = ping.Send(ip, time);//第一个参数为ip地址,第二个参数为ping的时间 120
|
||
if (reply.Status == IPStatus.Success)
|
||
{
|
||
return true;
|
||
//ping的通
|
||
}
|
||
|
||
else
|
||
{
|
||
return false;
|
||
//ping不通
|
||
|
||
}
|
||
}
|
||
public void getmac()
|
||
{
|
||
|
||
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
|
||
|
||
//远程服务器ip
|
||
string remoteHostNameAddress = "192.168.3.137";
|
||
//构造Ping实例
|
||
Ping pingSender = new Ping();
|
||
//Ping选项设置
|
||
PingOptions options = new PingOptions();
|
||
options.DontFragment = true;
|
||
//测试数据
|
||
string data = "test data abcabc";
|
||
byte[] buffer = Encoding.ASCII.GetBytes(data);
|
||
//设置超时时间
|
||
int timeout = 120;
|
||
//调用同步send方法发送数据,将返回结果保存至PingReply实例
|
||
PingReply reply = pingSender.Send(remoteHostNameAddress, timeout, buffer, options);
|
||
if (reply.Status == IPStatus.Success)
|
||
{
|
||
foreach (NetworkInterface adapter in adapters)
|
||
{
|
||
string address = "答复的主机地址:" + reply.Address.ToString();
|
||
string RoundtripTime = "往返时间:" + reply.RoundtripTime;
|
||
string Ttl = "生存时间(TTL):" + reply.Options.Ttl;
|
||
string GetPhysicalAddress = "MAC地址:" + adapter.GetPhysicalAddress();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
}
|