获取当前网络已连接的WIFI密码-来自utools插件

const cp = require('child_process')
let wifiPass
if (utools.isWindows()) {
  let stdoutBuffer = cp.execFileSync('netsh', ['wlan', 'show', 'interface'])
  let stdoutText = new TextDecoder('gbk').decode(stdoutBuffer)
  let ret = /^\s*SSID\s*: (.+)\s*$/gm.exec(stdoutText)
  if (!ret) throw new Error('未找到网络已连接的 WiFi 名称')
  const args = ['wlan', 'show', 'profile', `name=${ret[1]}`, 'key=clear']
  stdoutBuffer = cp.execFileSync('netsh', args)
  stdoutText = new TextDecoder('gbk').decode(stdoutBuffer)
  ret = /^\s*(?:Key Content|关键内容)\s*: (.+)\s*$/gm.exec(stdoutText)
  if (!ret) throw new Error('未能获取 WiFi 密码')
  wifiPass = ret[1]
} else if (utools.isMacOS()) {
  let stdoutText = cp.execFileSync('/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport', ['-I']).toString()
	if (stdoutText.includes('AirPort: Off')) throw new Error('WiFi 已关闭')
	let ret = /^\s*SSID: (.+)\s*$/gm.exec(stdoutText)
  if (!ret) throw new Error('未找到网络已连接的 WiFi 名称')
	const args = ['find-generic-password', '-D', 'AirPort network password', '-wa', ret[1]]
  wifiPass = cp.execFileSync('security', args).toString().trim()
}
utools.copyText(wifiPass)
print('WiFi 密码已复制')
return 'WiFi 密码:' + wifiPass

 

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注