記錄用H5的webrtc搜索ssh端口找到樹莓派ip地址
最近學(xué)用樹莓派,要連接ssh,發(fā)現(xiàn)找樹莓派的IP好麻煩,想有一種比較方便不用下軟件,還跨平臺找地址的方法,自己以前也學(xué)過點WEBRTC,了解到可以對IP段掃描,所以上網(wǎng)查了一下,找到了這個文章,《在Chrome中使用WebRTC ICE服務(wù)進行端口掃描》:https://zhuanlan.zhihu.com/p/98971461
上面有實例可以直接使用
我直接抄了說明內(nèi)的代碼修改方便理解
使用:新建一個文本把以下代碼粘貼進去,保存修改為.html的后綴名然后用chrome瀏覽器運行即可。
也可以打開一個web在線編輯器:http://app.ruizhen.net/WebEditor/ ,粘貼以下代碼:
<html>
? <body>
? <div id="hosts"></div>
<script>
var brute_array = [];
for (i = 0; i < 256; i++) {
? brute_address = "turn:192.168.1." + i + ":22?transport=tcp";
? brute_array.push({
??? urls: brute_address,
??? credential: "lobster",
??? username: "albino"
? });
}
var rtc_brute = new RTCPeerConnection({
? iceServers: brute_array,
? iceCandidatePoolSize: 0
});
rtc_brute.createDataChannel('', {
? reliable: false
});
?
rtc_brute.onicecandidateerror = function(e) {
? if (e.url == null) {
??? return;
? }
? //console.log(e.port)
? url_split = e.url.split(":");
? host_div = document.createElement('div');
? host_div.id = url_split[1];
? if(e.port){var result = "open";}else{var result = "close";}
? host_div.innerHTML = url_split[1] + ": 22 port is? " + result;
? document.getElementById('hosts').appendChild(host_div);
}
?// trigger the gathering of ICE candidates
rtc_brute.createOffer(function(offerDesc) {
? rtc_brute.setLocalDescription(offerDesc);
}, function(e) {
? console.log("Create offer failed callback.");
});
?</script>
???? </body>
</html>

這個代碼只能在Chrome上才能生效
Firefox會產(chǎn)生以下錯誤:

過程視頻在我的個人空間了可以找到。