这篇文章给大家分享的是有关使用Node.js怎么获取WI-FI密码的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
全局安装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很神奇是么?其实并不是,我们看看它是如何实现的
通过下面的命令查询wifi密码
security find-generic-password -D "AirPort network password" -wa "wifi-name"
所有的wi-fi连接信息都在/etc/NetworkManager/system-connections/
文件夹中
我们通过下面的命令来查询wifi密码
sudo cat /etc/NetworkManager/system-connections/<wifi-name>
通过下面的命令查询wifi密码
netsh wlan show profile name=<wifi-name> key=clear
它的实现源码也很简单,感兴趣可以学习
https://github.com/kevva/wifi-password
入口文件是index.js
,首先通过判断用户的操作系统去选择不同的获取方式
'use strict';
const wifiName = require('wifi-name');
module.exports = ssid => {
let fn = require('./lib/linux');
if (process.platform === 'darwin') {
fn = require('./lib/osx');
}
if (process.platform === 'win32') {
fn = require('./lib/win');
}
if (ssid) {
return fn(ssid);
}
return wifiName().then(fn);
};
'use strict';
const execa = require('execa');
module.exports = ssid => {
const cmd = 'sudo';
const args = ['cat', `/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('Could not get password');
}
return ret;
});
};
'use strict';
const execa = require('execa');
module.exports = ssid => {
const cmd = 'security';
const args = ['find-generic-password', '-D', 'AirPort network password', '-wa', ssid];
return execa(cmd, args)
.then(res => {
if (res.stderr) {
throw new Error(res.stderr);
}
if (!res.stdout) {
throw new Error('Could not get password');
}
return res.stdout;
})
.catch(err => {
if (/The specified item could not be found in the keychain/.test(err.message)) {
err.message = 'Your network doesn\'t have a password';
}
throw err;
});
};
'use strict';
const execa = require('execa');
module.exports = ssid => {
const cmd = 'netsh';
const args = ['wlan', 'show', 'profile', `name=${ssid}`, 'key=clear'];
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('Could not get password');
}
return ret;
});
};
感谢各位的阅读!关于“使用Node.js怎么获取WI-FI密码”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。