如何扫描IP地址范围并列出具有IP地址和MAC的所有活动PC [英] How to scan a range of IP address and list all the active PCs with IP address and MAC

查看:64
本文介绍了如何扫描IP地址范围并列出具有IP地址和MAC的所有活动PC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CodeProject中找到的文章基本上只是扫描网络并列出网络中存在的内容.
现在,我想通过添加"IP地址的开始"和"IP地址的结束"来限制扫描.

知道如何吗?还是只是一个"if"或"for"循环来限制搜索范围?

The articles i found in CodeProject is just basically scan the network and list out whatever is existed in the network.
Now i want to limit the scanning by adding "Starting of IP address", and "Ending of IP address".

Any idea how? or just a "if" or "for" loop to limit the searching range?

推荐答案

我找到了一个.
为本地实施基本IP扫描程序C#中的LAN [ ^ ]
I found one.
Implement a basic IP Scanner for a local LAN in C#[^]


查看以下代码:

See this code:

using System;
using System.Net;
using System.Linq;
using System.Collections;
using System.Runtime.InteropServices;

namespace devlist
{
	public class IPEnumeration: IEnumerable
	{
		private string startAddress;
		private string endAddress;
		
		internal static Int64 AddressToInt(IPAddress addr)
	    {
			byte[] addressBits = addr.GetAddressBytes();
			
	        Int64 retval = 0;
	        for (int i = 0; i < addressBits.Length; i++)
	        {
	            retval = (retval << 8) + (int)addressBits[i];
	        }
	
	        return retval;
	    }		
		
		internal static Int64 AddressToInt(string addr)
	    {
			return AddressToInt(IPAddress.Parse(addr));
	    }

	    internal static IPAddress IntToAddress(Int64 addr)
	    {
	    	return IPAddress.Parse(addr.ToString());
	    }	        
	    
	    
	    public IPEnumeration(string startAddress, string endAddress)
	    {
	    	this.startAddress = startAddress;
	    	this.endAddress = endAddress;
	    }
	    
	    IEnumerator IEnumerable.GetEnumerator()
	    {
	       return (IEnumerator) GetEnumerator();
	    }
	
	    public IPEnumerator GetEnumerator()
	    {
	        return new IPEnumerator(startAddress, endAddress);
	    }    
	    
	}
	
	public class IPEnumerator: IEnumerator
	{
		private string startAddress;
		private string endAddress;
		private Int64 currentIP;
		private Int64 endIP;
		
		public IPEnumerator(string startAddress, string endAddress)
		{
			this.startAddress = startAddress;
	    	this.endAddress = endAddress;	
	    	
	    	currentIP = IPEnumeration.AddressToInt(startAddress);
	    	endIP = IPEnumeration.AddressToInt(endAddress);
		}
		
		public bool MoveNext()
	    {
	        currentIP++;
	        return (currentIP <= endIP);
	    }
	
	    public void Reset()
	    {
	        currentIP = IPEnumeration.AddressToInt(startAddress);
	    }
	
	    object IEnumerator.Current
	    {
	        get
	        {
	            return Current;
	        }
	    }
	
	    public IPAddress Current
	    {
	        get
	        {
	            try
	            {
	            	return IPEnumeration.IntToAddress(currentIP);
	            }
	            catch (IndexOutOfRangeException)
	            {
	                throw new InvalidOperationException();
	            }
	        }
	    }
	}
	
    public static class IPHelper
    {
    	[DllImport("iphlpapi.dll", ExactSpelling=true)]
    	public static extern int SendARP( int DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen);
    	
    	public static string getMAC(IPAddress address)
    	{
    		int intAddress = BitConverter.ToInt32(address.GetAddressBytes(), 0);

			byte[] macAddr = new byte[6];
			uint macAddrLen = (uint) macAddr.Length;
			if (SendARP(intAddress, 0, macAddr, ref macAddrLen) != 0)
			    return "(NO ARP result)";
			
			string[] str = new string[(int)macAddrLen];
			for (int i = 0; i < macAddrLen; i++)
			    str[i] = macAddr[i].ToString("x2");
			
			return string.Join(":", str);
    	}
    }
    
	class Program
	{
		public static void Main(string[] args)
		{
			
			foreach(IPAddress addr in new IPEnumeration("172.25.216.10","172.25.216.20"))
	        {
				Console.WriteLine("{0}\t\t{1}",addr.ToString(), IPHelper.getMAC(addr));
	        }
			
			Console.ReadKey(true);
		}
	}
}


这篇关于如何扫描IP地址范围并列出具有IP地址和MAC的所有活动PC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆