Recently connected wireless networks

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Recently connected wireless networks

#1 Post by Boombox » 18 Oct 2012 06:04

Hi all...

Just need a little help optimizing some code if possible(?) Thanks.
-------------------------------------------------------------------------

Code: Select all

@echo off
echo.
echo.
echo   Details of locally stored networks...
echo.

if exist %userprofile%\desktop\%username%.txt del %userprofile%\desktop\%username%.txt

for /f "tokens=5-7 skip=4" %%g in ('netsh wlan show profiles') do (
netsh wlan show profiles name="%%g %%h %%i" key= clear | findstr "name Authentication key Content">>%userprofile%\desktop\%username%.txt)

for /f "tokens=5-7 skip=4" %%g in ('netsh wlan show profiles') do (
netsh wlan show profiles name="%%g %%h" key= clear | findstr "name Authentication key Content">>%userprofile%\desktop\%username%.txt)

for /f "tokens=5-7 skip=4" %%g in ('netsh wlan show profiles') do (
netsh wlan show profiles name="%%g" key= clear | findstr "name Authentication key Content">>%userprofile%\desktop\%username%.txt)

type %userprofile%\desktop\%username%.txt

echo.
echo   Saved to %userprofile%\desktop\%username%.txt...Keep y/n ?

set /p ch=
if %ch%==y exit
if %ch%==n del %userprofile%\desktop\%username%.txt
exit


Basically, how do I allow for multiple words in the SID without having to run the same command for each possible amount?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Recently connected wireless networks

#2 Post by foxidrive » 18 Oct 2012 06:36

You'll need to provide some data from the command and describe the task a little better.

FWIW my machine displays this (no WLAN):

c:\>netsh wlan show profiles
The Wireless AutoConfig Service (wlansvc) is not running.

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Recently connected wireless networks

#3 Post by Boombox » 18 Oct 2012 06:52

Thanks for the swift reply...

Netsh wlan show profiles (entered into the cmd prompt)

Will list the names of the wireless networks of which I have saved details of e.g

User Profiles
-------------

Dlink125
Black Swan
Student Bar Staffordshire

I then send each of the relevant lines through a FOR loop and capture the data in %username%.txt

Code: Select all

for /f "tokens=5-7 skip=4" %%g in ('netsh wlan show profiles') do (
netsh wlan show profiles name="%%g %%h %%i" key= clear | findstr "name Authentication key Content">>%userprofile%\desktop\%username%.txt)



Code: Select all

netsh wlan show profiles name="Student Bar Staffordshire" key= clear


....Will display all information held about this connection, including the Key/Password, FINDSTR is used to retrieve just the line with key.

My problem is dealing with the spaces in the SID.

%%g displays SID's with one word names
%%g %%h does two word SID's
%%g %%h %%i for 3 words....

How can I configure this for any number of words used. (Shit this was hard to explain!)

Thankyou

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Recently connected wireless networks

#4 Post by foxidrive » 18 Oct 2012 06:57

I'd need to see the data to figure out how to mess with it. Alter the passwords...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Recently connected wireless networks

#5 Post by foxidrive » 18 Oct 2012 07:01

It occurs to me that the wireless network name is part of the issue - use this to get it into one token:

Code: Select all

for /f "skip=4 delims=" %%g in ('netsh wlan show profiles') do (
netsh wlan show profiles name="%%g" key= clear | findstr "name Authentication key Content">>%userprofile%\desktop\%username%.txt)

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Recently connected wireless networks

#6 Post by Boombox » 18 Oct 2012 07:06

This is the output from my batch,

SSID name : "The Black Swan"
Authentication : WPA2-Personal
Security key : Present
Key Content : scoobydoo
SSID name : "Dlink"
Authentication : Open
Security key : Absent
SSID name : "SKYF84AB"
Authentication : WPA2-Personal
Security key : Present
Key Content : OAERFDWP
SSID name : "virginmedia9005887"
Authentication : WPA2-Personal
Security key : Present
Key Content : hsqbmmeq

The SID's with 3 words are captured first... (%%g %%h %%i)

Then any with 2... Then single word SID's.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Recently connected wireless networks

#7 Post by foxidrive » 18 Oct 2012 08:00

Try the code in the post above yours. Does that help?

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Recently connected wireless networks

#8 Post by Boombox » 18 Oct 2012 08:45

No, unfortunately not Foxi, I've been there before :(

I need to keep the spaces between each word of the SSID name (for obvious reasons)

This is the ACTUAL output of the NETSH WLAN SHOW PROFILES command...
---------------------------------------------------------------------------------------

Profiles on interface Wireless Network Connection:

Group policy profiles (read only)
---------------------------------
<None>

User profiles
-------------
All User Profile : The Black Swan
All User Profile : Dlink
All User Profile : SKYF84AB
All User Profile : virginmedia9005887


So as you can see, after skipping a few lines,
I'll need to specify tokens 5 onwards, (depending on SSID length)

Store each in %%g or whathaveyou, and insert the variable into

NETSH WLAN SHOW PROFILE NAME=" " KEY=CLEAR

But how do I determine what to put here ^

%%g is not enough for 'The Black Swan', I will need %%g %%h %%i

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Recently connected wireless networks

#9 Post by foxidrive » 18 Oct 2012 08:55

It helps when you post the actual details. :roll:

Try this - adjust the skip to suit you

Code: Select all

for /f "skip=4 tokens=3,* delims=: " %%g in ('netsh wlan show profiles') do (
netsh wlan show profiles name="%%h" key= clear | findstr "name Authentication key Content">>%userprofile%\desktop\%username%.txt)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Recently connected wireless networks

#10 Post by foxidrive » 18 Oct 2012 09:00

Here's a method if you only want the All User Profile : items, where skip isn't needed.


Code: Select all

for /f "tokens=3,* delims=: " %%g in ('netsh wlan show profiles ^|find /i "All User Profile : "') do (
netsh wlan show profiles name="%%h" key= clear | findstr "name Authentication key Content">>%userprofile%\desktop\%username%.txt)

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Recently connected wireless networks

#11 Post by Boombox » 18 Oct 2012 09:20

Yeah, sorry bout the details earlier, or lack thereof...

But 2 things, please.

In the delims= section, are there 2 characters, a colon and a space?

And why the shift from %%g to %%h?

Thanks Foxi and sorry again...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Recently connected wireless networks

#12 Post by foxidrive » 18 Oct 2012 09:28

The delims are set to separate the line into tokens and : and space are the separators.

The tokens are set to take the third token and the rest of the line. So you will get "Profile" in %%g and in %%h will be the rest of the line.

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Recently connected wireless networks

#13 Post by Boombox » 18 Oct 2012 10:02

So the entire SSID is contained within %%h ?

I guess I still don't fully understand the delims. I thought they were characters to be ignored...

I thought that, if I'm splitting the string using spaces for example,

I'd need to assign a variable to each individual word...?


Either way, I'm very grateful Foxi, I'll get it eventually. Thank you.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Recently connected wireless networks

#14 Post by foxidrive » 18 Oct 2012 10:17

You're welcome.

Try this batch file - the A is not case sensitive, I just used upper case to illustrate it. You will see that it outputs: one-two-three

Code: Select all

@echo off
for /f "tokens=1,2,3 delims=A" %%a in ("oneAtwoAthreeA") do echo %%a-%%b-%%c


Hopefully the example makes it clearer.

This will output: one-two-threeA

Code: Select all

@echo off
for /f "tokens=1,2,* delims=A" %%a in ("oneAtwoAthreeA") do echo %%a-%%b-%%c

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Recently connected wireless networks

#15 Post by Boombox » 18 Oct 2012 13:26

Well, I can see that the only difference between the two samples, is the asterix... :oops:


But what has the asterix changed to include the final A?

Is it showing all other tokens past 1 & 2, but not involving the delimiters in that data?


Thanks Foxi.

Post Reply