java 获取本机连接过的所有WiFI密码

				版权声明:本文为博主原创文章,未经博主允许不得转载。					https://blog.csdn.net/qq_21808961/article/details/80462351				</div>
							            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f1a9c33fcd.css">
					<div class="htmledit_views" id="content_views">
            <p>windows,cmd中可以通过命令行输入指令:</p>
netsh wlan show profiles
  
  

来获取连接过的WiFi名:

然后可以输入命令:获取WiFi名为 的个密码

netsh wlan show profiles name=哦 key=clear
  
  

也可以输入指令:遍历查询所有的wifi信息,然后把控制台打印的结果重定向到文件中保存下来,下次只要打开文件查询即可:

指令如下:我保存到D:\passworld.txt这个文件夹中,下次用文本编辑器打开查找即可。

for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh wlan show profiles') do  @echo %j | findstr -i -v echo | netsh wlan show profiles %j key=clear >>D:\passworld.txt

但是呢,毕竟连接过的WiFi挺多的。里面的WiFi配置信息有的是我们不需要知道的,这里写一个java的命令行程序来遍历,获取,生成WiFi密码文件,生成的目录是桌面:C:\Users\Administrator\Desktop\所有连过的WiFi密码.txt


  
  
  1. package system.cmd;
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintStream;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.Scanner;
  9. public class GetWiFiPassWord
  10. {
  11. /**
  12. * @param commandStr
  13. * cmd 控制台命令
  14. * @return 该控制台命令commandStr运行的结果
  15. */
  16. public static String exeCmd(String commandStr)
  17. {
  18. String result = null;
  19. BufferedReader br = null;
  20. try
  21. {
  22. Process p = Runtime.getRuntime().exec(commandStr);
  23. br = new BufferedReader( new InputStreamReader(p.getInputStream()));
  24. String line = null;
  25. StringBuilder sb = new StringBuilder();
  26. while ((line = br.readLine()) != null)
  27. {
  28. sb.append(line + "\n");
  29. }
  30. // System.out.println(sb.toString());
  31. result = sb.toString();
  32. } catch (Exception e)
  33. {
  34. e.printStackTrace();
  35. } finally
  36. {
  37. if (br != null)
  38. {
  39. try
  40. {
  41. br.close();
  42. } catch (Exception e)
  43. {
  44. e.printStackTrace();
  45. }
  46. }
  47. }
  48. return result;
  49. }
  50. /**
  51. * @throws FileNotFoundException
  52. */
  53. public static void printWiFiPassWord(String result)
  54. throws FileNotFoundException
  55. {
  56. // TODO Auto-generated method stub
  57. Scanner scanner = new Scanner(result);
  58. String line;
  59. String wifi;
  60. String passworld;
  61. while ((line = scanner.nextLine()) != null)
  62. {
  63. // SSID 名称 :“Hello”
  64. if (line.contains( "SSID 名称"))
  65. {
  66. wifi = line.substring(line.lastIndexOf( "“") + 1,
  67. line.length() - 1);
  68. System.out.println( "无线:" + wifi.trim()); // trim()去掉多余的空白符
  69. }
  70. // 关键内容 : *********
  71. else if (line.contains( "关键内容"))
  72. {
  73. passworld = line.substring(line.lastIndexOf( ":") + 1);
  74. System.out.println( "密码:" + passworld.trim()); // trim()去掉多余的空白符
  75. }
  76. }
  77. }
  78. public static String getWiFiMap(String result) throws FileNotFoundException
  79. {
  80. // TODO Auto-generated method stub
  81. Scanner scanner = new Scanner(result);
  82. String line;
  83. String wifi;
  84. String passworld;
  85. StringBuilder buff = new StringBuilder();
  86. HashMap<String, String> WiFiMap = new HashMap<String, String>();
  87. try
  88. {
  89. /*
  90. * 接口 WLAN 上的配置文件 哦: --->WiFi名是哦,位于"接口 WLAN 上的配置文件"这句话和冒号之间
  91. */
  92. // 有这句话说明包含有密码
  93. String WiFiNameLineFlag = "接口 WLAN 上的配置文件";
  94. // 捕获java.util.NoSuchElementException
  95. while ((line = scanner.nextLine()) != null)
  96. {
  97. // SSID 名称 :“Hello”
  98. if (line.contains(WiFiNameLineFlag))
  99. {
  100. wifi = line.substring(
  101. line.lastIndexOf(WiFiNameLineFlag)
  102. + WiFiNameLineFlag.length(),
  103. line.lastIndexOf( ":"));
  104. // System.out.print("无线:"+wifi.trim());//trim()去掉多余的空白符
  105. buff.append( "无线:" + wifi.trim() + "|");
  106. }
  107. // 关键内容 : *********
  108. if (line.contains( "关键内容"))
  109. {
  110. passworld = line.substring(line.lastIndexOf( ":") + 1);
  111. // System.out.println("|密码:"+passworld.trim());//trim()去掉多余的空白符
  112. buff.append( "密码:" + passworld.trim());
  113. }
  114. }
  115. } catch (Exception e)
  116. {
  117. // TODO: handle exception
  118. }
  119. return buff.toString();
  120. }
  121. /**
  122. * 获取连接过的WiFi的名称列表。
  123. *
  124. * @return 所有连接过的WiFi名称列表
  125. */
  126. public static ArrayList<String> getWiFiNameList()
  127. {
  128. String allWiFiName = "netsh wlan show profiles";
  129. String cmdResult = GetWiFiPassWord.exeCmd(allWiFiName);
  130. Scanner scanner = new Scanner(cmdResult); // 扫描结果
  131. ArrayList<String> WiFiNameList = new ArrayList<String>();
  132. String line = null;
  133. try
  134. {
  135. // 会抛出异常 java.util.NoSuchElementException:
  136. while ((line = scanner.nextLine()) != null)
  137. {
  138. // System.out.println(line);
  139. if (line.contains( ":"))
  140. {
  141. String name = line.substring(line.lastIndexOf( ":") + 1)
  142. .trim();
  143. // :后面没有名字的表示这只是个冒号,不是我们想要的WiFi名
  144. if (!name.equals( ""))
  145. WiFiNameList.add(name);
  146. }
  147. }
  148. } catch (Exception e)
  149. {
  150. // 不做处理,这里是为了让程序能运行下去
  151. // TODO: handle exception
  152. }
  153. return WiFiNameList;
  154. }
  155. /**
  156. * cmd查询name对应的WiFi名称配置文件,并返回cmd执行的结果
  157. *
  158. * @param name
  159. * @return
  160. */
  161. public static String getPassWordByName(String name)
  162. {
  163. String commandStr = "netsh wlan show profile name=" + name
  164. + " key=clear";
  165. String result = GetWiFiPassWord.exeCmd(commandStr);
  166. return result;
  167. }
  168. public static void main(String[] args)
  169. throws FileNotFoundException, InterruptedException
  170. {
  171. // 保存下标准输出流
  172. PrintStream out = System.out;
  173. System.out.println( "请勿关闭当前窗口");
  174. System.out.println( "正在生成WiFi密码文件...");
  175. String outFile = "所有连过的WiFi密码.txt";
  176. PrintStream ps = new PrintStream(outFile); // 创建文件输出流
  177. System.setOut(ps); // 设置使用新的输出流,System.out.XXX将输入到文件中
  178. // 获取WiFi名列表
  179. ArrayList<String> WiFiNameList = getWiFiNameList();
  180. for (String string : WiFiNameList)
  181. {
  182. // 根据每个WiFi列表中的WiFi名称,获取WiFi的密码
  183. System.out.println(getWiFiMap(getPassWordByName(string)));
  184. }
  185. // 恢复到原来的标准输出流
  186. System.setOut(out);
  187. System.out.println(
  188. "以生成WiFi密码文件,路径:.\\所有连过的WiFi密码.txt");
  189. // Thread.currentThread().sleep(1000 * 10);
  190. }
  191. }

运行结果:


  
  
  1. 请勿关闭当前窗口
  2. 正在生成WiFi密码文件...
  3. 以生成WiFi密码文件,路径:.\所有连过的WiFi密码.txt

打开当前工作路径下的:所有连过的WiFi密码.txt 文件即可查看WiFi密码,

这里过滤掉了大量的无关信息。只留下WiFi名和密码,这样只有电脑连接过的WiFi下次想在手机或者其他设置上连接该WiFi的时候就不愁找不到密码了。

下面给出可执行获取密码的可执行jar包:获取本机连过的所有WiFi密码

下载后,双击jar包运行即可。

参考博客:

LisenYang的专栏 的博客: 查看计算机连接过的WiFi密码(三种方法)

小蓝的博客  的博客: CMD 获取所有笔记本中连接过的WiFi密码

更新说明:

之前写的我用的是绝对路径,换了路径之后就跑不起来了。以后尽量不要使用绝对路径,生成的文件可以生成在当前目录下。

				版权声明:本文为博主原创文章,未经博主允许不得转载。					https://blog.csdn.net/qq_21808961/article/details/80462351				</div>
							            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f1a9c33fcd.css">
					<div class="htmledit_views" id="content_views">
            <p>windows,cmd中可以通过命令行输入指令:</p>

猜你喜欢

转载自blog.csdn.net/weixin_41722928/article/details/86664730