使用Java获取本地计算机的MAC地址
我可以用来
ip = InetAddress.getLocalHost();
NetworkInterface.getByInetAddress(ip);
获取mac地址,但是如果我在离线机器上使用这个代码,它就不起作用了。
那么,如何获取 Mac 地址?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
另一种方法是通过本机代码执行使用操作系统命令“getmac”。
Another way is to use an OS command 'getmac' through native code execution.
设备的 MAC 地址是分配给设备的唯一标识符网络接口控制器 (NIC)。对于网段内的通信,它用作大多数 IEEE 802 网络技术(包括以太网、Wi-Fi 和蓝牙)的网络地址。在开放系统互连 (OSI) 模型中,MAC 地址用于介质访问控制协议数据链路层的子层。
MAC 地址通常由网络接口卡制造商分配。每个都存储在硬件中,例如卡的只读存储器或通过固件机制。
使用以下函数
getPhysicalAddress()
获取 MAC 地址列表:上述函数的工作原理为
ipconfig
/all|查找“物理地址”>>MV-mac.txt.
IEEE 划分了 OSI 数据链路层 分为两个独立的子层:
返回媒体访问控制(MAC ) 地址以及与每台计算机中所有网卡的每个地址关联的网络协议列表(本地或跨网络)。
MAC address of a device is a unique identifier assigned to a network interface controller (NIC). For communications within a network segment, it is used as a network address for most IEEE 802 network technologies, including Ethernet, Wi-Fi, and Bluetooth. Within the Open Systems Interconnection (OSI) model, MAC addresses are used in the medium access control protocol sublayer of the data link layer.
MAC addresses are most often assigned by the manufacturer of network interface cards. Each is stored in hardware, such as the card's read-only memory or by a firmware mechanism.
Use the following function
getPhysicalAddress()
to get list of MAC address:The above function works as
ipconfig
/all|find "Physical Address" >>MV-mac.txt
.The IEEE divides the OSI data link layer into two separate sublayers:
Returns the media access control (MAC) address and list of network protocols associated with each address for all network cards in each computer, either locally or across a network.
科特林:
Kotlin:
输出:
output:
对于 Java 6+,您可以使用 <代码>NetworkInterface.getHardwareAddress。
请记住,计算机可以没有网卡,尤其是嵌入式或虚拟计算机。它也可以有多个。您可以使用
NetworkInterface.getNetworkInterfaces()
。With Java 6+, you can use
NetworkInterface.getHardwareAddress
.Bear in mind that a computer can have no network cards, especially if it's embedded or virtual. It can also have more than one. You can get a list of all network cards with
NetworkInterface.getNetworkInterfaces()
.有了我在这里找到的所有可能的解决方案和其他回复,然后我将贡献我的解决方案。您需要使用包含“ip”或“mac”的字符串指定参数,具体取决于您需要检查的内容。如果计算机没有接口,那么它将返回一个包含 null 的字符串,否则将返回一个包含您所要求的内容(ip 地址或 mac)的字符串。
如何使用它:
结果(如果计算机有网卡):
结果(如果计算机没有网卡):
这是代码:
更新于 02/05/2017: 感谢 @mateuscb 在帖子 如何用 Java 确定 Internet 网络接口不幸的是,之前该帖子没有得到任何赞成票,但他对此更新做出了贡献。
改进方法可以跳过虚拟机网卡(最流行的VM软件)
With all the possible solutions that i've found here and another replies, then i will contribute with my solution. You need to specify a parameter with a String containing "ip" or "mac" depending on what you need to check. If the computer has no interface, then it will return an String containing null, otherwise will return a String containing what you asked for (the ip address or the mac).
How to use it:
Result (if the computer has a network card):
Result (if the computer doesn't have a network card):
Here's the code:
UPDATED 02/05/2017: Thanks to @mateuscb on the post How to Determine Internet Network Interface in Java that unfortunately didn't get any upvote on that post before, but he contributed to this update.
The method has been improved to skip virtual machine network cards (most popular VM software)
至于离线的计算机,通常没有分配IP,因为DHCP被广泛使用......
而对于标题中的问题:
NetworkInterface.getHardwareAddress( )
As for the computer being offline, it usually doesn't have an IP assigned, because DHCP is widely used...
And for the question in the title:
NetworkInterface.getHardwareAddress()
试试这个:
Try this:
从此处清理代码:
Cleaned up code from here: