js获取局域网所有计算机ip,网页JS如何获取局域网IP地址

这段代码展示了如何使用JavaScript通过RTCPeerConnection API获取局域网内的IP地址,包括本地IP、公共IP和IPv6地址。通过创建数据通道和offer SDP来触发STUN服务器请求,然后解析本地描述信息以获取候选人IP。
摘要由CSDN通过智能技术生成
Your local IP addresses:
Your public IP addresses:
Your IPv6 addresses:

//get the IP addresses associated with an account

function getIPs(callback){

var ip_dups = {};

//compatibility for firefox and chrome

var RTCPeerConnection = window.RTCPeerConnection

|| window.mozRTCPeerConnection

|| window.webkitRTCPeerConnection;

var useWebKit = !!window.webkitRTCPeerConnection;

//bypass naive webrtc blocking using an iframe

if(!RTCPeerConnection){

//NOTE: you need to have an iframe in the page right above the script tag

//

//

//...getIPs called in here...

//

var win = iframe.contentWindow;

RTCPeerConnection = win.RTCPeerConnection

|| win.mozRTCPeerConnection

|| win.webkitRTCPeerConnection;

useWebKit = !!win.webkitRTCPeerC

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,作为一个语言模型,我不能直接与您交互并提供实时的计算结果。但是,我可以为您提供一个JavaScript示例代码,该代码可以检测当前局域网内的IP地址并将其显示在网页上。您可以将此代码复制到您的HTML文件中并在浏览器中运行。 ``` <!DOCTYPE html> <html> <head> <title>Local Network IP Scanner</title> </head> <body> <h1>Local Network IP Scanner</h1> <p>IP addresses found on your local network:</p> <ul id="ip-list"></ul> <script> // Get the IP address of the current machine var thisIp = location.hostname; // Split the IP address into an array of parts var ipParts = thisIp.split('.'); // Construct the base IP address for the local network var baseIp = ipParts[0] + '.' + ipParts[1] + '.' + ipParts[2] + '.'; // Loop through all possible IP addresses on the local network for (var i = 1; i < 255; i++) { // Construct the current IP address to check var currentIp = baseIp + i; // Create a new AJAX request to check if the current IP address is in use var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://' + currentIp + ':80', false); // If the request succeeds, add the IP address to the list xhr.onload = function() { if (xhr.status === 200) { var listItem = document.createElement('li'); listItem.textContent = currentIp; document.getElementById('ip-list').appendChild(listItem); } }; // Send the AJAX request try { xhr.send(); } catch (e) {} } </script> </body> </html> ``` 请注意,此代码将尝试通过端口80连接到每个可能的IP地址,以确定该地址是否正在使用。由于网络配置和安全设置的差异,此代码可能无法检测到所有IP地址
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值