0

I am trying to get all known WiFi SSIDs including the Password.

When I use the following script, I can't get it to work to set the SSIDs in doublequotes, (so blanks and so on are correctly used).

Also when I've got special characters like exclamation marks, they dissapear.

Can someone please help me?

@ECHO OFF
setlocal EnableExtensions DisableDelayedExpansion
color 02
netsh wlan show profiles | findstr /R /C:"[ ]:[ ]" > temp.txt
echo @echo off >> helper.bat
echo setlocal enabledelayedexpansion >> helper.bat
echo for /f "tokens=5*" %%%%i in (temp.txt) do ( set val=%%%%i %%%%j >> helper.bat
echo if "!val:~-1!" == " " set val=!val:~2,-1! >> helper.bat
echo echo !val! ^>^> final.txt) >> helper.bat
echo for /f "tokens=*" %%%%i in (final.txt) do @echo SSID: %%%%i ^>^> others_%computername%.txt ^& echo # ================================================ ^>^> others_%computername%.txt ^& netsh wlan show profiles name=%%%%i key=clear ^| findstr /N /R /C:"[ ]:[ ]" ^| findstr 33 ^>^> others_%computername%.txt ^& echo # ================================================ ^>^> others_%computername%.txt ^& echo # Key content is the password of your target SSID. ^>^> others_%computername%.txt ^& echo # ================================================ ^>^> others_%computername%.txt >> helper.bat
REM echo del /f /q temp.txt final.txt >> helper.bat
echo exit >> helper.bat
start "" /wait helper.bat
exit

Output is for example: Original (temp.txt):

Profil fr alle Benutzer : 1FRITZ!Box Fon WLAN 7390 

Output in final.txt:

1FRITZBox Fon WLAN 7390 

What it should look like in final.txt:

"1FRITZ!Box Fon WLAN 7390"
Compo
  • 36,585
  • 5
  • 27
  • 39
Markus
  • 41
  • 1
  • 4
  • 1
    Too much extra stuff in here. Learn about [mcve] . You should reduce this to the minimal problem, `echo "1FRITZ!Box Fon WLAN 7390" | problemCode > final.txt` Good luck. – shellter Jul 21 '23 at 22:20
  • 1
    It should also be mentioned that a WiFi passphrase can include every ASCII character in the decimal range 32 to 126. That includes spaces, and doublequotes among those. Also the output from `netsh.exe` is in an inconsistet tabular format, there is no guarantee that either the SSID string, or the passphrase does not return lines ending with space characters which are not in the actual strings. – Compo Jul 22 '23 at 01:11
  • Your "answer" should be added as an edit to your original question. Good luck. – shellter Jul 23 '23 at 21:24

1 Answers1

0

Here is your original code complete with unnecessary things removed, fixes, and improvements. It no longer writes additional files, or defines variables. This should mean that echoing of some characters won't affect your output, and therefore doublequoting the passphrase is unnecessary.

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
(
    For /F "Tokens=4,*" %%G In (
        '%SystemRoot%\System32\netsh.exe WLAN Show Profiles
         ^| %SystemRoot%\System32\findstr.exe /R "\<:\>"'
    ) Do (
        Echo Profile: %%H
        Echo # ======================================================
        %SystemRoot%\System32\netsh.exe WLAN Show Profiles Name="%%H" Key=Clear^
         | %SystemRoot%\System32\findstr.exe /N /R "\<:\>"^
         | %SystemRoot%\System32\findstr.exe /R "^33:"
        Echo # ======================================================
        Echo # Key content is the passphrase for your target Profile.
        Echo # ======================================================
    )
) 1>"others_%ComputerName%.txt"

Please note, I do not recommend that others should use this methodology, as proof would be needed that netsh.exe will always output the target string, on the same line on all systems.

Compo
  • 36,585
  • 5
  • 27
  • 39