chivas01

Auto Disable Wireless when ethernet detected

13 posts in this topic

I'm trying to put together a script that will automatically disable the wireless card when an ethernet connected is made and then re enable the wireless when the ethernet is disconnected.

Any help would be appreciated.

Share this post


Link to post
Share on other sites



You might want to start here.

Thank you for the response. However, I have already looked into the devcon utility and i am able to write a script that will disable the wireless and enable via command line and/or app script. What i'm looking for (and unable to do) is a way to have a script detect whether a lan connection is present or not...meaning, detect whether the ethernet cable is plugged or unplugged and be able to return a TRUE or FALSE. I have been trying to find how windows xp detirmines this, but using devcon and ipconfig, the ethernet adapter is always coming back active. There are many applications available that do this task, but i'm trying to find the code or script that detects it and from there will disable or enable the wireless.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I know this is quite an old topic to resurrect, but I recently found myself looking for the same information. I've since completed a purely AutoIT script that does the job for about 20 laptops, and thought I'd share. Hopefully this will make it easier for the next person..

Assumes there are only two adapters, the LAN and Wireless...

#include <File.au3>
#NoTrayIcon

While 1
   $IP = @IPAddress1

    If $IP = "127.0.0.1" Then
        Run('c:\WINDOWS\System32\cmd.exe' & ' /c sc query WZCSVC | find "STATE" > C:\state.txt', "", @SW_HIDE)
        Sleep( 5000 )
            $file = "c:\state.txt"
            FileOpen($file, 0)
            $line = FileReadLine($file, 1)
            $state = StringTrimLeft(StringTrimRight($line, 1), 32)
            If $state <> "RUNNING" Then
                Run('c:\WINDOWS\System32\cmd.exe' & ' /c net start WZCSVC', "", @SW_HIDE)
            Else
                Sleep( 10000 )
            EndIf
    Else
        Run('c:\WINDOWS\System32\cmd.exe' & ' /c sc query WZCSVC | find "STATE" > C:\state.txt', "", @SW_HIDE)
        Sleep( 5000 )
            $file = "c:\state.txt"
            FileOpen($file, 0)
            $line = FileReadLine($file, 1)
            $state = StringTrimLeft(StringTrimRight($line, 1), 32)
                If $state = "RUNNING" Then
                Run('c:\WINDOWS\System32\cmd.exe' & ' /c net stop WZCSVC', "", @SW_HIDE)
                Else
                Sleep( 10000 )
                EndIf
    EndIf
WEnd
Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Here is a function I used in a login script to detect if the wireless card is being used via WMI, much cleaner way to do it:

Func NetStatus($devid = 12)
    $objWMI = ObjGet("winmgmts:\\localhost\root\cimv2")
    $ifaces = $objWMI.ExecQuery("select * from win32_NetworkAdapter, "WQL", 0x10 + 0x20)
    For $iface in $ifaces
        If $iface.DeviceID == $devid Then Return $iface.NetConnectionStatus
    Next
EndFunc

I use the default value of 12 because that's the deviceid of my wireless card, but you can obviously pass it as a parameter yourself for your deviceid.

Share this post


Link to post
Share on other sites

You can use a wmi query to return the ethernet adapters with a status value of connected from win32_networkadapters.

Share this post


Link to post
Share on other sites

I have my own way to do this:

#include <File.au3>
#include <Array.au3>

#NoTrayIcon

while 1

    runwait(@ComSpec & ' /c for /F "tokens=1-10" %A in (''"netsh interface show interface | find /i "local""'') DO (echo %B %C %D %E %F %G) >> c:\temp\network.txt',"",@SW_HIDE)
    runwait(@ComSpec & ' /c for /F "tokens=1-10" %A in (''"netsh interface show interface | find /i "wireless""'') DO (echo %B %C %D %E %F %G) >> c:\temp\network.txt',"",@SW_HIDE)

    local $Network[10]
     _FileReadToArray("c:\temp\network.txt",$Network)
    $x = 1
    $y = 1
    Do
        if StringInStr($Network[$x],"Local") > 0 and StringInStr($Network[$x],"Connected",1) > 0 then ;if LAN is connected disable all wifi
            Do
                if StringInStr($Network[$y],"Wireless") > 0 and StringInStr($Network[$y],"Connected",1) > 0 Then ; if wireless and connected
                    $Wireless_Name = StringReplace($Network[$y],"Connected Dedicated ","")
                    runwait(@ComSpec & ' /c netsh interface set interface name="'&$Wireless_Name&'" disable',"",@SW_HIDE)
                EndIf
                $y += 1
            Until $y = $Network[0]+1
        EndIf
        if StringInStr($Network[$x],"Local") > 0 and StringInStr($Network[$x],"Disconnected",1) > 0 then
            Do
                if StringInStr($Network[$y],"Wireless") > 0 and StringInStr($Network[$y],"Disconnected",1) > 0 Then ; if wireless and disconnected
                    $Wireless_Name = StringReplace($Network[$y],"Disconnected Dedicated ","")
                    runwait(@ComSpec & ' /c netsh interface set interface name="'&$Wireless_Name&'" enable',"",@SW_HIDE)
                EndIf
                $y += 1
            Until $y = $Network[0]+1
        EndIf
        $x +=1
    Until $x = $Network[0]+1
    runwait(@ComSpec & ' /c del c:\temp\network.txt /f',"",@SW_HIDE)
    sleep(2000)
WEnd
1 person likes this

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

 

I have my own way to do this:

#include <File.au3>
#include <Array.au3>

#NoTrayIcon

while 1

    runwait(@ComSpec & ' /c for /F "tokens=1-10" %A in (''"netsh interface show interface | find /i "local""'') DO (echo %B %C %D %E %F %G) >> c:\temp\network.txt',"",@SW_HIDE)
    runwait(@ComSpec & ' /c for /F "tokens=1-10" %A in (''"netsh interface show interface | find /i "wireless""'') DO (echo %B %C %D %E %F %G) >> c:\temp\network.txt',"",@SW_HIDE)

    local $Network[10]
     _FileReadToArray("c:\temp\network.txt",$Network)
    $x = 1
    $y = 1
    Do
        if StringInStr($Network[$x],"Local") > 0 and StringInStr($Network[$x],"Connected",1) > 0 then ;if LAN is connected disable all wifi
            Do
                if StringInStr($Network[$y],"Wireless") > 0 and StringInStr($Network[$y],"Connected",1) > 0 Then ; if wireless and connected
                    $Wireless_Name = StringReplace($Network[$y],"Connected Dedicated ","")
                    runwait(@ComSpec & ' /c netsh interface set interface name="'&$Wireless_Name&'" disable',"",@SW_HIDE)
                EndIf
                $y += 1
            Until $y = $Network[0]+1
        EndIf
        if StringInStr($Network[$x],"Local") > 0 and StringInStr($Network[$x],"Disconnected",1) > 0 then
            Do
                if StringInStr($Network[$y],"Wireless") > 0 and StringInStr($Network[$y],"Disconnected",1) > 0 Then ; if wireless and disconnected
                    $Wireless_Name = StringReplace($Network[$y],"Disconnected Dedicated ","")
                    runwait(@ComSpec & ' /c netsh interface set interface name="'&$Wireless_Name&'" enable',"",@SW_HIDE)
                EndIf
                $y += 1
            Until $y = $Network[0]+1
        EndIf
        $x +=1
    Until $x = $Network[0]+1
    runwait(@ComSpec & ' /c del c:\temp\network.txt /f',"",@SW_HIDE)
    sleep(2000)
WEnd

Iam getting error as

(22) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

if StringInStr($Network[$y],"Wireless") > 0 and StringInStr($Network[$y],"Connected",1) > 0 Then
if StringInStr(^ ERROR
Edited by Cyborg5000

Share this post


Link to post
Share on other sites

netsh results are localized. Change searched strings for those your netsh returns.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

netsh results are localized. Change searched strings for those your netsh returns.

Iam using this with the help of above script

 

RunWait (@ComSpec & ' /c for /F "tokens=1-10" %A in (''"netsh interface show interface | find /i "Connected""'') DO (echo %B %C %D %E %F %G) >> c:tempnetwork.txt',"")
    local $Network[10]
     _FileReadToArray("c:tempnetwork.txt",$Network)
    $x = 1
    $y = 1
    Do
        if StringInStr($Network[$x],"Local") > 0 and StringInStr($Network[$x],"Connected",1) > 0 then ;if LAN is connected disable all wifi
            Do
                if StringInStr($Network[$y],"Wireless") > 0 and StringInStr($Network[$y],"Connected",1) > 0 Then ; if wireless and connected
                    $Wireless_Name = StringReplace($Network[$y],"Connected Dedicated ","")
                    RunWait (@ComSpec & ' /c netsh interface set interface "Wireless Network Connection" admin=disable',"")
                EndIf
                $y += 1
            Until $y = $Network[0]+1
        EndIf

Share this post


Link to post
Share on other sites

In a cmd shell, what does "netsh interface show interface" display?


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

In a cmd shell, what does "netsh interface show interface" display?

C:WindowsSystem32>netsh interface show interface
 
Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Disabled       Disconnected   Dedicated        Cisco AnyConnect Secure Mobility
Client Connection
Disabled       Disconnected   Dedicated        Wireless Network Connection
Enabled        Connected      Dedicated        Local Area Connection

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now