FromTheRafters
2021-02-08 04:11:59 UTC
Zaidy & I are working on a Windows 10 network on/off toggle switch
but I'm not a programmer so I'm mixing & matching from what I can find.
Newsgroups: alt.comp.os.windows-10
Subject: Re: A Taskbar shortcut to turn off/on (toggle) the Internet connection?
Date: Mon, 1 Feb 2021 10:51:51 -0500
Message-ID: <rv982m$tc9$***@gioia.aioe.org>
The original goal was to combine these two taskbar shortcuts into one
(OFF) Run as admin to disconnect the Internet connection
%comspec% /c route delete 0.0.0.0 192.168.0.1
(ON) Run as admin to toggle the Internet connection back on
%comspec% /c route add 0.0.0.0 mask 0.0.0.0 192.168.0.1
But we couldn't get a single shortcut to toggle the net on and off.
Based on modifying these sample batch scripts
https://stackoverflow.com/questions/22367173/get-default-gateway-from-batch-file
https://stackoverflow.com/questions/11081735/how-to-use-if-else-structure-in-a-batch-file
We ended up writing this batch script below which toggles the network.
@echo off
;; nettoggle.bat by Zaidy036 20210207 on alt.comp.os.windows-10
;; We still need to get around the need for admin privileges
set defgw=192.168.0.1
set "ip="
for /f "tokens=2,3 delims={,}" %%a in ('"WMIC NICConfig where IPEnabled="True" get DefaultIPGateway /value | find "I" "') do if not defined ip set ip=%%~a
IF "%ip%"=="%defgw%" ( %comspec% /c %windir%\system32\route.exe delete 0.0.0.0 %defgw%) ELSE ( %comspec% /c %windir%\system32\route.exe add 0.0.0.0 mask 0.0.0.0 %defgw%)
exit
Char Jackson suggested this sample file which somehow
avoids the need to run that script as admin.
https://pastebin.com/x5897YCX
That sample file uses this code I think to become admin
:: GetAdmin
:-------------------------------------
:: Verify permissions
if '%errorlevel%' NEQ '0' (
echo Getting administrative privileges...
goto DoUAC
) else ( goto getAdmin )
:DoUAC
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:getAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
But when I add that code to the nettoggle.bat script,
it works if I run the script as admin, but it hangs
at the command prompt if I run it as a user with
admin privileges.
@echo off
;; nettoggle.bat by Zaidy036 20210207 on alt.comp.os.windows-10
;; With an attempt to get admin privileges programmatically
:: GetAdmin
:-------------------------------------
:: Verify permissions
if '%errorlevel%' NEQ '0' (
echo Getting administrative privileges...
goto DoUAC
) else ( goto getAdmin )
:DoUAC
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:getAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
set defgw=192.168.0.1
set "ip="
for /f "tokens=2,3 delims={,}" %%a in ('"WMIC NICConfig where IPEnabled="True" get DefaultIPGateway /value | find "I" "') do if not defined ip set ip=%%~a
IF "%ip%"=="%defgw%" ( %comspec% /c %windir%\system32\route.exe delete 0.0.0.0 %defgw%) ELSE ( %comspec% /c %windir%\system32\route.exe add 0.0.0.0 mask 0.0.0.0 %defgw%)
exit
Can you help us figure out why?
but I'm not a programmer so I'm mixing & matching from what I can find.
Newsgroups: alt.comp.os.windows-10
Subject: Re: A Taskbar shortcut to turn off/on (toggle) the Internet connection?
Date: Mon, 1 Feb 2021 10:51:51 -0500
Message-ID: <rv982m$tc9$***@gioia.aioe.org>
The original goal was to combine these two taskbar shortcuts into one
(OFF) Run as admin to disconnect the Internet connection
%comspec% /c route delete 0.0.0.0 192.168.0.1
(ON) Run as admin to toggle the Internet connection back on
%comspec% /c route add 0.0.0.0 mask 0.0.0.0 192.168.0.1
But we couldn't get a single shortcut to toggle the net on and off.
Based on modifying these sample batch scripts
https://stackoverflow.com/questions/22367173/get-default-gateway-from-batch-file
https://stackoverflow.com/questions/11081735/how-to-use-if-else-structure-in-a-batch-file
We ended up writing this batch script below which toggles the network.
@echo off
;; nettoggle.bat by Zaidy036 20210207 on alt.comp.os.windows-10
;; We still need to get around the need for admin privileges
set defgw=192.168.0.1
set "ip="
for /f "tokens=2,3 delims={,}" %%a in ('"WMIC NICConfig where IPEnabled="True" get DefaultIPGateway /value | find "I" "') do if not defined ip set ip=%%~a
IF "%ip%"=="%defgw%" ( %comspec% /c %windir%\system32\route.exe delete 0.0.0.0 %defgw%) ELSE ( %comspec% /c %windir%\system32\route.exe add 0.0.0.0 mask 0.0.0.0 %defgw%)
exit
Char Jackson suggested this sample file which somehow
avoids the need to run that script as admin.
https://pastebin.com/x5897YCX
That sample file uses this code I think to become admin
:: GetAdmin
:-------------------------------------
:: Verify permissions
nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
:: On Error No Adminif '%errorlevel%' NEQ '0' (
echo Getting administrative privileges...
goto DoUAC
) else ( goto getAdmin )
:DoUAC
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:getAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
But when I add that code to the nettoggle.bat script,
it works if I run the script as admin, but it hangs
at the command prompt if I run it as a user with
admin privileges.
@echo off
;; nettoggle.bat by Zaidy036 20210207 on alt.comp.os.windows-10
;; With an attempt to get admin privileges programmatically
:: GetAdmin
:-------------------------------------
:: Verify permissions
nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
:: On Error No Adminif '%errorlevel%' NEQ '0' (
echo Getting administrative privileges...
goto DoUAC
) else ( goto getAdmin )
:DoUAC
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:getAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
set defgw=192.168.0.1
set "ip="
for /f "tokens=2,3 delims={,}" %%a in ('"WMIC NICConfig where IPEnabled="True" get DefaultIPGateway /value | find "I" "') do if not defined ip set ip=%%~a
IF "%ip%"=="%defgw%" ( %comspec% /c %windir%\system32\route.exe delete 0.0.0.0 %defgw%) ELSE ( %comspec% /c %windir%\system32\route.exe add 0.0.0.0 mask 0.0.0.0 %defgw%)
exit
Can you help us figure out why?