123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- namespace DllEapFileUpload
- {
- public class ArpTool
- {
- /// <summary>
- /// 获取ARP查询字符串
- /// </summary>
- /// <returns></returns>
- private static string GetARPResult()
- {
- Process p = null;
- string output = string.Empty;
- try
- {
- p = Process.Start(new ProcessStartInfo("arp", "-a")
- {
- CreateNoWindow = true,
- UseShellExecute = false,
- RedirectStandardOutput = true
- });
- output = p.StandardOutput.ReadToEnd();
- }
- catch (Exception ex)
- {
- //throw new Exception("IPInfo: Error Retrieving 'arp -a' Results", ex);
- LogBLL.Err("IPInfo: Error Retrieving 'arp -a' Results", ex);
- }
- finally
- {
- if (p != null)
- {
- p.Close();
- }
- }
- return output;
- }
- /// <summary>
- /// 获取IP地址与Mac地址对应数据表
- /// </summary>
- /// <returns>Mac-IP</returns>
- public static List<string[]> GetIPInfo()
- {
- try
- {
- var list = new List<string[]>();
- foreach (var arp in GetARPResult().Split(new char[] { '\n', '\r' }))
- {
- if (!string.IsNullOrEmpty(arp))
- {
- var pieces = (from piece in arp.Split(new char[] { ' ', '\t' })
- where !string.IsNullOrEmpty(piece)
- select piece).ToArray();
- if (pieces.Length == 3)
- {
- //pieces[1]Mac
- //pieces[0]IP
- list.Add(new string[2] { pieces[1], pieces[0] });
- }
- }
- }
- return list;
- }
- catch (Exception ex)
- {
- LogBLL.Err("IPInfo: Error Retrieving 'arp -a' Results", ex);
- return null;
- //throw new Exception("IPInfo: Error Parsing 'arp -a' results", ex);
- }
- }
- /// <summary>
- /// Mac地址转换为IP地址
- /// </summary>
- /// <param name="str">IP</param>
- /// <returns></returns>
- public static string GetIpFromMac(string str)
- {
- //str = str.Trim().ToString().Replace(":", "-");
- List<string[]> getipinfo = GetIPInfo();
- string ipinfo = (from ip in getipinfo
- where ip[1].ToLowerInvariant() == str.ToLowerInvariant()
- select ip[0]).FirstOrDefault();
- if (string.IsNullOrEmpty(ipinfo))
- {
- return "";
- }
- else
- {
- return ipinfo.ToUpperInvariant();
- }
-
- }
- }
- }
|