1.使用CMD (不好用,且編譯在網站後無法使用)

using System.Diagnostics;
public string GetMac(string IP)
        {
            string str1 = String.Empty;
            try
            {
                string str2 = string.Empty;
                str2 = RunCmd("nbtstat -A " + IP);
                if (str2.ToLower().IndexOf("mac address", 0) > -1)
                {
                    str2 = str2.Trim();
                    str1 = str2.Substring(str2.ToLower().IndexOf("mac address", 0), 31);
                    str1 = str1.Remove(0, 14);
                    str1 = str1.ToUpper();
                }
                else
                    str1 = "";
            }
            catch
            {
                Response.Write("<Script>alert('抓不到')</Script>");
                str1 = "";
            }
            return str1;
        }
        public string RunCmd(string command)
        {
            //實例一個Process類,啟動一個獨立進程  
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            string result = "";
            //Process類有一個StartInfo屬性,這個是ProcessStartInfo類,包括了一些屬性和方法,下面我們用到了他的幾個屬性:  
            p.StartInfo.FileName = "cmd.exe";           //設定程序名  
            p.StartInfo.Arguments = "/c " + command;    //設定程式執行參數  
            p.StartInfo.UseShellExecute = false;        //關閉Shell的使用 
            p.StartInfo.RedirectStandardInput = true;   //重定向標準輸入  
            p.StartInfo.RedirectStandardOutput = true;  //重定向標準輸出  
            p.StartInfo.RedirectStandardError = false;   //重定向錯誤輸出  
            p.StartInfo.CreateNoWindow = true;          //設置不顯示窗口  


            p.Start();   //啟動  


            //p.StandardInput.WriteLine(command);       //也可以用這種方式輸入要執行的命令  
            //p.StandardInput.WriteLine("exit");        //不過要記得加上Exit要不然下一行程式執行的時候會當機  
            //p.WaitForExit(1);
            //int Timeout=2;
            //while( Timeout > 0 && !p.HasExited)
            //{
            //    System.Threading.Thread.Sleep(1000);
            //    Timeout -= 1;
            //} 
            try
            {
                result = p.StandardOutput.ReadToEnd();
            }
            catch
            {
                result = "";
            }
            if (!p.HasExited)
            {
                p.Kill();
            }
            //强制关闭进程->無法成功
            //for (int i = 0; i < System.Diagnostics.Process.GetCurrentProcess().Modules.Count; i++)
            //{
            //    if (System.Diagnostics.Process.GetCurrentProcess().Modules[i].FileName.ToLower().IndexOf("cmd", 0) > -1)
            //     {
            //         string exeName = System.Diagnostics.Process.GetCurrentProcess().Modules[i].FileName;
            //         string[] exeArray = exeName.Split('\\');
            //         RunCmd("taskkill /im " + exeArray[exeArray.Length - 1] + " /f "); 
            //     }
            //}
            p.Dispose();
            p.Close();
            return result;        //從輸出流取得命令執行結果
        }

 

2. 使用ARP(簡單好用但只能查同網段喔)


using System.Runtime.InteropServices;
using System.Net;

        [DllImport("iphlpapi.dll", ExactSpelling = true)]
        public static extern int SendARP(int DestIP, int SrcIP,ref byte[] MacAddr, ref uint PhyAddrLen);


        static private string GetRemoteMAC(IPAddress LocalIP, IPAddress RemoteIP)
        {
            byte[] MacAddr = new byte[6];
            uint PhyAddrLen = (uint)MacAddr.Length;
            string temp = "";
            int res = SendARP((int)RemoteIP.Address, (int)LocalIP.Address, MacAddr, ref PhyAddrLen);
            //如果有回達 res就是0
            if (res == 0)
            {
                for (int i = 0; i < PhyAddrLen; i++)
                {
                    //轉成看得懂的MAC型態
                    temp += MacAddr[i].ToString("x2") + "-";
                }
                //減掉最後一個"-" 並轉成大寫
                temp = (temp.Remove(temp.Length - 1, 1)).ToUpper();
                return temp;
            }
            else
            {
                return "";
            }
        }

arrow
arrow
    全站熱搜

    caramels 發表在 痞客邦 留言(0) 人氣()