脚本
ruby
# 使用io库
def get_cmd_result(cmd)
IO.popen(cmd,:external_encoding=>'GBK').read.encode("utf-8")
end
def list_wifi
wifi_pwds = Hash.new
# 获取所有wifi文件
o1 = get_cmd_result("netsh wlan show profiles")
# 获取所有匹配结果
wifi_names = o1.scan(/所有用户配置文件\s*:\s*(.*?)\n/m)
unless wifi_names.empty?
wifi_names.each do |wifi_name|
o2 = get_cmd_result("netsh wlan show profile name=\"#{wifi_name[0]}\" key=clear")
wifi_pwd = o2.scan(/关键内容\s*:\s*(.*)\n/)
wifi_pwds.update({wifi_name[0] => wifi_pwd[0]})
end
end wifi_pwds
end
puts list_wifi
输出结果
{"TP-LINK_2301"=>["1234567"], "CU_trtsds"=>["123456"], "Mi 10S"=>["12345678"]}
本文由【产品经理不是经理】gzh 同步发布,欢迎关注