在批量编程的准确输出方面需要帮助

时间:2017-03-21 15:57:56

标签: batch-file

我正在运行代码:

netsh wlan show profile name=%INPUT% key=clear | findstr Key>password.txt

输出(在password.txt文本文件中):

Key Content            : mywifipassword

我需要在单独的文件password.txt中输出“mywifipassword”。

不要在输出文件中输入“Key Content:”字样。

5 个答案:

答案 0 :(得分:1)

要将命令的输出捕获到变量中,请使用for循环:

for /f "tokens=2 delims=:" %%a in ('netsh wlan show profile name^="%input%" key^=clear ^|find  "Key Content"') do set "pwd=%%a"
>password.txt echo(%pwd:~1%

for获取第二个标记(冒号后),因此您需要删除开头的空格。

答案 1 :(得分:0)

for /f "tokens=1*delims=:" %%a in ('netsh wlan show profile name=%INPUT% key=clear ^| findstr Key') do set "password=%%b"
echo %password:~1%>password.txt

该命令放在引号内,管道|使用插入符^进行转义,以指示管道是命令的一部分,而不是for

for /f读取命令的每一行输出,在:上进行标记,将第一个标记(最多:)分配到%%a以及该行的其余部分(在:之后%%b

......正如Lotpings指出的那样,%%b将有一个前导空格,所以把它放在一个变量中并从第二个字符输出(1,从“字符0”开始计算)

OR

for /f "tokens=3delims=: " %%a in ('netsh wlan show profile name=%INPUT% key=clear ^| findstr Key') do echo %%a>password.txt

其中分隔符为: Space ,因此令牌1变为Key,令牌2 Content和令牌3为密码。

如果密码可以包含空格,则使用“tokens = 2 * delims =:”和echo %%b

答案 2 :(得分:0)

试试这个,

set /p INPUT="Type your wifi name:"
netsh wlan show profile name=%INPUT% key=clear>pass.txt
for /f "tokens=2 delims=[" %%f in (pass.txt) do (for /f "delims=]" %%g in ("%%f") do echo %%g)>password.txt
del pass.txt

答案 3 :(得分:0)

我猜你遇到的问题是缺少UAC。除非从高架cmd窗口开始,否则Netsh不会返回Key值。

如果尚未运行提升,则以下批处理将使用PowerShell运行具有其自身实例的提升控制台。

它会递归所有个人资料并显示个人资料,SSID和密钥。 我的Wlan中没有Windows客户端,所以我无法自己测试它。

:: Enum-SSIDKEY.cmd
@echo off & setlocal EnableExtensions DisableDelayedExpansion
:: elevate if not already Admin
net file 1>nul 2>&1 || (powershell -ex unrestricted -Command ^
  "Start-Process -Verb RunAs -FilePath '%comspec%' -ArgumentList '/c %~f0 %*'"
  goto :eof)
:: Put code here that needs elevation
For /F "tokens=3* delims=: " %%A in (
  'netsh wlan show profiles ^|findstr User.Profile '
) Do (Set "SSID=" & Set "Key="
      For /F "tokens=1,2* delims= " %%S in (
        'netsh wlan show profile name^="%%~B" key^=clear ^|findstr "SSID Key"'
      ) Do Set %%S=%%U
      Echo Profile: %%B SSID: %SSID% has Key: %Key%
)

答案 4 :(得分:0)

您可以尝试使用我在Windows 7(法语机器)上测试过的代码:

@echo off
:Main
Title  WiFi Password Recovery by Hackoo 2017
Mode con cols=45 lines=30 & color 9E
echo [SSID] Menu :
set "pwd="
echo(
Setlocal enabledelayedexpansion
for /f "skip=2 delims=: tokens=2" %%a in ('netsh wlan show profiles') do (
    if not "%%a"=="" (
        set "ssid=%%a"
        set "ssid=!ssid:~1!"
        echo [!ssid!]
    )
)
EndLocal
echo(
Set /p "Input=Type your SSID Name : "
cls
Mode con cols=85 lines=5
for /f "tokens=2 delims=:" %%a in ('netsh wlan show profile name^="%Input%" key^=clear ^|find /I "Conten"') do set "pwd=%%a"
    If defined pwd (
        echo(
        echo  The password of the SSID [%Input%] is ==^> "%pwd:~1%" without double quotes
    ) else (
        echo(
        color 0C
        echo The password of the SSID [%Input%] is empty or not defined !
    )
)
echo(
echo Hit any key to return to SSID Menu
Pause>nul & goto Main

或者,如果您想将所有密码保存在文本文件中,如下所示:

@echo off & setlocal enabledelayedexpansion
Title  WiFi Password Recovery by Hackoo 2017
Mode con cols=75 lines=30
cls & color 9E & echo.
ECHO                  **************************************
echo                         WiFi Password Recovery
ECHO                  **************************************
echo.
if _%1_==_Main_  goto :Main

:getadmin
    echo                    %~nx0: elevating self
    set vbs=%temp%\getadmin.vbs
(
    echo Set UAC = CreateObject^("Shell.Application"^)
    echo UAC.ShellExecute "%~s0", "Main %~sdp0 %*", "", "runas", 1
)> "%vbs%"
    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
goto :eof
::*************************************************************************************
:Main
echo                        [SSID] ====^> "Password"
echo [SSID] ====^> "Password" > "%~dp0PassWifi.txt"
for /f "skip=2 delims=: tokens=2" %%a in ('netsh wlan show profiles') do (
    if not "%%a"=="" (
        set "ssid=%%a"
        set "ssid=!ssid:~1!"
        call :getpass "!ssid!"
    )
)
echo.
echo.
echo Done
If Exist "%~dp0PassWifi.txt" start "" "%~dp0PassWifi.txt"
pause>nul
exit /b
::*************************************************************************************
:getpass
set name=%1
set name=!name:"=!
Set passwd=
for /f "delims=: tokens=1,2" %%a in ('netsh wlan show profiles %1 key^=clear ^|find /I "Conten"') do (
    set "passwd=%%b"
)
If defined passwd (
    set passwd=!passwd:~1!
    echo                  [!name!] ====^> "!passwd!"
    echo [!name!] ====^> "!passwd!" >> "%~dp0PassWifi.txt"
) else (
    echo                  [!name!] ====^> empty or not defined
    echo [!name!] ====^> empty or not defined >> "%~dp0PassWifi.txt"
)
exit /b
::*************************************************************************************