Jump to content


Photo

Toggle WiFi on wired connection


  • Please log in to reply
13 replies to this topic

#1 Elderon

Elderon
  • Members
  • 36 posts

Posted 17 August 2012 - 05:34 AM

Long time user, first time poster.
I wanted a script that when I plugged my laptop into a wired network it would disable my wireless card and when I unplugged my laptop from a wired network it would re-enable my wireless card. I found many solutions that either were not free or did not work exactly how I wanted it to. I am open to suggestions and if you use my code and tweak it please post what you did and why. Other than that feel free to do with it as you please. Also, if you have any questions as to how or why I did something feel free to ask.


EDIT: Updated the script to use the Registry instead of the clipboard.
EDIT: Fixed Registry to overwrite old values if they exist.
EDIT: Added a ZIP download that includes icons.


; Script created by ELDERON
; Last modified  9/5/12
; Tested on AHK_L
; Tested on English Windows 7 Ultimate x64
; Online Repo:
; 				http://www.autohotkey.com/community/viewtopic.php?f=2&t=90229
; Instructions:
;				Name your Wired Connection to LAN
;				Name your Wireless Connection to WIFI
;				Configure this script to run when you login
;				ex. Save as C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\ToggleWiFi.ahk
;				ex. Add a registry entry under HKU\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Run

Menu, Tray, Icon, %A_ScriptDir%\Assets\NOWIFI.ico,,1
;STARTOFSCRIPT - Auto reload script if changed
SetTimer,UPDATEDSCRIPT,1000 

; Checks every 10 seconds for change in LAN adapter status
Loop{
Menu, Tray, Tip, Checking LAN Connection! Tries: %A_Index% ; For Debug - Remove this line
Sleep, 10000
TOGGLEWIFI()
}


;FUNCTION
TOGGLEWIFI()
{
; Deletes old Interface information
RegDelete, HKLM, Software\AHKSCRIPTS\ToggleWiFi

; Collects interface information and saves to the registry under HKLM\Software\AHKSCRIPTS\ToggleWIFI
RunWait, %comspec% /c for /f "delims=" `%i in ('netsh interface show interface') do (if NOT "`%i"=="" (if NOT "`%i"=="Admin State    State          Type             Interface Name" (if NOT "`%i"=="-------------------------------------------------------------------------" (reg add HKLM\Software\AHKSCRIPTS\ToggleWIFI /t REG_SZ /v "`%i" /d "`%i" /f)))), , hide

; Reads all interfaces and saves them to variable INTSTAT
Loop, HKLM, Software\AHKSCRIPTS\ToggleWIFI, 0, 0
{
	RegRead, value
	if ErrorLevel
		value = *error*
	INTSTAT .= value . "-"
    ;MsgBox, 4, , %value%`n`nContinue?
    ;IfMsgBox, NO, break
}

; Removes beginning and ending dashes
INTSTAT := Trim(INTSTAT,"-")

; Replaces all double spaces with a single space
Loop 
{ 
    StringReplace, INTSTAT, INTSTAT, %A_Space%%A_Space%, %A_Space%, UseErrorLevel 
    if ErrorLevel = 0  ; No more replacements needed. 
        break 
}

; Splits interfaces by a dash
StringSplit, INTERFACE, INTSTAT, -, , All

; Loops through each found interface until it finds LAN
Loop, %INTERFACE0%
{
	INTNAME := INTERFACE%a_index%
	if (InStr(INTNAME, "LAN")){
		; If this is the LAN adapter split it into it's settings
		StringSplit, LAN, INTERFACE%a_index%, %A_SPACE%
		; Double check that this is the LAN adapter
		if (LAN4 = "LAN") {
			; Make sure it is enabled, if not this script is useless
			if (LAN1 = "Enabled") {
				; When an Ethernet cable is plugged in LAN2 should equal Connected
				if (LAN2 = "Connected"){
					; Loops through each found interface again until it finds WIFI
					Loop, %INTERFACE0%
					{
						INTNAME := INTERFACE%a_index%
						if (InStr(INTNAME, "WIFI")){
							; If this is the WIFI adapter split it into it's settings
							StringSplit, WIFI, INTERFACE%a_index%, %A_SPACE%
							; Double check that this is the WIFI adapter
							if (WIFI4 = "WIFI") {
								; If it is already disabled don't do anything
								if (WIFI1 = "Enabled") {
									; Disables the WIFI adapter
									runwait, %comspec% /c netsh interface set interface name="WIFI" admin=DISABLED, , hide
									Menu, Tray, Icon, %A_ScriptDir%\Assets\NOWiFi.ico,,1
									;MsgBox, WIFI should be DISABLED
								}
							}
						}
					}
					; If there is no Ethernet cable connected this LAN2 should equal Disconnectd
				}else if (LAN2 = "Disconnected"){
				; Loop through each found interface again until it finds WIFI
					Loop, %INTERFACE0%
					{
						INTNAME := INTERFACE%a_index%
						if (InStr(INTNAME, "WIFI")){
							; If this is the WIFI adapter split it into it's settings
							StringSplit, WIFI, INTERFACE%a_index%, %A_SPACE%
							; Double check that this is the WIFI adapter
							if (WIFI4 = "WIFI") {
								; If it is already enabled don't do anything
								if (WIFI1 = "Disabled") {
									; Enables the WIFI adapter
									runwait, %comspec% /c netsh interface set interface name="WIFI" admin=ENABLED, , hide
									Menu, Tray, Icon, %A_ScriptDir%\Assets\WiFi.ico,,1
									;MsgBox, WIFI should be ENABLED
								}
							}
						}
					}
					; If LAN is not found error out here
				}else{
					MsgBox, Something is wrong!`n*****Interface List*****`n%TRIM1%`n*****EOF*****`nMake sure your wired connection is named LAN and your wireless connection is named WIFI!
				}
			}
		}
	}
}
return
}

UPDATEDSCRIPT: 
FileGetAttrib,attribs,%A_ScriptFullPath% 
IfInString,attribs,A 
{ 
FileSetAttrib,-A,%A_ScriptFullPath% 
SplashTextOn,,,Updated script, 
Sleep,500 
Reload 
} 
Return 
;ENDOFSCRIPT - Auto reload script if changed


#2 nak1017

nak1017
  • Members
  • 2 posts

Posted 06 September 2012 - 12:25 AM

I'm been trying to learn AutoHotKey to do this exact task!
I'm stuck at work, but I'm definitely going to try this when I get home

#3 Elderon

Elderon
  • Members
  • 36 posts

Posted 06 September 2012 - 12:41 AM

Let me know if you run into any issues, I have been fixing them as I encounter them but that is just one system. I am surprised there was not already a way to do this, come to think of it I think Windows should have this built in!

#4 nak1017

nak1017
  • Members
  • 2 posts

Posted 06 September 2012 - 12:55 AM

It's ironic you say that M$ should have baked it in as I had some friends from M$ over on Labor day and not a one of them could figure out how to do it on my home laptop even though they swore up and down it should pick the best connection automatically, which is wired where I had the laptop setup.
I'm inclined to believe them though since my work laptop has no problems and will do it automatically, but I can't figure out what Boeing did to the damn thing to get it to work...

Anyway, I'll tell you how it works out

#5 Elderon

Elderon
  • Members
  • 36 posts

Posted 06 September 2012 - 12:58 AM

I've never really checked to see which adapter Windows was using; the way I look at it is I don't want to take a chance, if I'm hard-wired I don't need WiFi.

#6 csmith665

csmith665
  • Members
  • 8 posts

Posted 06 September 2012 - 05:45 AM

Thank you. This script will be helpful.

#7 subjectnamehere

subjectnamehere
  • Members
  • 3 posts

Posted 10 September 2012 - 02:04 PM

I tried using this script, but I am having a couple issues.

When you say to add a registry entry, what exactly do you mean? I found the location in the registry editor, but I am unsure of what to do next.

The other issue is that when I plug my ethernet cord into the computer, it still uses the wireless connection even though it is connected to both networks. I am guessing that this has something to do with the registry problem, but I figured I should mention it.

Thanks for writing this script and any help you can give.

#8 Elderon

Elderon
  • Members
  • 36 posts

Posted 10 September 2012 - 05:49 PM

There are multiple ways of making a program run when you start your computer, under the instructions section I listed 2.

; Configure this script to run when you login
; ex. Save as C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\ToggleWiFi.ahk
; ex. Add a registry entry under HKU\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Run

So you can either save it to C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\ToggleWiFi.ahk
OR
Create a registry entry at HKU\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Run
Create a new string value and name it whatever you want and for the data set it to the path of ToggleWifi.ahk in quotes
EX) ToggleWifi = "C:\Users\Elderon\Scripts\AHKScripts\Finished\ToggleWiFi.ahk"

As for your issue with it not disabling your WiFi card, make sure you have your adapters named LAN and WIFI

#9 subjectnamehere

subjectnamehere
  • Members
  • 3 posts

Posted 10 September 2012 - 08:58 PM

Thanks a lot. It turns out I renamed a network to wifi, not the adapter. It seems to be working now, although my computer says I am still connected to both networks.

#10 Elderon

Elderon
  • Members
  • 36 posts

Posted 10 September 2012 - 09:19 PM

If you look at your adapter it should be disabled, if not there is another issue. Let me know if the adapter itself is disabled.

#11 subjectnamehere

subjectnamehere
  • Members
  • 3 posts

Posted 11 September 2012 - 01:06 AM

The adapter isn't disabled. If it matters, I am on a college network. Sometimes the network tells the computer that the type of connection is different than it really is.

#12 Elderon

Elderon
  • Members
  • 36 posts

Posted 11 September 2012 - 01:44 AM

Can you take a screen shot of HKLM\Software\AHKSCRIPTS\ToggleWIFI and post it? It shouldn't matter what network you are connected to, this is directly with your laptop. Do you have UAC enabled?

#13 WifiToggle

WifiToggle
  • Guests

Posted 14 September 2012 - 12:07 AM

This script is exactly what I need. Multiple times thru-out the day for work reasons I am connecting via wired lan then back to wireless.

I haven't tested it yet but I looked over the script and I want to ask a request.

Instead of the user renaming their wired and wireless connections, can you add something like this to the script.

Lan = 
WiFi =

Then change the rest of your code to use the variables?

I am afraid of trying it and breaking the script.

thanks

#14 Elderon

Elderon
  • Members
  • 36 posts

Posted 14 September 2012 - 04:04 AM

Sorry, that will not work in the current setup. I use the space as a delimiter in StringSplit commands and if your adapter has a space (Local Area Network) the script will fail. It is easier to just change the names and as far as I can think there is no reason that changing the names should break anything else. If you could come up with a solution to circumvent this I will be more than happy to add it to the published script!