static List <ArpEntry> ScanTarget(IPAddress ip, ScanOptions options) { List <ArpEntry> results = new List <ArpEntry>(); OctetString community = new OctetString(options.Community); List <Variable> ARPTypeResult = new List <Variable>(); List <Variable> ARPIPResult = new List <Variable>(); List <Variable> ARPPhysAddrResult = new List <Variable>(); try { Messenger.BulkWalk(VersionCode.V2, new IPEndPoint(ip, 161), new OctetString(options.Community), new ObjectIdentifier("1.3.6.1.2.1.4.22.1.4"), ARPTypeResult, options.SNMPTimeout, 10, WalkMode.WithinSubtree, null, null); Messenger.BulkWalk(VersionCode.V2, new IPEndPoint(ip, 161), new OctetString(options.Community), new ObjectIdentifier("1.3.6.1.2.1.4.22.1.2"), ARPPhysAddrResult, options.SNMPTimeout, 10, WalkMode.WithinSubtree, null, null); Messenger.BulkWalk(VersionCode.V2, new IPEndPoint(ip, 161), new OctetString(options.Community), new ObjectIdentifier("1.3.6.1.2.1.4.22.1.3"), ARPIPResult, options.SNMPTimeout, 10, WalkMode.WithinSubtree, null, null); } catch (Lextm.SharpSnmpLib.Messaging.TimeoutException te) { Console.WriteLine("Request to {0} timed out, skipping... : {1}", ip, te); return(results); } foreach (Variable v in ARPTypeResult) { if (v.Data.Equals(new Integer32(4)) && options.ProcessStaticARPEntries == false) { continue; } else if (v.Data.Equals(new Integer32(3)) && options.ProcessDynamicARPEntries == false) { continue; } else if (v.Data.Equals(new Integer32(2)) && options.ProcessInvalidARPEntries == false) { continue; } else if (v.Data.Equals(new Integer32(1)) && options.ProcessOtherARPEntries == false) { continue; } uint[] numID = v.Id.ToNumerical().ToArray(); numID[9] = 3; ObjectIdentifier IPID = new ObjectIdentifier(numID); ISnmpData IPData = ARPIPResult.Where(i => i.Id == IPID).Select(x => x).Single().Data; numID[9] = 2; ObjectIdentifier PhysAddrID = new ObjectIdentifier(numID); ISnmpData PhysAddrData = ARPPhysAddrResult.Where(i => i.Id == PhysAddrID).Select(x => x).Single().Data; System.Net.NetworkInformation.PhysicalAddress mac = new System.Net.NetworkInformation.PhysicalAddress(PhysAddrData.ToBytes().Skip(2).ToArray()); ArpEntry entry = new ArpEntry(IPAddress.Parse(IPData.ToString()), mac); results.Add(entry); } return(results); }
static List<ArpEntry> ScanTarget(IPAddress ip, ScanOptions options) { List<ArpEntry> results = new List<ArpEntry>(); OctetString community = new OctetString(options.Community); List<Variable> ARPTypeResult = new List<Variable>(); List<Variable> ARPIPResult = new List<Variable>(); List<Variable> ARPPhysAddrResult = new List<Variable>(); try { Messenger.BulkWalk(VersionCode.V2, new IPEndPoint(ip, 161), new OctetString(options.Community), new ObjectIdentifier("1.3.6.1.2.1.4.22.1.4"), ARPTypeResult, options.SNMPTimeout, 10, WalkMode.WithinSubtree, null, null); Messenger.BulkWalk(VersionCode.V2, new IPEndPoint(ip, 161), new OctetString(options.Community), new ObjectIdentifier("1.3.6.1.2.1.4.22.1.2"), ARPPhysAddrResult, options.SNMPTimeout, 10, WalkMode.WithinSubtree, null, null); Messenger.BulkWalk(VersionCode.V2, new IPEndPoint(ip, 161), new OctetString(options.Community), new ObjectIdentifier("1.3.6.1.2.1.4.22.1.3"), ARPIPResult, options.SNMPTimeout, 10, WalkMode.WithinSubtree, null, null); } catch(Lextm.SharpSnmpLib.Messaging.TimeoutException te) { Console.WriteLine("Request to {0} timed out, skipping... : {1}", ip, te); return results; } foreach (Variable v in ARPTypeResult) { if (v.Data.Equals(new Integer32(4)) && options.ProcessStaticARPEntries == false) continue; else if (v.Data.Equals(new Integer32(3)) && options.ProcessDynamicARPEntries == false) continue; else if (v.Data.Equals(new Integer32(2)) && options.ProcessInvalidARPEntries == false) continue; else if (v.Data.Equals(new Integer32(1)) && options.ProcessOtherARPEntries == false) continue; uint[] numID = v.Id.ToNumerical().ToArray(); numID[9] = 3; ObjectIdentifier IPID = new ObjectIdentifier(numID); ISnmpData IPData = ARPIPResult.Where(i => i.Id == IPID).Select(x => x).Single().Data; numID[9] = 2; ObjectIdentifier PhysAddrID = new ObjectIdentifier(numID); ISnmpData PhysAddrData = ARPPhysAddrResult.Where(i => i.Id == PhysAddrID).Select(x => x).Single().Data; System.Net.NetworkInformation.PhysicalAddress mac = new System.Net.NetworkInformation.PhysicalAddress(PhysAddrData.ToBytes().Skip(2).ToArray()); ArpEntry entry = new ArpEntry(IPAddress.Parse(IPData.ToString()), mac); results.Add(entry); } return results; }