一段扫描周边设备的代码

比较简单,死办法,获取本机相关网卡接口信息,再对该IP地址段进行扫描。不多解释,直接上代码了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace WNetWatch
{
    class Program
    {
        static void Main(string[] args)
        {
            Ping_all();
            Console.Read();
        }

        static List<IPAddress> NetworkGateways()
        {
            List<IPAddress> ips = new List<IPAddress>();

            foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (f.OperationalStatus == OperationalStatus.Up )
                {
                    foreach (GatewayIPAddressInformation d in f.GetIPProperties().GatewayAddresses)
                    {
                        ips.Add( d.Address);
                    }
                }
            }

            return ips;
        }

         static void Ping_all()
        {
            var ips = NetworkGateways();
            foreach(var gate_ip in ips)
            {
                //Extracting and pinging all other ip's.
                string[] array = gate_ip.ToString().Split('.');
                if (array.Length != 4)
                    continue;
                for (int i = 2; i <= 255; i++)
                {
                    string ping_var = array[0] + "." + array[1] + "." + array[2] + "." + i;
                    //time in milliseconds           
                    Ping(ping_var, 4, 4000);

                }
            }
        }

        static void Ping(string host, int attempts, int timeout)
        {
            for (int i = 0; i < attempts; i++)
            {
                Task.Factory.StartNew(() => {
                    try
                    {
                        System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
                        ping.PingCompleted += new PingCompletedEventHandler(PingCompleted);
                        ping.SendAsync(host, timeout, host);
                    }
                    catch
                    {

                    }
                });
            }
        }
        static void PingCompleted(object sender, PingCompletedEventArgs e)
        {
            string ip = (string)e.UserState;
            if (e.Reply != null && e.Reply.Status == IPStatus.Success)
            {
                string hostname = GetHostName(ip);
                string macaddres = GetMacAddress(ip);

                Console.WriteLine(string.Format("IP:{0}\tHostName:{1}\tMacAddress:{2}", ip, hostname, macaddres));

            }
            else
            {
               
            }
        }

         static string GetHostName(string ipAddress)
        {
            try
            {
                IPHostEntry entry = Dns.GetHostEntry(ipAddress);
                if (entry != null)
                {
                    return entry.HostName;
                }
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }

            return null;
        }


        //Get MAC address
         static string GetMacAddress(string ipAddress)
        {
            string macAddress = string.Empty;
            System.Diagnostics.Process Process = new System.Diagnostics.Process();
            Process.StartInfo.FileName = "arp";
            Process.StartInfo.Arguments = "-a " + ipAddress;
            Process.StartInfo.UseShellExecute = false;
            Process.StartInfo.RedirectStandardOutput = true;
            Process.StartInfo.CreateNoWindow = true;
            Process.Start();
            string strOutput = Process.StandardOutput.ReadToEnd();
            string[] substrings = strOutput.Split('-');
            if (substrings.Length >= 8)
            {
                macAddress = substrings[3].Substring(Math.Max(0, substrings[3].Length - 2))
                         + "-" + substrings[4] + "-" + substrings[5] + "-" + substrings[6]
                         + "-" + substrings[7] + "-"
                         + substrings[8].Substring(0, 2);
                return macAddress;
            }

            else
            {
                return "OWN Machine";
            }
        }    

    }
}


你可能感兴趣的:(一段扫描周边设备的代码)