close

透過 C# 下 arp -a 指令,得到表

ARP (Address Resolution Protocol)

位址解析通訊協定,

下這個指令可以得到登記在我這台電腦中的網際網路網址及實體位址。

程式碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace HelloMac
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string GetMac(string IP)
        {
            string dirResults = "";
            ProcessStartInfo psi = new ProcessStartInfo();
            Process proc = new Process();
            psi.FileName = "arp";
            psi.RedirectStandardInput = false;
            psi.RedirectStandardOutput = true;
            psi.Arguments = "-a "+IP;
            psi.UseShellExecute = false;
            psi.CreateNoWindow = true;
            try
            {
                proc = Process.Start(psi);
                dirResults = proc.StandardOutput.ReadToEnd();
                proc.WaitForExit();
            }
            catch (Exception)
            { }
            
            Match m = Regex.Match(dirResults, "\\w+\\-\\w+\\-\\w+\\-\\w+\\-\\w+\\-\\w\\w");

            if (m.ToString() != "")
            {
                return m.ToString();
            }
            else
            {
                return "找不到主機信息";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.AppendText(GetMac(textBox1.Text) + "\r\n");
        }
    }
}

 

結果:

arrow
arrow
    文章標籤
    程式語言 命令提示符
    全站熱搜

    史克威爾凱特 發表在 痞客邦 留言(0) 人氣()