给我推荐5种用js获取ip和mac地址的方法并且写出来
- 使用HTTP请求头获取IP地址:
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
- 使用第三方API获取IP地址:
fetch('https://api.ipify.org?format=json')
.then(response => response.json())
.then(data => console.log(data.ip));
- 使用WebRTC获取局域网IP地址:
const pc = new RTCPeerConnection();
pc.createDataChannel('');
pc.createOffer().then(offer => pc.setLocalDescription(offer));
pc.onicecandidate = event => {
if (event.candidate) {
const ip = event.candidate.address;
console.log(ip);
}
}
- 使用Node.js的os模块获取MAC地址:
const os = require('os');
const networkInterfaces = os.networkInterfaces();
const macAddress = networkInterfaces['eth0'][0].mac;
console.log(macAddress);
- 使用第三方库获取MAC地址:
const macaddress = require('macaddress');
macaddress.one((err, mac) => {
console.log(mac);
});
原文地址: https://www.cveoy.top/t/topic/bjpf 著作权归作者所有。请勿转载和采集!