使用ARP获取局域网内设备IP和MAC地址
根据Arp列表数据,查询本地设备在线状态
使用 arp -a 获得所有内网地址,首先看Mod对象
public struct MacIpPair
{
public string HostName;
public string MacAddress;
public string IpAddress; public override string ToString()
{
string str = "";
str += $"HostName:{HostName}\t{IpAddress}\t{MacAddress}";
return str;
} }
其次看看查询方法:
public List<MacIpPair> GetAllMacAddressesAndIppairs()
{
List<MacIpPair> mip = new List<MacIpPair>();
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = "arp";
pProcess.StartInfo.Arguments = "-a ";
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
string cmdOutput = pProcess.StandardOutput.ReadToEnd();
string pattern = @"(?<ip>([0-9]{1,3}\.?){4})\s*(?<mac>([a-f0-9]{2}-?){6})"; foreach (Match m in Regex.Matches(cmdOutput, pattern, RegexOptions.IgnoreCase))
{
mip.Add(new MacIpPair()
{
MacAddress = m.Groups["mac"].Value,
IpAddress = m.Groups["ip"].Value
});
} return mip;
}
在写个调用就可以了:
class Program
{
static void Main(string[] args)
{
var arp = new Comm.ArpHelper();
var i = arp.GetLocalIpInfo();
Console.WriteLine(i.ToString()); var l = arp.GetAllMacAddressesAndIppairs();
l.ForEach(x =>
{
//Console.WriteLine($"IP:{x.IpAddress} Mac:{x.MacAddress}");
Console.WriteLine(x.ToString());
}); Console.WriteLine("\r\n==================================================\r\n"); Console.WriteLine("本地网卡信息:");
Console.WriteLine(arp.GetLocalIpInfo() + " == " + arp.getLocalMac()); string ip = "192.168.68.42";
Console.Write("\n\r远程 " + ip + " 主机名信息:");
var hName = arp.GetRemoteHostName(ip);
Console.WriteLine(hName); Console.WriteLine("\n\r远程主机 " + hName + " 网卡信息:");
string[] temp = arp.getRemoteIP(hName);
for (int j = ; j < temp.Length; j++)
{
Console.WriteLine("远程IP信息:" + temp[j]);
} Console.WriteLine("\n\r远程主机MAC :");
Console.WriteLine(arp.getRemoteMac("192.168.68.21", "192.168.68.255"));
Console.WriteLine(arp.getRemoteMac("192.168.68.21", "192.168.68.44")); Console.ReadKey();
}
}
出处:https://segmentfault.com/q/1010000008600333/a-1020000011467457
=====================================================================
c# 通过发送arp包获取ip等信息
利用dns类和WMI规范获取IP及MAC地址
在C#编程中,要获取主机名和主机IP地址,是比较容易的.它提供的Dns类,可以轻松的取得主机名和IP地址.
示例:
string strHostName = Dns.GetHostName(); //得到本机的主机名
IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本机IP
string strAddr = ipEntry.AddressList[0].ToString(); //假设本地主机为单网卡
在这段代码中使用了两个类,一个是Dns类,另一个为IPHostEntry类,二者都存在于命名空间System.Net中.
Dns类主要是从域名系统(DNS)中检索关于特定主机的信息,上面的代码第一行就从本地的DNS中检索出本地主机名.
IPHostEntry类则将一个域名系统或主机名与一组IP地址相关联,它与DNS类一起使用,用于获取主机的IP地址组.
要获取远程主机的IP地址,其方法也是大同小异.
在获取了IP地址后,如果还需要取得网卡的MAC地址,就需要进一步探究了.
这里又分两种情况,一是本机MAC地址,二是远程主机MAC地址.二者的获取是完全不同的.
在获取本机的MAC地址时,可以使用WMI规范,通过SELECT语句提取MAC地址.在.NET框架中,WMI规范的实现定义在System.Management命名空间中.
ManagementObjectSearcher类用于根据指定的查询检索管理对象的集合
ManagementObjectCollection类为管理对象的集合,下例中由检索对象返回管理对象集合赋值给它.
示例:
ManagementObjectSearcher query =new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration") ;
ManagementObjectCollection queryCollection = query.Get();
foreach( ManagementObject mo in queryCollection )
{
if(mo["IPEnabled"].ToString() == "True")
mac = mo["MacAddress"].ToString();
}
获取远程主机的MAC地址时,需要借用API函数SendARP.该函数使用ARP协议,向目的主机发送ARP包,利用返回并存储在高速缓存中的IP和MAC地址对,从而获取远程主机的MAC地址.
示例:
Int32 ldest= inet_addr(remoteIP); //目的ip
Int32 lhost= inet_addr(localIP); //本地ip
try
{
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest,0, ref macinfo, ref len); //发送ARP包
return Convert.ToString(macinfo,16);
}
catch(Exception err)
{
Console.WriteLine("Error:{0}",err.Message);
}
return 0.ToString();
但使用该方式获取MAC时有一个很大的限制,就是只能获取同网段的远程主机MAC地址.因为在标准网络协议下,ARP包是不能跨网段传输的,故想通过ARP协议是无法查询跨网段设备MAC地址的。
示例程序全部代码:
using System.Net;
using System;
using System.Management;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions; public class ArpHelper
{ [DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip); //使用arp -a命令获取ip和mac地址
public List<MacIpPair> GetAllMacAddressesAndIppairs()
{
List<MacIpPair> mip = new List<MacIpPair>();
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = "arp";
pProcess.StartInfo.Arguments = "-a ";
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
string cmdOutput = pProcess.StandardOutput.ReadToEnd();
string pattern = @"(?<ip>([0-9]{1,3}\.?){4})\s*(?<mac>([a-f0-9]{2}-?){6})"; foreach (Match m in Regex.Matches(cmdOutput, pattern, RegexOptions.IgnoreCase))
{
mip.Add(new MacIpPair()
{
MacAddress = m.Groups["mac"].Value,
IpAddress = m.Groups["ip"].Value
});
} return mip;
} //获取本机的IP
public MacIpPair GetLocalIpInfo()
{
string strHostName = Dns.GetHostName(); //得到本机的主机名
IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本机IP
string strAddr = ipEntry.AddressList[].ToString(); //假设本地主机为单网卡 string mac = "";
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
if (mo["IPEnabled"].ToString() == "True")
mac = mo["MacAddress"].ToString();
} return new MacIpPair { HostName = strHostName, IpAddress = strAddr, MacAddress = mac };
} //获取本机的MAC
public string getLocalMac()
{
string mac = null;
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
if (mo["IPEnabled"].ToString() == "True")
mac = mo["MacAddress"].ToString();
}
return (mac);
} //根据主机名获取远程主机IP
public string[] getRemoteIP(string RemoteHostName)
{
IPHostEntry ipEntry = Dns.GetHostByName(RemoteHostName);
IPAddress[] IpAddr = ipEntry.AddressList;
string[] strAddr = new string[IpAddr.Length];
for (int i = ; i < IpAddr.Length; i++)
{
strAddr[i] = IpAddr[i].ToString();
}
return strAddr;
} //根据ip获取远程主机名
public string GetRemoteHostName(string ip)
{
var d = Dns.GetHostEntry(ip);
return d.HostName;
} //获取远程主机MAC
public string getRemoteMac(string localIP, string remoteIP)
{
string res = "FFFFFFFFFFFF";
Int32 rdest = inet_addr(remoteIP); //目的ip
Int32 lhost = inet_addr(localIP); //本地ip try
{
//throw new Exception();
Int64 macinfo = new Int64();
Int32 len = ;
int sendRes = SendARP(rdest, lhost, ref macinfo, ref len);
var mac = Convert.ToString(macinfo, ); return formatMacAddres(mac);
}
catch (Exception err)
{
Console.WriteLine("Error:{0}", err.Message);
}
return formatMacAddres(res);
} private string formatMacAddres(string macAdd)
{
StringBuilder sb = new StringBuilder();
macAdd = macAdd.Length == ? "" + macAdd : macAdd;
if (macAdd.Length == )
{
int i = ;
while (i < macAdd.Length)
{
string tmp = macAdd.Substring(i, ) + "-";
sb.Insert(, tmp);
//sb.Append(tmp, 0, tmp.Length);
i += ;
}
}
else
{
sb = new StringBuilder("");
}
return sb.ToString().Trim('-').ToUpper(); } }
出处:https://www.cnblogs.com/purplenight/articles/2688241.html
============================================================
C#通过SendARP()获取WinCE设备的Mac网卡物理地址
ARP(Address Resolution Protocol) 即 地址解析协议,是根据IP地址获取物理地址的一个TCP/IP协议。
SendARP(Int32 dest, Int32 host, out Int64 mac, out Int32 length)
①dest:访问的目标IP地址,既然获取本机网卡地址,写本机IP即可 这个地址比较特殊,必须从十进制点分地址转换成32位有符号整数 在C#中为Int32;
②host:源IP地址,即时发送者的IP地址,这里可以随便填写,填写Int32整数即可;
③mac:返回的目标MAC地址(十进制),我们将其转换成16进制后即是想要的结果用out参数加以接收;
④length:返回的是pMacAddr目标MAC地址(十进制)的长度,用out参数加以接收。
如果使用的是C++或者C语言可以直接调用 inet_addr("192.168.0.×××")得到 参数dest 是关键
现在用C#来获取,首先需要导入"ws2_32.dll"这个库,这个库中存在inet_addr(string cp)这个方法,之后我们就可以调用它了。
|
1
2
3
4
5
|
//首先,要引入命名空间:using System.Runtime.InteropServices;1 using System.Runtime.InteropServices;//接下来导入C:\Windows\System32下的"ws2_32.dll"动态链接库,先去文件夹中搜索一下,文件夹中没有Iphlpapi.dll的在下面下载2 [DllImport("ws2_32.dll")]3 private static extern int inet_addr(string ip);//声明方法 |
Iphlpapi.dll的点击 这里 下载
|
1
|
|
|
1
2
3
4
5
6
7
8
9
10
|
//第二 调用方法Int32 desc = inet_addr("192.168.0.××");/*由于个别WinCE设备是不支持"ws2_32.dll"动态库的,所以我们需要自己实现inet_addr()方法输入是点分的IP地址格式(如A.B.C.D)的字符串,从该字符串中提取出每一部分,为int,假设得到4个int型的A,B,C,D,,IP = D<<24 + C<<16 + B<<8 + A(网络字节序),即inet_addr(string ip)的返回结果,我们也可以把该IP转换为主机字节序的结果,转换方法一样 A<<24 + B<<16 + C<<8 + D*/ |
接下来是完整代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
using System;using System.Runtime.InteropServices;using System.Net;using System.Diagnostics;using System.Net.Sockets;public class MacAddressDevice { [DllImport("Iphlpapi.dll")] private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length); //获取本机的IP public static byte[] GetLocalIP() { //得到本机的主机名 string strHostName = Dns.GetHostName(); try { //取得本机所有IP(IPV4 IPV6 ...) IPAddress[] ipAddress = Dns.GetHostEntry(strHostName).AddressList; byte[] host = null; foreach (var ip in ipAddress) { while (ip.GetAddressBytes().Length == 4) { host = ip.GetAddressBytes(); break; } if (host != null) break; } return host; } catch (Exception) { return null; } } // 获取本地主机MAC地址 public static string GetLocalMac(byte[] ip) { if(ip == null) return null; int host = (int)((ip[0]) + (ip[1] << 8) + (ip[2] << 16) + (ip[3] << 24)); try { Int64 macInfo = 0; Int32 len = 0; int res = SendARP(host, 0, out macInfo, out len); return Convert.ToString(macInfo, 16); } catch (Exception err) { Console.WriteLine("Error:{0}", err.Message); } return null; } }} |
最终取得Mac地址
|
1
2
3
4
|
//本机Mac地址string Mac = GetLocalMac(GetLocalIP());//得到Mac地址是小写的,或者前后次序颠倒的,自己转换为正常的即可。 |
出处:https://www.cnblogs.com/JourneyOfFlower/p/SendARP.html
================================================================
使用ARP获取局域网内设备IP和MAC地址的更多相关文章
- 查看局域网内某个ip的mac地址
首先需要ping一下对方的ip,确保本地的arp表中缓存对方的ip和mac的关系 C:\Windows\System32>ping 192.168.1.231 正在 Ping 192.168 ...
- 获取客户机的ip和mac地址
只获取clientIP package com.ppms.utils; import javax.servlet.http.HttpServletRequest; /** * Created by l ...
- Java获取本机的IP与MAC地址
有些机器有许多虚拟的网卡,获取IP地址时会出现一些意外,所以需要一些验证: // 获取mac地址 public static String getMacAddress() { try { Enumer ...
- C#获取路由器外网IP,MAC地址
C#实现的获取路由器MAC地址,路由器外网地址.对于要获取路由器MAC地址,一定需要知道路由器web管理系统的用户名和密码.至于获取路由器的外网IP地址,可以不需要知道路由器web管理系统的用户名和密 ...
- 扫描局域网内所有主机和MAC地址的Shell脚本
#!/bin/bash #author: InBi #date: 2011-08-16 #website: http://www.itwhy.org/2011/08-20/939.html ##### ...
- JAVA获取本机IP和Mac地址
在项目中,时常需要获取本机的Ip或是Mac地址,进行身份和权限验证,本文就是通过java代码获取ip和Mac. package com.svse.query;import java.net.In ...
- 怎么查询局域网内全部电脑IP和mac地址等信息?
在局域网内查询在线主机的IP一般比较简单,但局域网内全部电脑的IP怎么才能够查到呢?查询到IP后我还要知道对方的一些详细信息(如MAC地址.电脑名称等)该怎么查询呢??? 工具/原料 Windows ...
- 怎么查询局域网内全部电脑IP和mac地址..
在局域网内查询在线主机的IP一般比较简单,但局域网内全部电脑的IP怎么才能够查到呢?查询到IP后我还要知道对方的一些详细信息(如MAC地址.电脑名称等)该怎么查询呢??? 工具/原料 Windows ...
- 查看局域网内所有IP的方法
1,windows下查看局域网内所有IP的方法: 在MS-DOS命令行输入arp -a 2,Linux下,查看局域网内所有IP的方法: 在命令行输入nmap -sP 172.10.3.0/24
随机推荐
- Linux性能优化实战学习笔记:第三十四讲
一.上节回顾 上一节,我带你学习了 Linux 网络的基础原理.简单回顾一下,Linux 网络根据 TCP/IP模型,构建其网络协议栈.TCP/IP 模型由应用层.传输层.网络层.网络接口层等四层组成 ...
- [LeetCode] 112. Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- monkey-api-encrypt 1.1.2版本发布啦
时隔10多天,monkey-api-encrypt发布了第二个版本,还是要感谢一些正在使用的朋友们,提出了一些问题. GitHub主页:https://github.com/yinjihuan/mon ...
- c# .net core + .net framework mongodb nuget 包
FastNet.Framework.Mongo https://github.com/my-core/FastNet.Framework GH.MongoDb.GenericRepository ht ...
- SpringBoot第十八篇:异步任务
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11095891.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 系统中的异 ...
- LeetCode 28:实现strStr() Implement strStr()
爱写bug(ID:icodebugs) 作者:爱写bug 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needl ...
- hbase 待看代码
flush split mvcc rpc get put mutation netty reactor模型 page cache 缓存 I/O 又被称作标准 I/O,大多数文件系统的默认 I/O 操 ...
- CAS5单点登录
看这篇文章即可:https://www.jianshu.com/p/c1273d81c4e4>https://www.jianshu.com/p/c1273d81c4e4
- WPF DataGrid 使用CellTemplateSelector 时SelectTemplate方法Item参数为NULL
首先说明 在SelectTemplate中并Item参数并不是真的一直为Null.而是先执行空参数,之后再会执行有参数的. 至于原因 我也不知道... 具体验证过程是 也就说 做好非空检测即可
- 云原生生态周报 Vol. 16 | CNCF 归档 rkt,容器运行时“上古”之战老兵凋零
作者列表:木苏,临石,得为,等等 业界要闻 安全漏洞 CVE-2019-9512 CVE-2019-9514 http2 的 DOS 漏洞,一旦攻击成功会耗尽服务器的 cpu/mem,从而导致服务不可 ...