Home > other >  Based on the IP address for the index of the ARP table adapter
Based on the IP address for the index of the ARP table adapter

Time:11-18



Based on the IP address for the index of the ARP table adapter?
Using c # language
As shown in figure, through the arp - a command can I get the arp table
I hope I can through 192.168.1.122 then get a return value of 0 x6 function or 0 x7 is obtained by 192.168.1.3 return value,

CodePudding user response:

This code can be
Interface IP192.168.1.3=& gt; Check the corresponding interface serial number, the interface under all ARP records;
The interface number 0 x7=& gt; Check the corresponding interface IP, ARP all records under this interface;
The IP address of the ARP record 192.168.1.122=& gt; ARP records of MAC address, and all the other contains the IP interface corresponding IP, serial number;
Record the MAC address of the 50-8 c ARP to f5 - f6-31-34=& gt; The IP address of the ARP records, and all the other contains the IP interface corresponding IP, serial number,

 using System; 
using System.Collections.Generic;
Using System. Diagnostics;
Using System. Linq;
Using System. Text. RegularExpressions;

The namespace ConsoleApp1
{
Class Program
{
The static void Main (string [] args)
{
//call the arp. Exe - a, and through the redirect standard output to obtain the result of the command
Process the proc=Process. Start (
New ProcessStartInfo ()
{
FileName="arp. Exe",
The Arguments="-a",
UseShellExecute=false,
RedirectStandardOutput=true
});
Proc. Start ();
The string result=proc. StandardOutput. ReadToEnd ();
Proc. WaitForExit ();

//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the result of parsing command, according to the interface of cutting and individual treatment
Var sections=result. The Split (' pick up ');

//IP interface and the interface ID number as the index, arp records (is)
//results contain arp records of IP and MAC
//the Value of the dictionary data type is ValueTuple type, c # 7 or higher version can support
//if your version is not enough, you can change the type of the Value to the custom structure or class, the same below
Dictionary DicIf=
New Dictionary (a);

//in the arp to record the IP or MAC for the index (check), check the interface
//results contain the interface IP and index
Dictionary DicIP=
New Dictionary (a);
The foreach (var section in sections)
{
if (! Section. The Contains (" mouth ")) continue;//the split of the first member is a "\ r \ n", skip

//interface IP, as well as the interface ID
Var match=
The Regex. Match (
Section, @ ". *? (\ d {1, 3} \ \ d {1, 3} \ \ d {1, 3} \ \ d {1, 3}). *? (0 x [0-9 a - f] +) ");
String ifIP=match. Groups [1]. The Value;
String ifIndex=match. Groups. [2] the Value;
DicIf [ifIP]=new List<(string IfIP, string Index, string ArpIp, string MAC) & gt; (a);
DicIf [ifIndex]=new List<(string IfIP, string Index, string ArpIp, string MAC) & gt; (a);

//with regular match the interface under all the arp record
//Group [1] is IP, Group [2] is MAC
Var matches=
The Regex Matches (
Section,
@ "(\ d {1, 3} \ \ d {1, 3} \ \ d {1, 3} \ \ d {1, 3}) \ s + ([0-9 a - f -] {17}) \ s + \ s + \ s +",
RegexOptions. Singleline | RegexOptions. IgnoreCase);
Foreach (Match m in matches)
{
String IP=m.G roups [1]. The Value;

//IP in the first period of more than 223, the IP multicast address or the broadcast address, don't need to deal with
String ipPrefix=IP. The Split (') [0];
If (the Convert. ToInt32 (ipPrefix) & gt; 223) continue;

String MAC=m.G roups [2]. The Value;
//add a forward lookup record
DicIf [ifIP]. Add ((ifIP ifIndex, IP, MAC));
DicIf [ifIndex]. Add ((ifIP ifIndex, IP, MAC));
//add a reverse lookup record, arp entries in the IP and MAC add a
The foreach (var key in new string [] {} IP and MAC)
{
If (dicIP. Either ContainsKey (key))
{
DicIP [key] the Add ((ifIP ifIndex, IP, MAC));
}
The else
{
DicIP [key]=
New List<(string IfIP, string Index, string ArpIp, string MAC) & gt; () {(ifIP ifIndex, IP, MAC)};
}
}
}
}
Console. WriteLine (" forward lookup dictionary (interface IP/serial number=& gt; Arp record) : ");
Foreach (var key in dicIf. Keys)
{
Console. WriteLine (
$" KEY: {KEY} \ r \ nVALUES: [+ "
"${string. Join (" \ r \ n", dicIf [key]. Select (r=& gt; $" interface IP: {r.I fIP}, interface serial number: {r.I ndex}, ARP, IP: {state Richard armitage rpIp}, ARP - MAC: {r.M AC} ")}] \ r \ n ");
}

Console. WriteLine (" reverse lookup dictionary: (arp record the IP/MAC=& gt; Interface IP/serial number) ");
Foreach (var key in dicIP. Keys)
{
Console. WriteLine (
$" KEY: {KEY} \ r \ nVALUES: [+ "
"${string. Join (" \ r \ n", dicIP [key]. Select (r=& gt; $" interface IP: {r.I fIP}, interface serial number: {r.I ndex}, ARP, IP: {state Richard armitage rpIp}, ARP - MAC: {r.M AC} ")}] \ r \ n ");
}
The Console. The Read ();
}
}
}


The results
 forward lookup dictionary (interface IP/serial number=& gt; Arp record) : 
KEY: 192.168.220.1
VALUES: [interface IP: 192.168.220.1, interface serial number: 0 x7, ARP, IP: 192.168.220.254, ARP - MAC: 00-50-56 - e6 - ca - af
Interface IP: 192.168.220.1, interface serial number: 0 x7, ARP, IP: 192.168.220.255, ARP - MAC: ff ff ff ff ff - - - - ff]

KEY: 0 x7
VALUES: [interface IP: 192.168.220.1, interface serial number: 0 x7, ARP, IP: 192.168.220.254, ARP - MAC: 00-50-56 - e6 - ca - af
Interface IP: 192.168.220.1, interface serial number: 0 x7, ARP, IP: 192.168.220.255, ARP - MAC: ff ff ff ff ff - - - - ff]

KEY: 192.168.2.51
VALUES: [interface IP: 192.168.2.51, interface serial number: 0 xb, ARP, IP: 192.168.2.1, ARP - MAC: 00-15-5 d - 02-32-03
Interface IP: 192.168.2.51, interface serial number: 0 xb, ARP, IP: 192.168.2.50, ARP - MAC: 58-00-10-18-3 - a - 8 b
Interface IP: 192.168.2.51, interface serial number: 0 xb, ARP, IP: 192.168.2.52, ARP - MAC: 00-50-56-3 - a - ee - 69
Interface IP: 192.168.2.51, interface serial number: 0 xb, ARP, IP: 192.168.2.68, ARP - MAC: 4 c - 0 b - be - 04-22-3 e
Interface IP: 192.168.2.51, interface serial number: 0 xb, ARP, IP: 192.168.2.255, ARP - MAC: ff ff ff ff ff - - - - ff]

KEY: 0 xb
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related