HomeBlogMagic

C# GetIP from MAC-Address

Mit dem program arp kann über das Netzwerk anhand der MAC-Adresse die zugehörige IP-Adresse ermittelt werden.
Für C# habe ich dazu folgende Funktion geschrieben:

static string GetIp(string sMacAddress)
{
  string sIp = "";
  string sFormatedMac;
  if(sMacAddress.Length == 12)
  {
    sFormatedMac = "";
    for (int i = 0; i < 12; i += 2)
    {
      sFormatedMac += sMacAddress[i];
      sFormatedMac += sMacAddress[i+1];
      if(i+2  < 12)
        sFormatedMac += "-";
    }
    sFormatedMac = sFormatedMac.ToLower();
  }
  else
  {
    sFormatedMac = sMacAddress.ToLower();
  }
  bool bIpIsFound = false;
  int iTimeout = 10;
  while (bIpIsFound == false && iTimeout-- > 0)
  {
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = true;
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardOutput = true;
    startInfo.FileName  = "arp";
    startInfo.Arguments = "-a";
    Process process = Process.Start(startInfo);
    string sArpData;
    do
    {
      sArpData = process.StandardOutput.ReadToEnd();
      string[] sArpEntries = sArpData.Split('\n');
      foreach (string sArpEntry in sArpEntries)
      {
        if (sArpEntry.ToLower().Contains(sFormatedMac))
        {
          List<string> slArpValues = new List<String>(sArpEntry.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries));
          if (slArpValues.Last() == "\r")
            slArpValues.RemoveAt(slArpValues.Count - 1);
          if (slArpValues.Count == 3)
          {
            sIp = slArpValues[0];
            bIpIsFound = true;
            Console.WriteLine("MacFound :)");
          }
        }
      }
    } while (sArpData.Length > 0);
    System.Threading.Thread.Sleep(1000);
  }
  return sIp;
}

Hin und wieder benutze ich die selbe Funktion auch in der Powershell:

$sArpCode = @"
using System;
namespace Arp
{
public class CArp
{
static string GetIp(string sMacAddress)
{
  string sIp = "";
  string sFormatedMac;
  if(sMacAddress.Length == 12)
  {
    sFormatedMac = "";
    for (int i = 0; i < 12; i += 2)
    {
      sFormatedMac += sMacAddress[i];
      sFormatedMac += sMacAddress[i+1];
      if(i+2  < 12)
        sFormatedMac += "-";
    }
    sFormatedMac = sFormatedMac.ToLower();
  }
  else
  {
    sFormatedMac = sMacAddress.ToLower();
  }
  bool bIpIsFound = false;
  int iTimeout = 10;
  while (bIpIsFound == false && iTimeout-- > 0)
  {
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = true;
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardOutput = true;
    startInfo.FileName  = "arp";
    startInfo.Arguments = "-a";
    Process process = Process.Start(startInfo);
    string sArpData;
    do
    {
      sArpData = process.StandardOutput.ReadToEnd();
      string[] sArpEntries = sArpData.Split('\n');
      foreach (string sArpEntry in sArpEntries)
      {
        if (sArpEntry.ToLower().Contains(sFormatedMac))
        {
          List<string> slArpValues = new List<String>(sArpEntry.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries));
          if (slArpValues.Last() == "\r")
            slArpValues.RemoveAt(slArpValues.Count - 1);
          if (slArpValues.Count == 3)
          {
            sIp = slArpValues[0];
            bIpIsFound = true;
            Console.WriteLine("MacFound :)");
          }
        }
      }
    } while (sArpData.Length > 0);
    System.Threading.Thread.Sleep(1000);
  }
  return sIp;
}
}
}";
Add-Type -TypeDefinition $sArpCode -Language CSharp 
iex "[Arp.CArp]::Main(`"08-00-27-86-C2-6C`")"
Permalink: https://adirmeier.de/Blog/ID_135
Tags: Blog, C#, ip, powersheel, windowsvon am 2017-06-08