C# winform版 nbtstat

参考:http://www.cnblogs.com/geqinggao/archive/2013/01/21/2869644.html

一、nbtstat命令

显示基于 TCP/IP 的 NetBIOS (NetBT) 协议统计资料、本地计算机和远程计算机的 NetBIOS 名称表和 NetBIOS 名称缓存。Nbtstat 可以刷新 NetBIOS 名称缓存和使用 Windows Internet 名称服务 (WINS) 注册的名称。使用不带参数的 nbtstat 显示帮助。

百科详情:http://baike.baidu.com/link?url=irHv_hhUAktsb71Notle85v7UtvUCi6Rcdb79dP8yQv_rrMR62m5_x1Vhg8FKuwr

二、实现效果

三、winform实现

引用:

using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;

 

 /// <summary>

        /// 默认加载

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void Form1_Load(object sender, EventArgs e)

        {

            txtIP.Text = "192.168.1.101"; //ip地址输入框

        }



        /// <summary>

        /// 验证IP地址

        /// </summary>

        /// <param name="ipAddress"></param>

        /// <returns></returns>

        public static bool IsIPAddress(string ip)

        {



            if (string.IsNullOrEmpty(ip) || ip.Length < 7 || ip.Length > 15) return false;



            string regformat = @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$";



            Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);



            return regex.IsMatch(ip);



        }



        private void btnNbtstat_Click(object sender, EventArgs e)

        {

            txtInfo.Text = "";  //文本信息显示框

            string ipStr = txtIP.Text;

            if (string.IsNullOrWhiteSpace(ipStr))

            {

                MessageBox.Show("IP不能为空!");

            }

            if (!IsIPAddress(ipStr))

            {

                MessageBox.Show("IP格式不正确!");

                return;

            }



            byte[] bs = new byte[50] { 0x0, 0x00, 0x0, 0x10, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0x43, 0x4b, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x0, 0x0, 0x21, 0x0, 0x1 };

            byte[] Buf = new byte[500];

            byte[,] recv = new byte[18, 28];

            string str = "", strHost = "", Group = "", User = "", strMac = "";

            int receive, macline = 0, usernum = 0;

            string[] domainuser = new string[2];

            domainuser[0] = "";

            domainuser[1] = "";



            try

            {

                IPEndPoint senderTest = new IPEndPoint(IPAddress.Any, 0);

                EndPoint Remote = (EndPoint)senderTest;



                IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ipStr), 137);



                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);



                server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);

                server.SendTo(bs, bs.Length, SocketFlags.None, ipep);

                receive = server.ReceiveFrom(Buf, ref Remote);

                server.Close();



                if (receive > 0)

                {

                    recv = new byte[18, (receive - 56) % 18];



                    for (int k = 0; k < (receive - 56) % 18; k++)

                    {

                        for (int j = 0; j < 18; j++)

                        {

                            recv[j, k] = Buf[57 + 18 * k + j];

                        }

                    }



                    for (int k = 0; k < (receive - 56) % 18; k++)

                    {

                        str = "";

                        if (System.Convert.ToString(recv[15, k], 16) == "0" && (System.Convert.ToString(recv[16, k], 16) == "4" || System.Convert.ToString(recv[16, k], 16) == "44"))

                        {

                            for (int j = 0; j < 15; j++)

                            {

                                str += System.Convert.ToChar(recv[j, k]).ToString();

                            }

                            strHost = str.Trim();

                        }



                        if (System.Convert.ToString(recv[15, k], 16) == "0" && (System.Convert.ToString(recv[16, k], 16) == "84" || System.Convert.ToString(recv[16, k], 16).ToUpper() == "C4"))

                        {

                            for (int j = 0; j < 15; j++)

                            {

                                str += System.Convert.ToChar(recv[j, k]).ToString();

                            }

                            Group = str.Trim();

                        }



                        if (System.Convert.ToString(recv[15, k], 16) == "3" && (System.Convert.ToString(recv[16, k], 16) == "4" || System.Convert.ToString(recv[16, k], 16) == "44"))

                        {

                            for (int j = 0; j < 15; j++)

                            {

                                str += System.Convert.ToChar(recv[j, k]).ToString();

                            }

                            domainuser[usernum] = str.Trim();

                            usernum++;

                        }



                        if (System.Convert.ToString(recv[15, k], 16) == "0" && System.Convert.ToString(recv[16, k], 16) == "0" && System.Convert.ToString(recv[17, k], 16) == "0")

                        {

                            macline = k;



                            for (int i = 0; i < 6; i++)

                            {

                                if (i < 5)

                                {

                                    strMac += System.Convert.ToString(recv[i, macline], 16).PadLeft(2, '0').ToUpper() + ":";

                                }

                                if (i == 5)

                                {

                                    strMac += System.Convert.ToString(recv[i, macline], 16).PadLeft(2, '0').ToUpper();

                                }

                            }

                            k = (receive - 56) % 18;

                        }

                    }

                    User = domainuser[1];

                    if (string.IsNullOrEmpty(domainuser[1])) { User = domainuser[0]; }



                    txtInfo.Text = "主机:" + strHost + Environment.NewLine + "用户组:" + Group + Environment.NewLine + "用户:" + User + Environment.NewLine + "MAC地址:" + strMac;



                }

            }

            catch (SocketException ex)

            {

                Console.WriteLine(ex.Message);

            }

        }

 

 

 文章推荐:

快速局域网扫描,使用Ping类

http://www.cnblogs.com/wind8250/archive/2008/05/23/1205427.html

 

LanContain是具有多种网络管理功能的工具软件

http://www.cnblogs.com/mossan/archive/2008/05/05/1183749.html

 

你可能感兴趣的:(WinForm)