java取主机的网卡物理地址
生活随笔
收集整理的這篇文章主要介紹了
java取主机的网卡物理地址
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.用jdk的java.net.NetworkInterface接口
package com.tc.temp;import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration;public class Network {public static void main(String[] args) throws Exception {Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();while(networkInterfaces.hasMoreElements()) {NetworkInterface interface1 = networkInterfaces.nextElement();if (interface1.getParent() == null && interface1.getInetAddresses() != null && interface1.getHardwareAddress() != null){Enumeration<InetAddress> inetAddrs = interface1.getInetAddresses();System.out.print("網卡名稱:");System.out.println(interface1.getName() + " " + interface1.getDisplayName());System.out.print("MAC:");byte[] btMac = interface1.getHardwareAddress();for (int i = 0; i < btMac.length; i++) {System.out.print(Integer.toHexString(btMac[i] & 0xff).toUpperCase());if(i < btMac.length - 1) System.out.print(":");}System.out.println();System.out.println("網卡IP:");while (inetAddrs.hasMoreElements())System.out.println(inetAddrs.nextElement());System.out.println();System.out.println();}}} }2.用jni
window
#include "stdafx.h" #include "com_cndatacom_cdc_SystemLicense.h" #include "jni.h" #include "windows.h" #include "dos.h" #include <wbemidl.h> #pragma comment(lib,"wbemuuid.lib") #include <tchar.h>struct LinkedNode{unsigned short *val;size_t len;LinkedNode *next; };size_t wslength(wchar_t *ws) {size_t i = 0;while (*(ws + i) != '\0')i++;return i; }void wscat(wchar_t *ws1, wchar_t *ws2, unsigned short *ls) {wchar_t wc = '\0';size_t i = 0;while ((wc = *(ws1 + i)) != '\0') {*(ls + i) = wc;i++;}size_t j = 0;while ((wc = *(ws2 + j)) != '\0') {*(ls + i + j) = wc;j++;} }JNIEXPORT jobjectArray JNICALL Java_com_cndatacom_cdc_SystemLicense_getMachineInfo(JNIEnv * jniEnv_, jclass jc) {IWbemLocator *locator = NULL;IWbemServices *services = NULL;IEnumWbemClassObject *pEnumerator = NULL;BSTR resource = SysAllocString(L"ROOT\\CIMV2");BSTR language = SysAllocString(L"WQL");//Caption, Description, MACAddress, ServiceName, DNSHostNameBSTR query =SysAllocString(L"SELECT MACAddress FROM Win32_NetworkAdapterConfiguration where IPEnabled = true");HRESULT hr = 0;hr = CoInitializeEx(0, COINIT_MULTITHREADED);hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &locator);hr = locator->ConnectServer(resource, NULL, NULL, 0, NULL, 0, 0, &services);hr = services->ExecQuery(language, query, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);struct LinkedNode *first = NULL;struct LinkedNode *point = NULL;size_t allleng = 0;if (pEnumerator != NULL) {IWbemClassObject *result = NULL;ULONG uCount = 1, uReturned;//printf("獲取IEnumWbemClassObject成功!\n");int iEnumIdx = 0;const wchar_t *str = NULL;while ((hr = pEnumerator->Next(WBEM_INFINITE, uCount, &result, &uReturned)) == S_OK) {SAFEARRAY *pvNames = NULL;if (result->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK) {//printf("獲取名稱成功!\n");long vbl, vbu;SafeArrayGetLBound(pvNames, 1, &vbl);SafeArrayGetUBound(pvNames, 1, &vbu);for (long idx = vbl; idx <= vbu; idx++) {//printf("遍歷名稱列表!\n");long aidx = idx;wchar_t *wsName = 0;VARIANT vValue;VariantInit(&vValue);SafeArrayGetElement(pvNames, &aidx, &wsName);BSTR bs = SysAllocString(wsName);HRESULT hRes = result->Get(bs, 0, &vValue, NULL, 0);SysFreeString(bs);if (hRes == S_OK) {if (vValue.vt == VT_BSTR) {//int len = wslength(wsName) + wslength(vValue.bstrVal);int len = wslength(vValue.bstrVal);unsigned short *ls = (unsigned short *) calloc(len, 16);wchar_t *t = L"";wscat(t, vValue.bstrVal, ls);struct LinkedNode *nw;nw = (LinkedNode*)calloc(1, sizeof(LinkedNode));nw->len = len;nw->next = NULL;nw->val = ls;if (point != NULL)point->next = nw;point = nw;if (first == NULL)first = point;allleng ++;}}VariantClear(&vValue);SysFreeString(wsName);}}result->Release();if (pvNames)SafeArrayDestroy(pvNames);iEnumIdx++;}}pEnumerator->Release();locator->Release();services->Release();jobjectArray _array = jniEnv_->NewObjectArray(allleng, jniEnv_->FindClass("[C"), NULL);point = first;int x = 0;while(true) {jcharArray _longArray = jniEnv_->NewCharArray(point->len);jniEnv_->SetCharArrayRegion(_longArray, 0, point->len, point->val);jniEnv_->SetObjectArrayElement(_array, x, _longArray);x ++;LinkedNode *tmp = point;point = point->next;free(tmp);if (x >= allleng)break;}return _array; }linux
/** com_cndatacom_cdc_SystemLicense.cpp** Created on: 2012-11-30* Author: root*/ #include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <net/if.h> #include <malloc.h> #include "com_cndatacom_cdc_SystemLicense.h"const char hexChar[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };void char2Hex(char i, char str[]) {*str = hexChar[(i & 255) >> 16];*(str + 1) = hexChar[i & 15]; }JNIEXPORT jobjectArray JNICALL Java_com_cndatacom_cdc_SystemLicense_getMachineInfo(JNIEnv * _jniEnv, jclass _jclzz) {int sockfd, i;struct ifreq buffer;sockfd = socket(PF_INET, SOCK_DGRAM, 0);if (sockfd <= 0) {fputs("Can not create socket!\n", stderr);return NULL;}memset(&buffer, 0, sizeof(struct ifreq));strcpy(buffer.ifr_name, "eth0");ioctl(sockfd, SIOCGIFHWADDR, &buffer);close(sockfd);jchar mac[17];for (i = 0; i < 6; i++) {char item[2];char2Hex(buffer.ifr_hwaddr.sa_data[i] & 0xff, item);mac[i * 3] = item[0];mac[i * 3 + 1] = item[1];if(i != 5) mac[i * 3 + 2] = ':';}jobjectArray _array = _jniEnv->NewObjectArray(1, _jniEnv->FindClass("[C"), NULL);jcharArray _longArray = _jniEnv->NewCharArray(17);_jniEnv->SetCharArrayRegion(_longArray, 0, 17, mac);_jniEnv->SetObjectArrayElement(_array, 0, _longArray);return _array; }轉載于:https://www.cnblogs.com/hujz/archive/2013/03/14/2959458.html
總結
以上是生活随笔為你收集整理的java取主机的网卡物理地址的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用前端框架Foundation 4来帮
- 下一篇: 支付宝网商贷和借呗有什么区别