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
-
package system.cmd;
-
-
import java.io.BufferedReader;
-
import java.io.FileNotFoundException;
-
import java.io.InputStreamReader;
-
import java.io.PrintStream;
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.Scanner;
-
-
public class GetWiFiPassWord
-
{
-
/**
-
* @param commandStr
-
* cmd 控制台命令
-
* @return 该控制台命令commandStr运行的结果
-
*/
-
public static String exeCmd(String commandStr)
-
{
-
String result = null;
-
BufferedReader br = null;
-
try
-
{
-
Process p = Runtime.getRuntime().exec(commandStr);
-
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
-
String line = null;
-
StringBuilder sb = new StringBuilder();
-
while ((line = br.readLine()) != null)
-
{
-
sb.append(line + "\n");
-
}
-
// System.out.println(sb.toString());
-
result = sb.toString();
-
} catch (Exception e)
-
{
-
e.printStackTrace();
-
} finally
-
{
-
if (br != null)
-
{
-
try
-
{
-
br.close();
-
} catch (Exception e)
-
{
-
e.printStackTrace();
-
}
-
}
-
}
-
-
return result;
-
}
-
/**
-
* @throws FileNotFoundException
-
*/
-
public static void printWiFiPassWord(String result)
-
throws FileNotFoundException
-
{
-
// TODO Auto-generated method stub
-
Scanner scanner = new Scanner(result);
-
String line;
-
String wifi;
-
String passworld;
-
while ((line = scanner.nextLine()) != null)
-
{
-
// SSID 名称 :“Hello”
-
if (line.contains("SSID 名称"))
-
{
-
wifi = line.substring(line.lastIndexOf("“") + 1,
-
line.length() - 1);
-
System.out.println("无线:" + wifi.trim());// trim()去掉多余的空白符
-
}
-
// 关键内容 : *********
-
else if (line.contains("关键内容"))
-
{
-
passworld = line.substring(line.lastIndexOf(":") + 1);
-
System.out.println("密码:" + passworld.trim());// trim()去掉多余的空白符
-
}
-
}
-
}
-
public static String getWiFiMap(String result) throws FileNotFoundException
-
{
-
// TODO Auto-generated method stub
-
Scanner scanner = new Scanner(result);
-
String line;
-
String wifi;
-
String passworld;
-
StringBuilder buff = new StringBuilder();
-
HashMap<String, String> WiFiMap = new HashMap<String, String>();
-
try
-
{
-
/*
-
* 接口 WLAN 上的配置文件 哦: --->WiFi名是哦,位于"接口 WLAN 上的配置文件"这句话和冒号之间
-
*/
-
// 有这句话说明包含有密码
-
String WiFiNameLineFlag = "接口 WLAN 上的配置文件";
-
// 捕获java.util.NoSuchElementException
-
while ((line = scanner.nextLine()) != null)
-
{
-
// SSID 名称 :“Hello”
-
-
if (line.contains(WiFiNameLineFlag))
-
{
-
wifi = line.substring(
-
line.lastIndexOf(WiFiNameLineFlag)
-
+ WiFiNameLineFlag.length(),
-
line.lastIndexOf(":"));
-
// System.out.print("无线:"+wifi.trim());//trim()去掉多余的空白符
-
buff.append("无线:" + wifi.trim() + "|");
-
}
-
// 关键内容 : *********
-
if (line.contains("关键内容"))
-
{
-
passworld = line.substring(line.lastIndexOf(":") + 1);
-
// System.out.println("|密码:"+passworld.trim());//trim()去掉多余的空白符
-
buff.append("密码:" + passworld.trim());
-
}
-
}
-
} catch (Exception e)
-
{
-
// TODO: handle exception
-
}
-
-
return buff.toString();
-
}
-
/**
-
* 获取连接过的WiFi的名称列表。
-
*
-
* @return 所有连接过的WiFi名称列表
-
*/
-
public static ArrayList<String> getWiFiNameList()
-
{
-
String allWiFiName = "netsh wlan show profiles";
-
String cmdResult = GetWiFiPassWord.exeCmd(allWiFiName);
-
Scanner scanner = new Scanner(cmdResult);// 扫描结果
-
ArrayList<String> WiFiNameList = new ArrayList<String>();
-
String line = null;
-
try
-
{
-
// 会抛出异常 java.util.NoSuchElementException:
-
while ((line = scanner.nextLine()) != null)
-
{
-
// System.out.println(line);
-
if (line.contains(":"))
-
{
-
String name = line.substring(line.lastIndexOf(":") + 1)
-
.trim();
-
// :后面没有名字的表示这只是个冒号,不是我们想要的WiFi名
-
if (!name.equals(""))
-
WiFiNameList.add(name);
-
}
-
}
-
} catch (Exception e)
-
{
-
// 不做处理,这里是为了让程序能运行下去
-
// TODO: handle exception
-
}
-
return WiFiNameList;
-
}
-
/**
-
* cmd查询name对应的WiFi名称配置文件,并返回cmd执行的结果
-
*
-
* @param name
-
* @return
-
*/
-
public static String getPassWordByName(String name)
-
{
-
String commandStr = "netsh wlan show profile name=" + name
-
+ " key=clear";
-
String result = GetWiFiPassWord.exeCmd(commandStr);
-
return result;
-
}
-
public static void main(String[] args)
-
throws FileNotFoundException, InterruptedException
-
{
-
// 保存下标准输出流
-
PrintStream out = System.out;
-
System.out.println("请勿关闭当前窗口");
-
System.out.println("正在生成WiFi密码文件...");
-
String outFile = "所有连过的WiFi密码.txt";
-
PrintStream ps = new PrintStream(outFile); // 创建文件输出流
-
System.setOut(ps); // 设置使用新的输出流,System.out.XXX将输入到文件中
-
// 获取WiFi名列表
-
ArrayList<String> WiFiNameList = getWiFiNameList();
-
for (String string : WiFiNameList)
-
{
-
// 根据每个WiFi列表中的WiFi名称,获取WiFi的密码
-
System.out.println(getWiFiMap(getPassWordByName(string)));
-
}
-
// 恢复到原来的标准输出流
-
System.setOut(out);
-
System.out.println(
-
"以生成WiFi密码文件,路径:.\\所有连过的WiFi密码.txt");
-
// Thread.currentThread().sleep(1000 * 10);
-
}
-
}
运行结果:
请勿关闭当前窗口 正在生成WiFi密码文件... 以生成WiFi密码文件,路径:.\所有连过的WiFi密码.txt
打开当前工作路径下的:所有连过的WiFi密码.txt 文件即可查看WiFi密码,
这里过滤掉了大量的无关信息。只留下WiFi名和密码,这样只有电脑连接过的WiFi下次想在手机或者其他设置上连接该WiFi的时候就不愁找不到密码了。
下面给出可执行获取密码的可执行jar包:获取本机连过的所有WiFi密码
下载后,双击jar包运行即可。
参考博客:
LisenYang的专栏 的博客: 查看计算机连接过的WiFi密码(三种方法)
小蓝的博客 的博客: CMD 获取所有笔记本中连接过的WiFi密码
更新说明:
之前写的我用的是绝对路径,换了路径之后就跑不起来了。以后尽量不要使用绝对路径,生成的文件可以生成在当前目录下。