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 >.
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'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%---
I need the output of all User profiles only with their names without the first column "All User Profile"
Example:
All User Profile : WifiName
Output:
WifiName
I am using:
for /f %%a in ('netsh wlan show profile | find /i "all user profile"') do set "%%~a"
echo %a% > C:\test.txt
#echo off
for /f "tokens=2 skip=1 delims=:" %%a in ('netsh wlan show profile') do echo %%a >> .\test.txt
Hello dear people and others,
Today i wanted to create a simple script, thought it would be easy to store the outcome to var of the following command:
wmic bios get serialnumber | findstr /N /V SerialNumber
Outcome:
2:H3GK4S1
3:
The problem is when i try to get the serial with wmic, it returns the string as expected but also an empty string/line. When i try to store the serial to a variable it stores it and then directly overwrites it with the empty string. This is the function i nearly got working now:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
And this gives the following output:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
2:H3GK4S1
3:
As can be seen above, the loop overwrites the serial var, if someone can help me towards the right directon to get this working, would be mad.
At the Command Prompt:
For /F "Tokens=1* Delims==" %g In ('WMIC BIOS Get SerialNumber /Value') Do #For /F "Tokens=*" %i In ("%h") Do #Set "serial=%i" & Echo %i
Or in a batch file:
#For /F "Tokens=1* Delims==" %%g In ('WMIC BIOS Get SerialNumber /Value'
) Do #For /F "Tokens=*" %%i In ("%%h") Do #Set "serial=%%i" & Echo %%i
#Pause
EditIf you're happy to use a labelled section in your batch file:
#Echo Off
Set "serial="
For /F "Skip=1 Delims=" %%A In ('WMIC BIOS Get SerialNumber') Do If Not Defined serial Call :Sub %%A
Set serial 2>Nul
Pause
GoTo :EOF
:Sub
Set "serial=%*"
GoTo :EOF
Try like this:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber /format:value') DO for /f "tokens=* delims=" %# in ("%g") do set "serial=%#"
echo %serial%
Mind that's a command that should be executed in the command prompt directly.For a batch file you'll need to double the % in the for loop tokens.
In a batch file, you can also use a goto to end the loop after the first iteration :
#echo off
for /f "tokens=2 delims=:" %%a in ('wmic bios get serialnumber ^| findstr /N /V SerialNumber') do (
set "$var=%%a"
goto:next
)
exit/b
:next
echo Result=^> [%$var: =%]
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.