如何使用java从cmd获取wifi密码

时间:2015-12-28 10:07:55

标签: java cmd

我尝试用java提供我的wifi连接密码。但我必须以管理员的身份做这件事。

try {
    Process p = Runtime.getRuntime().exec("netsh.exe wlan show profiles name=superonline key=clear");
    BufferedReader bf = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while (bf.readLine() != null)
        System.out.println(bf.readLine());
} catch (Exception ex) {
    ex.printStackTrace();
}

当我以标准用户身份尝试时:

enter image description here

当我以管理员身份尝试时:

如何在java代码中执行此操作?

enter image description here

2 个答案:

答案 0 :(得分:0)

你无法通过java获得这种访问权。

您可以做的最好的事情是将命令的输出包装到InputStream中并尝试从那里读取数据

  public static void main(String[] args) {
   try {
   // create a new process
   System.out.println("Creating Process...");
   Process p = Runtime.getRuntime().exec("netsh.exe wlan .......");

   // get the input stream of the process and print it
   InputStream in = p.getInputStream();
   for (int i = 0; i < in.available(); i++) {
   System.out.println("" + in.read());
   }

   // wait for 10 seconds and then destroy the process
   Thread.sleep(10000);
   p.destroy();

   } catch (Exception ex) {
   ex.printStackTrace();
   }

   }

答案 1 :(得分:0)

使用java.util.Scanner类:

Process p = Runtime.getRuntime().exec("netsh wlan show profiles name=superonline key=clear"); 
Scanner sc=new Scanner(p.getInputStream());
while (sc.hasNextLine()) {
    System.out.println(sc.nextLine());
}