Is there a variable for the connected wlan? - variables

I started writing this small batch file, where I get all the wlan data with:
netsh wlan show profile (name) key=clear
This is not a problem but I am asking if there is a variable like for example:
%CurrentWlan%
so I can do:
netsh wlan show profile %CurrentWlan% key=clear >wlan.txt

Here is a possible solution:
#echo off
for /f "eol=B tokens=*" %%A IN ('netsh wlan show interfaces ^| findstr SSID') do (
for /f "delims=: tokens=2" %%A IN ("%%A") do (
for /f "tokens=*" %%A IN ("%%A") do (
netsh wlan show profiles "%%A" key=clear >wlan.txt
)
)
)
Which I am going to break it down:
We first parse the output of the command netsh wlan show interfaces searching for SSID string. As there is also a line containing BSSID we ignore it with eol=B.
Now, we want to parse the value after : symbol, so we set it as delimeter. We can access the network name, now, setting tokens to 2.
We remove all unneded spaces in the result with another for loop specifying tokens option to *.
So, now, we want all the info about currently connected network (%%A). We redirect output to wlan.txt.

Related

grab batch text content and put it in a variable

okay i have this command
netsh wlan show profiles
pretend that the wireless network that appears is named Zask.router
the prompt should appear something like this;
..........................................................
Profiles on interface wifi:
group policy profiles (read only)
<none>
user profiles
All User Profile : Zask.router
..........................................................
is there any way i can get the content "Zask.router" to be placed in a variable?
okay thanks, new question. i changed the code to this;
for /f "tokens=4 delims=: " %%A in ('netsh wlan show profiles') do set "profile=%%A"
now i added this to the next line;
netsh wlan show profiles "%profile%" key=clear
then something that says "key content" (must be administrator) should appear with a wifi password next to it, how do i get that password to also go into a variable?
i tried doing something like this but it just displays the word "such" for some strange reason...
for /f "tokens=4 delims=: " %%A in ('netsh wlan show profiles') do set "profile=%%A
for /f "tokens=4 delims=: " %%A in ('netsh wlan show profiles "%profile%" key=clear') do set "profile2=%%A"
echo %profile2%
Assuming that the output you want is on the last line...
for /f "tokens=4 delims=: " %%A in ('netsh wlan show profiles') do set "YourVariable=%%A"
Use the "FOR /?" command to see help for FOR command. We are using it to split your lines into tokens separated by either spaces or :
tokens=4 specifies that we want to save the 4th token
delims=:(space) specifies that the characters ":" and " " are delimiters
YourVariable is being set for every line in the output of the NETSH command. In this case, since we only care about the last line it works out without the need to compare text to find the desired line.

How can I set the line a string is found on as a variable, and then find the contents of that line number in another file?

Okay, this is kind of complicated so ill try to explain it as best as is can.
I am currently writing a simple program for my own use using notepad.
I am using the language batch and running the program through Command Prompt.
Part of the Program lets you access an account you created with username and password:
set /p USERNAME1= Username?
findstr /n "%USERNAME1%" Usernames.txt
In Usernames.txt are the usernames of each account that has been created, one per line.
If your username is found, it is displayed along with the line number before it in the program.
It then asks for your password:
set /p PASSWORD1= Password?
This is where the problem starts. When the accounts are created, the usernames are stored, one per line, in Usernames.txt , and so are the passwords but in Passwords.txt
I need the program to check if the password you typed is the same as the password on the same line number the username is on, in Passwords.txt
I know this is complicated but if anyone can help it would be greatly appreciated!
Thanks
You're using findstr /N to get the line number of usernames.txt, which is a good start. You're getting the entire line, prefixed with the line number and a colon :.
So for /F can be used to extract the number only. The option string "tokens=1 delims=:" defines to divide the found line at the (first) :, so the line number is separated from the user name.
Finally, another for /F can be used to get the line of the passwords.txt files.
Putting all those things together, the following code snippet emerges:
set /P USERNAME1=Username?
set /P PASSWORD1=Password?
set /A LINENUMBER=0
for /F "tokens=1 delims=:" %%I in (
'findstr /N /I /X /C:"%USERNAME1%" "\path\to\usernames.txt"'
) do (
set LINENUMBER=%%I
goto :CONTINUE1
)
:CONTINUE1
set /A LINENUMBER-=1
if %LINENUMBER% lss 0 (
exit /B
) else if %LINENUMBER% equ 0 (
set SKIPPING=
) else (
set SKIPPING=skip=%LINENUMBER%
)
for /F "usebackq %SKIPPING% delims=" %%I in (
"\path\to\passwords.txt"
) do (
if "%%I" equ "%PASSWORD1%" (
goto :CONTINUE2
)
exit /B
)
:CONTINUE2
rem do something...
So the variable USERNAME1 holds the entered user name to search and PASSWORD1 the entered password.
If the user name cannot be found in usernames.txt, or the entered password does not match the found one, the script is terminated using exit /B.
Note that user names are compared in a case-insensitive manner (/I switch), but the password are compared case-sensitively.

batch list net view and write their ip address to a variable

I basically need a script (batch) that automatically converts the users on the local network to IP addresses. What I mean is basically set every user that shows up in the command "net view" to a different variable, for example:
If I had 4 different users on the network, I would need the file to list:
1: (%1%)
2: (%2%)
3: (%3%)
4: (%4%)
So I need the script to set each user on the network to a different variable (starting at 1)
Also...
How would I set the local IP address of each computer name as a variable?
update for second part: I need to know how to set the ip address as a variable. I just need to cut out the excess stuff. If I type
ping (computername) -4
I get: well, I guess it is just easier to show you...
I think I need to use the findstr command, but I don't know.
Is this what you're looking for?
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1" %%a in ('net view') do (
set comp=%%a & set comp=!comp:\\=!
for /f "tokens=2 delims=[]" %%b in (
'ping -4 !comp!'
) do (Echo !comp! - %%b)
)

How to split an ip address into seperate octets and perform arithmetic in cmd

What I am trying to do is configure some vlan scopes via cmd. What I have accomplished so far is to retrieve the IP Address of the Domain Controller and remove the space in front of the value. I have accomplished this using the following commands:
rem extract ip address via local cmd
for /f "tokens=2 delims=:" %i in ('ipconfig ^| find /i "IPv4 Address"') do set ip_address=%i
example result: set ip_address= 10.0.0.25
rem remove empty space from ip address var
set ip_address=%ip_address: =%
echo %ip_address% now results in 10.0.0.25 without the space in front.
What I would like to do next is split the ip_address variable in separate octet variables so that arithmetic can be performed on the octet of choice.
For example: 10.0.0.25 could then be manipulated to reflect 10.[+100].0.[-24]
the desired result would then be 10.100.0.1
I would prefer to accomplish this strictly using windows command line but if a better method exits I am open to suggestions.
Thanks in advance
Joel
Try this to split up the octets into variables.
#echo off
set "ip=10.0.0.25"
SET "offsets=0.100.0.-24"
#echo off
for /f "tokens=1-4 delims=. " %%a in ("%ip%") do (
set octetA=%%a
set octetB=%%b
set octetC=%%c
set octetD=%%d
)
FOR /f "tokens=1-4 delims=." %%a in ("%offsets%") do (
SET /a octetA+=%%a
SET /a octetB+=%%b
SET /a octetC+=%%c
SET /a octetD+=%%d
)
echo "%octetA%","%octetB%","%octetC%","%octetD%"
pause
This is the same solution of foxidrive, but squashed a little... :)
#echo off
set "ip= 10.0.0.25"
SET "offsets=0.100.0.-24"
for /F "tokens=1-4 delims=. " %%a in ("%ip%") do (
for /F "tokens=1-4 delims=." %%i in ("%offsets%") do (
set /A octetA=%%a+%%i, octetB=%%b+%%j, octetC=%%c+%%k, octetD=%%d+%%l
)
)
echo "%octetA%","%octetB%","%octetC%","%octetD%"
pause
Are you aware of autohotkey?
Also, you could try a powershell script.

How to type in between two bracets-Batch File

Hey well I'm here again and this time I want to know how to type in between bracets from an input variable for the input of an IP Address.
I want the code to look something like this.
set input=
set /p input=IP Address: [ ].[ ].[ ].[ ]
And once I've executed the batch and typed in my IP I want it to look like this.
IP Address: [ 127 ].[ 0 ].[ 0 ].[ 1 ]
Is there anyway of doing this?
I think this would be a very complicated thing to do.
If you would try to do this in Batch you need to rewrite your line with braces on every keyboardhit the user does. The only command I know so far which listens on a single KB-hit is the Choice command.
If you manage it somehow to repeat the users input on every KB-hit you need to bytehack your batch file with backspace characters and have a logic to combine the users input with the braces. This is not only very unconfortable but also very slow.
I would recommend to let your users enter any string and after this checking it by "regex" (windows regex is not very nice)
Here is an example function which I use to check IPs of printers.
%printer% would be the string your user enters. This functions also has an nslookup function. You can ommit this if you like.
This 2nd line does all the magic, but you can also enter some IPs which are not possible like 321.099.000.666
:CheckIP
echo %printer% | findstr /R "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*">nul || goto :EOF
FOR /F "tokens=* delims=" %%A in ('nslookup %printer% 2^>^&1 ') do (
echo %%A | findstr /R /C:"Name: ">nul && set printer=%%A
echo %%A | findstr /R /C:"\*\*\*">nul && (set printerfqdn=%printer% & goto printerip)
)
rem ***************************************
rem * Name: printername.location.companydomain
rem * printerfqdn begins at position 10
rem ***************************************
set printer=%printer:~9%
goto :EOF