■ 네트워크 스캐너 사용하기

----------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Net;

using System.Net.NetworkInformation;

using System.Runtime.InteropServices;

using System.Text;

using System.Threading;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Import

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region ARP 보내기 - SendARP(destinationIP, sourceIP, macAddressByteArray, physicalAddressLength);

 

        /// <summary>

        /// ARP 보내기

        /// </summary>

        /// <param name="destinationIP">목적 IP</param>

        /// <param name="sourceIP">소스 IP</param>

        /// <param name="macAddressByteArray">MAC 주소 바이트 배열</param>

        /// <param name="physicalAddressLength">물리적 주소 길이</param>

        /// <returns>처리 결과</returns>

        [DllImport("iphlpapi.dll", ExactSpelling = true)]

        private static extern int SendARP(int destinationIP, int sourceIP, byte[] macAddressByteArray, ref uint physicalAddressLength);

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 스레드

        /// </summary>

        private Thread thread = null;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 폼 닫을 경우 처리하기 - Form_FormClosing(sender, e)

 

        /// <summary>

        /// 폼 닫을 경우 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_FormClosing(object sender, FormClosingEventArgs e)

        {

            if(this.thread != null)

            {

                this.thread.Abort();

            }

        }

 

        #endregion

        #region 조회 버튼 클릭시 처리하기 - searchButton_Click(sender, e)

 

        /// <summary>

        /// 조회 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void searchButton_Click(object sender, EventArgs e)

        {

            this.thread = new Thread(Search);

 

            this.thread.IsBackground = true;

 

            this.thread.Start();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 호스트명 구하기 - GetHostName(ipAddress)

 

        /// <summary>

        /// 호스트명 구하기

        /// </summary>

        /// <param name="ipAddress">IP 주소</param>

        /// <returns>호스트명</returns>

        private string GetHostName(IPAddress ipAddress)

        {

            try

            {

                IPHostEntry ipHostEntry = Dns.GetHostEntry(ipAddress);

 

                return ipHostEntry.HostName;

            }

            catch(Exception)

            {

                return ipAddress.ToString();

            }

        }

 

        #endregion

        #region MAC 주소 구하기 - GetMACAddress(ipAddressString)

 

        /// <summary>

        /// MAC 주소 구하기

        /// </summary>

        /// <param name="ipAddressString">IP 주소 문자열</param>

        /// <returns>MAC 주소</returns>

        private string GetMACAddress(string ipAddressString)

        {

            IPAddress ipAddress = IPAddress.Parse(ipAddressString);

 

            byte[] macAddressByteArrat = new byte[6];

 

            uint macAddressByteArratLength = (uint)macAddressByteArrat.Length;

 

            if(SendARP((int)ipAddress.Address, 0, macAddressByteArrat, ref macAddressByteArratLength) != 0)

            {

                return "ARP COMMAND FAILED";

            }

 

            string[] macAddressStringArray = new string[(int)macAddressByteArratLength];

 

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

            {

                macAddressStringArray[i] = macAddressByteArrat[i].ToString("x2");

            }

 

            return string.Join(":", macAddressStringArray);

        }

 

        #endregion

 

        #region 조회하기 - Search()

 

        /// <summary>

        /// 조회하기

        /// </summary>

        private void Search()

        {

            try

            {

                this.listView.Items.Clear();

 

                Application.DoEvents();

 

                IPAddress startIPAddress   = IPAddress.Parse(this.startIPAddressTextBox.Text);

                IPAddress endIPAddress     = IPAddress.Parse(this.endIPAddressTextBox.Text);

                IPAddress currentIPAddress = null;

 

                string hostName = null;

 

                int[] startIPAddressArray = new int[4];

                int[] endIPAddressArray   = new int[4];

 

                startIPAddressArray[0] = Convert.ToInt32(startIPAddress.GetAddressBytes()[0]);

                startIPAddressArray[1] = Convert.ToInt32(startIPAddress.GetAddressBytes()[1]);

                startIPAddressArray[2] = Convert.ToInt32(startIPAddress.GetAddressBytes()[2]);

                startIPAddressArray[3] = Convert.ToInt32(startIPAddress.GetAddressBytes()[3]);

 

                endIPAddressArray[0] = Convert.ToInt32(endIPAddress.GetAddressBytes()[0]);

                endIPAddressArray[1] = Convert.ToInt32(endIPAddress.GetAddressBytes()[1]);

                endIPAddressArray[2] = Convert.ToInt32(endIPAddress.GetAddressBytes()[2]);

                endIPAddressArray[3] = Convert.ToInt32(endIPAddress.GetAddressBytes()[3]);

 

                this.progressBar.Minimum = Convert.ToInt32(startIPAddress.GetAddressBytes()[3]);

                this.progressBar.Maximum = Convert.ToInt32(endIPAddress.GetAddressBytes()[3]);

                this.progressBar.Value   = Convert.ToInt32(startIPAddress.GetAddressBytes()[3]);

 

                for(int a = startIPAddressArray[0]; a <= endIPAddressArray[0]; a++)

                {

                    for(int b = startIPAddressArray[1]; b <= endIPAddressArray[1]; b++)

                    {

                        for(int c = startIPAddressArray[2]; c <= endIPAddressArray[2]; c++)

                        {

                            for(int d = startIPAddressArray[3]; d <= endIPAddressArray[3]; d++)

                            {

                                this.progressBar.Value = d;

 

                                currentIPAddress = IPAddress.Parse(a.ToString() + "." + b.ToString() + "." + c.ToString() + "." +

                                    d.ToString());

 

                                Ping        ping        = new Ping();

                                PingOptions pintOptions = new PingOptions();

 

                                pintOptions.DontFragment = true;

 

                                string message          = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

                                byte[] messageByteArray = Encoding.ASCII.GetBytes(message);

 

                                int timeout = 150;

 

                                PingReply pintReply = ping.Send(currentIPAddress, timeout, messageByteArray, pintOptions);

 

                                hostName = null;

 

                                string macAddress = null;

 

                                if(pintReply.Status == IPStatus.Success)

                                {

                                    hostName   = GetHostName(currentIPAddress);

                                    macAddress = GetMACAddress(currentIPAddress.ToString());

 

                                    ListViewItem item = new ListViewItem();

 

                                    item.Text = currentIPAddress.ToString();

                                    item.SubItems.Add(hostName);

                                    item.SubItems.Add(macAddress);

 

                                    this.listView.Items.Add(item);

                                }

 

                                if(d == this.progressBar.Maximum)

                                {

                                    MessageBox.Show("네트워크 스캔을 완료하였습니다.", "정보", MessageBoxButtons.OK,

                                        MessageBoxIcon.Information);

                                }

                            }

                        }

                    }

                }

            }

            catch(Exception exception)

            {

                MessageBox.Show(exception.Message, "에러", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            finally

            {

                this.thread.Abort();

            }

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요