I'd like to create simple script to show wlan password on Windows10.
This will be well solution for users that's not familiar with cmd commands.
On Windows 7 it could be done using GUI, but not on newer OSes.
I stuck on line
for /f "tokens=*" %%j in ('netsh wlan show profile %ssid% key=clear ^| findstr "Key Content"') do set wlan_password=%%j
The variable wlan_password is always null. Even if i change set instruction to e.g. echo it shows that syntax is incorrect. I can't troubleshoot that.
Why the line above doesn't work, but the line:
for /f "tokens=*" %%i in ('netsh wlan show interfaces ^| findstr "Profile"') do set wlan_output=%%i
works well?
#echo off
set wlan_output=
set connected_ssid=
set ssid=
set wlan_password=
for /f "tokens=*" %%i in ('netsh wlan show interfaces ^| findstr "Profile"') do set wlan_output=%%i
for /f "tokens=2 delims=:" %%a in ("%wlan_output%") do set connected_ssid=%%a
call :TRIM %connected_ssid% connected_ssid
set ssid=%1
if "%ssid%"=="" set /p "ssid=Podaj nazwe sieci [%connected_ssid%]: " || set "ssid=%connected_ssid%"
if not "%ssid%"=="" (
for /f "tokens=*" %%j in ('netsh wlan show profile %ssid% key=clear ^| findstr "Key Content"') do set wlan_password=%%j
echo "Haslo do sieci %ssid%: %wlan_password%"
exit /b
)
else (
echo "Nie podano nazwy sieci. Nie mozna odczytac hasla"
exit /b
)
pause
exit /b
:TRIM
SET %2=%1
GOTO :EOF
Ugh - sorry for not spotting the real problem sooner: you have also to escape the = within the for command:
for /f "tokens=*" %%j in ('netsh wlan show profile %ssid% key^=clear ^| findstr /c:"Key Content"') do set wlan_password=%%j
set wlan_password
Note: use findstr /c:"Key Content" or find "Key Content", because findstr "Key Content" returns each line that contains Key OR Content (or both). (Not that it would make any difference in this special case, but without /c: it will bite you sooner or later)
To get the key only:
for /f "tokens=1,* delims=:" %%j in ('netsh wlan show profile %ssid% key^=clear ^| find "Key Content"') do set "wlan_password=%%k"
set "wlan_password=%wlan_password:~1%"
echo ---%wlan_password%---
Related
I'm writing a simple little batch file that gets the password of a saved Wi-Fi network, but I want to grab the Key Content, then paste it on its own. Here's the current code:
#echo off
set /p name=Enter Wi-Fi Name:
cls
echo %name%
netsh wlan show profile name="%name%" key=clear
cmd /k
This gives me a long list of data, but the line I'm looking for is the "Key Content" line. What I essentially want to do is grab the "Key Content" line, clear all the lines, then echo the "Key Content" line. Is this possible without any plugins on Windows 11?
I'm new to the site and coding as a whole, by the way, so what may seem like something completely obvious to you is something I have a 95 percent chance not to know. Thank you!
Here's a batch-file snippet, based upon you having received and properly validated the end users input:
#For /F "Tokens=1,* Delims=:" %%H In ('
%SystemRoot%\System32\netsh.exe WLAN Show Profiles Name^="%name%" Key^=Clear
2^>NUL ^| %SystemRoot%\System32\findstr.exe /RIC:"^[ ][ ]*Key Content[ ][ ]*: "
') Do #(Set "}=%%I" & SetLocal EnableDelayedExpansion
For %%J In ("!}:~1!") Do #EndLocal & Echo(%%J)
You should always perform robust validation of any end users input, especially when using the Set /P command, which can accept nothing or absolutely anything, (including malicious content). If the target systems are Windows 8 / Server 2012 or above, then I wouldn't waste time asking the end user to self-determine the wireless profile name, and then type it correctly at a prompt. I'd just output all of the profile names, along side their returned keys.
The following examples are untested, and assume that your 'passwords' do not include double-quote characters. They should provide the output in a CSV-like format, ("ProfileName","Password"):
#For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
/NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Get Name
/Format:MOF 2^>NUL') Do #For /F "Tokens=1,* Delims=:" %%H In ('
%SystemRoot%\System32\netsh.exe WLAN Show Profiles Name^="%%G" Key^=Clear
2^>NUL ^| %SystemRoot%\System32\findstr.exe /RIC:"^[ ][ ]*Key Content[ ][ ]*: "
') Do #(Set "}=%%I" & SetLocal EnableDelayedExpansion
For %%J In ("!}:~1!") Do #EndLocal & Echo("%%G",%%J)
#Pause
As you appear to have used the cmd tag, despite your question being about a batch-file, you could probably do similarly, directly in the Command Prompt, (cmd.exe) too:
For /F Tokens^=6^ Delims^=^" %G In ('%SystemRoot%\System32\wbem\WMIC.exe /NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Get Name /Format:MOF 2^>NUL') Do #For /F "Tokens=1,* Delims=:" %H In ('"%SystemRoot%\System32\netsh.exe WLAN Show Profiles Name="%G" Key=Clear 2>NUL | %SystemRoot%\System32\findstr.exe /RIC:"^[ ][ ]*Key Content[ ][ ]*: ""') Do #Set "}=%I" & For /F Delims^=^ EOL^= %J In ('%SystemRoot%\System32\cmd.exe /V /D /C "Echo "%G","!}:~1!""') Do #(Set /P ="%J") 0<NUL & Echo(
I would like to run this as a 1 line command (not from a batch), but however I try, I cannot succeed:
(for /f "tokens=2 delims=:" %%a in ('netsh wlan show profiles ^| findstr "Profile"') do (
set str=%%a
set str=!str:~1!
echo !str!
)) >> wifi_networks.txt
This is what I tried:
(for /f "tokens=2 delims=:" %%a in ('netsh wlan show profiles ^| findstr "Profile"') do (set str=%%a & set str=!str:~1! & echo !str!)) >> wifi_networks.txt
Any help please?
When using a batch file, you double up the %'s, as you aren't they should be returned to single again.
Try this, (untested):
(For /F "Tokens=2Delims=:" %A In ('NetSh WLAN Show Profiles^|Find "Profile"') Do #For /F "Tokens=*Delims= " %B In ("%A") Do #Echo %B)>>"wifi_networks.txt"
If wifi_networks.txt doesn't already exist you can change >> to >.
I want to return the Default Gateway like i have for the IPv4 but it returns Blank.
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b
set ip=%ip:~1%
echo.
echo IP Address is: %ip%
echo.
This is what i have so far but it returns blank, i have tried to alter it but it still returns a blank answer.
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "Default"') do set ip=%%b
set ip=%ip:~2%
echo.
echo The Gateway is: %ip%
echo.
Any ideas how i can do this. and return the default gateway for a given Computer.
Here's a WMIC script:
#echo off
for /f "tokens=2,3 delims={,}" %%a in ('"WMIC NICConfig where IPEnabled="True" get DefaultIPGateway /value | find "I" "') do echo IPv4 %%~a IPV6 %%~b
pause
set "ip="
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "Default"') do if not defined ip set ip=%%b
Take a look at the listing from ipconfig - it's likely that there is more than one default line. You would therefore get the data from the last line containing default
The above construct returns the first line containing default.
This worked for me.
Got first listed gateway.
#echo off
set "ip="
for /f "tokens=2,3 delims={,}" %%a in ('"WMIC NICConfig where IPEnabled="True" get DefaultIPGateway /value | find "I" "') do if not defined ip set ip=%%~a
echo IP Mobile Address is: %ip%
pause
for /f "tokens=1-5" %%a in ('route -4 print 0.*'
) do #if "%%e"=="" if "%%a"=="%%b" set "ip=%%c"
This will extract the default gateway from the ipv4 route table.
I am making a program that checks if a user's IP is a certain IP address.
Currently, I created a successful internal IP version:
#echo off
set userIp=192.168.90.100
for /f "tokens=4 delims= " %%i in ('route print ^| find " 0.0.0.0"') do set localIp=%%i
for /f "delims=[] tokens=2" %%a in ('ping %computername% -4 -n 1 ^| findstr "["') do set thisip=%%a
goto :Check
:Check
if %localIp%==%userIp% goto :Good
if %thisip%==%userIp% goto :Good
goto :Bad
And I am trying to make the same thing that works with external IPs.
I researched online, and here is what I got so far.
#echo off
for /f "tokens=2 delims=:" %%a IN ('nslookup myip.opendns.com. resolver1.opendns.com ^| findstr /IC:"Address"') do if /i %%a=="10.11.12.13" goto :Good
goto :Bad
I need a bit of help on how to fix this.
With pure batch/already present tools:
EDIT: changed the batch to properly handle also IPv6 addresses
#Echo off
for /f "tokens=1* delims=: " %%A in (
'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
) Do set ExtIP=%%B
Echo External IP is : %ExtIP%
Reference
Another one with powershell:
#Echo off
For /f %%A in (
'powershell -command "(Invoke-Webrequest "http://api.ipify.org").content"'
) Do Set ExtIP=%%A
Echo External IP is : %ExtIP%
And another slightly different powershell variant:
#Echo off
For /f %%A in (
'powershell -nop -c "(Invoke-RestMethod http://ipinfo.io/json).IP"'
) Do Set ExtIP=%%A
Echo External IP is : %ExtIP%
To get your public IP without additional parsing do this:
curl "http://api.ipify.org"
EDIT:
This version is more reliable across windows language versions:
for /f "tokens=3 delims== " %%A in ('
nslookup -debug myip.opendns.com. resolver1.opendns.com 2^>NUL^|findstr /C:"internet address"
') do set "ext_ip=%%A"
First it seems there is a . too much after the first .com.
Second when using your command with simply google.com and echo %a I get the following:
" xx.xx.xx.x" without the quotes and with two leading spaces!
So your if will never be true!
Change it to something like this: if "%%a"==" xx.xx.xx.x" Goto:good and you should be fine.
I want to generate a txt file with my dns history. Although the batch script executes just fine on windows 8, when i run it on windows 7 it simply creates a blank txt file. Does anyone knows why this is happening?
Here's the batch script
#echo off
setlocal enableextensions
set "baseName=dnshistory"
set "count=0"
for /f "delims=%baseName%." %%a in (
'dir /b /o-d "%baseName%*.txt" 2^>nul'
) do ( set /a "count=%%a+1" & goto saveData )
:saveData
ipconfig /displaydns | find "Record Name" > "%baseName%%count%.txt"
Is you Windows 7 version in English too ?
Open a CMD windows and test just the command :
ipconfig /displaydns | find /i "Record Name"
and look if something is displayed.
If not, try just the command :
ipconfig /displaydns
and look the language used Then correct your code with the correct words.
IE in Portuguese it will be :
ipconfig /displaydns | find /i "Nome do Registro"
try this for a language independent solution:
:saveData
(for /f "tokens=2 delims=:" %%a in ('ipconfig /displaydns') do (
echo %%a| find "." |findstr /v /r "[0-9]$"
))>file.txt
(take every line, filter those, that have a . after a : (second token) and filter out all lines that end with a number)
EDIT another approach (because the above gives some unwanted lines):
find the first line after every ----------------- line:
#echo off
setlocal enabledelayedexpansion
ipconfig /displaydns |findstr /n "^" >a.txt
for /f "tokens=1 delims=:" %%a in ('findstr /c:" --------------" a.txt') do (
set /a line=%%a+1
for /f "tokens=1,2,* delims=:" %%i in ('findstr /B "!line!:" a.txt') do echo(%%k
)