您的位置:首页 > 理论基础 > 计算机网络

多线程测试网络情况,不同网段的机器用nbtstat判断是否开机

2009-07-27 08:31 281 查看
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net.NetworkInformation;
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
namespace 线程
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <=254; i++)
{
Work w = new Work();
w.IP ="192.168.1."+i.ToString();
Thread t = new Thread(new ThreadStart(w.main));
t.Start();
Thread.Sleep(1000);
}
Console.ReadLine();
}

}
public class Work
{
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
public string count ;
public int i = 0;
public string IP;
public void main()
{
//实现ping功能
Ping pingSender = new Ping();
PingReply reply =pingSender.Send(IP);
if (reply.Status == IPStatus.Success)
{
long RoundtripTime = reply.RoundtripTime;
if(IP==reply.Address.ToString())
{
Console.Write(IP + "可以ping通,延时"+reply.RoundtripTime.ToString());
}
if (RoundtripTime > 10)
{
Console.Write("延时过大,时间为"+RoundtripTime.ToString());
}
Console.WriteLine();
}
else//利用netbios协议,用命令nbtstat判断客户端是否在线,尤其用于不同网段无法ping通的机器
{
string s = GetNetCardAddress(IP);
if ("".Equals(s))
{
Console.WriteLine(IP + "不通");
}
else
{
Console.WriteLine(IP + "通,MAC为" + s);
}
}
}
//获取远程主机mac地址
public string GetNetCardAddress(string strIp)
{
string mac = "";
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "nbtstat";
process.StartInfo.Arguments = " -a " + strIp;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string output = process.StandardOutput.ReadToEnd().Replace(" ", "").Replace("/n", "");
process.Close();
int length = output.IndexOf("MACAddress=");
if (length > 0)
{
mac =output.Substring(length + 11, 17);
}
return mac;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐