Need batch file to turn-off service based on Wi-fi connected

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rishiaknt
Posts: 1
Joined: 11 Oct 2011 00:56

Need batch file to turn-off service based on Wi-fi connected

#1 Post by rishiaknt » 11 Oct 2011 01:03

Hello Mates:

I am a newbie to this forum and need some help. I use my official laptop for both office and personal work. The problem I face is that my office IT guys have installed some application which runs a service (to automatically connect to my office WAN) on my system which i don't want to run when I am at home as the WAN maintains a history of all the sites I visit.

So, I need a batch file which I can place at start-up (or schedule to run at some other event) which will check which wireless network am I connected to (Office Wireless 1 or Office Wireless 2, or Home Wireless 1) and if Home Wireless 1 is connected then turn off the service (Service 1).

Can someone please spend some time and provide me a solution for my problem. The only constraint which I see with this approach is that since it takes my machine some time to connect to any wireless network, placing this batch file at startup may not help. I think it should be scheduled to run when my machine connects to a wireless network. Please help me out here....

Thanks in advance.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Need batch file to turn-off service based on Wi-fi conne

#2 Post by aGerman » 13 Oct 2011 13:19

It's just an idea and I'm not sure whether it will work in your case. For that reason you should execute RASDIAL in a CMD window and have a look at the output:
- if you're NOT connected it should display 2 lines
- if you're connected it should display 3 lines with the connection name in the 2nd line
That's the base for the following code. I tried to write it language independent that's why it's important to know whether you get the same result like me with the test above.

Code: Select all

@echo off

REM Loop while no connection is found
:loop
set /a n=0
for /f %%i in ('rasdial') do set /a n+=1
if %n% lss 3 (>nul ping -n 2 127.0.0.1 &goto loop)

REM Read the connection name.
for /f "skip=1 delims=" %%i in ('rasdial') do if not defined connection set "connection=%%i"

echo %connection%
pause

That code should display nothing while you're not connected but it should display the connection name as soon as you're connected.

In case it would work you could remove the PAUSE command and append the following line to turn off the service:

Code: Select all

if /i "%connection%"=="Home Wireless 1" net stop "Service 1"

Regards
aGerman

Post Reply