首頁 > web前端 > js教程 > 主體

淺談利用Node.js如何取得WI-FI密碼

青灯夜游
發布: 2021-09-08 10:49:47
轉載
2821 人瀏覽過

利用Node.js如何取得WI-FI密碼?以下這篇文章跟大家介紹一下使用Node.js取得WI-FI密碼的方法,希望對大家有幫助!

淺談利用Node.js如何取得WI-FI密碼

【推薦學習:《nodejs 教學》】

示範效果

全域安裝wifi-password-cli依賴

npm install wifi-password-cli -g
# or
npx wifi-password-cli
登入後複製

使用

$ wifi-password [network-name]

$ wifi-password
12345678

$ wifi-password 办公室wifi
a1234b2345
登入後複製

覺得Node.js很神奇是麼?其實並不是,我們看看它是如何實現的

實作原理

#OSX系統

##透過下面的指令查詢wifi密碼

security find-generic-password -D "AirPort network password" -wa "wifi-name"
登入後複製

Linux系統

所有的wi-fi連線資訊都在

/etc/NetworkManager/system-connections/資料夾中

我們透過下面的指令來查詢wifi密碼

sudo cat /etc/NetworkManager/system-connections/<wifi-name>
登入後複製

Windows系統

透過下面的指令查詢wifi密碼

netsh wlan show profile name=<wifi-name> key=clear
登入後複製

實作原始碼

它的實作原始碼也很簡單,有興趣可以學習

https://github.com/kevva/wifi-password

入口檔案是

index.js,先透過判斷使用者的作業系統去選擇不同的取得方式

&#39;use strict&#39;;
const wifiName = require(&#39;wifi-name&#39;);

module.exports = ssid => {
	let fn = require(&#39;./lib/linux&#39;);

	if (process.platform === &#39;darwin&#39;) {
		fn = require(&#39;./lib/osx&#39;);
	}

	if (process.platform === &#39;win32&#39;) {
		fn = require(&#39;./lib/win&#39;);
	}

	if (ssid) {
		return fn(ssid);
	}

	return wifiName().then(fn);
};
登入後複製

Linux
&#39;use strict&#39;;
const execa = require(&#39;execa&#39;);

module.exports = ssid => {
	const cmd = &#39;sudo&#39;;
	const args = [&#39;cat&#39;, `/etc/NetworkManager/system-connections/${ssid}`];

	return execa.stdout(cmd, args).then(stdout => {
		let ret;

		ret = /^\s*(?:psk|password)=(.+)\s*$/gm.exec(stdout);
		ret = ret && ret.length ? ret[1] : null;

		if (!ret) {
			throw new Error(&#39;Could not get password&#39;);
		}

		return ret;
	});
};
登入後複製

OSX
&#39;use strict&#39;;
const execa = require(&#39;execa&#39;);

module.exports = ssid => {
	const cmd = &#39;security&#39;;
	const args = [&#39;find-generic-password&#39;, &#39;-D&#39;, &#39;AirPort network password&#39;, &#39;-wa&#39;, ssid];

	return execa(cmd, args)
		.then(res => {
			if (res.stderr) {
				throw new Error(res.stderr);
			}

			if (!res.stdout) {
				throw new Error(&#39;Could not get password&#39;);
			}

			return res.stdout;
		})
		.catch(err => {
			if (/The specified item could not be found in the keychain/.test(err.message)) {
				err.message = &#39;Your network doesn\&#39;t have a password&#39;;
			}

			throw err;
		});
};
登入後複製

Windows
&#39;use strict&#39;;
const execa = require(&#39;execa&#39;);

module.exports = ssid => {
	const cmd = &#39;netsh&#39;;
	const args = [&#39;wlan&#39;, &#39;show&#39;, &#39;profile&#39;, `name=${ssid}`, &#39;key=clear&#39;];

	return execa.stdout(cmd, args).then(stdout => {
		let ret;

		ret = /^\s*Key Content\s*: (.+)\s*$/gm.exec(stdout);
		ret = ret && ret.length ? ret[1] : null;

		if (!ret) {
			throw new Error(&#39;Could not get password&#39;);
		}

		return ret;
	});
};
登入後複製
更多程式相關知識,請造訪:

程式設計影片! !

以上是淺談利用Node.js如何取得WI-FI密碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:juejin.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!