:: WIFIchange.bat
@echo off
cls
::
:: Set Router Information
:: you can use password login or keypair
::
set rHOST=192.168.1.1
set rPORT=22
set rUSER=admin
set rKEY=keypriv.ppk
::set rPASS=youradminpassword
::
:: Set WiFi Network Information as found in NVRAM
::
set rGUEST24net1=wl0.1_wpa_psk
set rGUEST24net2=wl0.2_wpa_psk
set rGUEST24net3=wl0.3_wpa_psk
set rGUEST50net1=wl1.1_wpa_psk
set rGUEST50net2=wl1.2_wpa_psk
set rGUEST50net3=wl1.3_wpa_psk
::
:: Set a LOGFILE and ERRMSG
::
set LOGFILE=WIFIchange.txt
set ERRMSG=
::
:: Go to PLINK location
::
D:
CD D:\Programs\router
::
:: Set variables for new passwords (get the %1 argument and use it for all networks)
::
set newpass=%1
::
:: Start Logging
::
@echo. >> %LOGFILE% 2>&1
@echo ******************************************************* >> %LOGFILE% 2>&1
@echo -- BAT BEG ----------------- %date%_%time:~0,2%.%time:~3,2%.%time:~6,2% >> %LOGFILE% 2>&1
::
:: Check password variable to ensure it is Ok
::
IF [%newpass%] NEQ [] @echo Password is not blank
IF [%newpass%] == [] set ERRMSG=ERROR:Password is BLANK
IF [%newpass%] == [] goto ERRORPROCESS
IF "%newpass:~7,1%"=="" (
set ERRMSG=ERROR:Password less than 8 characters
goto ERRORPROCESS
) ELSE (
@echo Password is at least 8 characters
)
:: check for illegal characters... add more of these if it suits you
set PAT="^:"
echo.%newpass% | findstr /C:"%PAT%" 1>nul
if errorlevel 1 (
@echo Password does not Contain 'colon'
) ELSE (
set ERRMSG=%ERRMSG% ERROR:Password contains 'colon'
)
set errorlevel=
::
:: If there is a problem with the given password, %ERRMSG% will contain a value, and we abandon the script
::
IF "%ERRMSG%" NEQ "" GOTO ERRORPROCESS
::
:: If password variable is Ok, we continue...
::
::
:: Set variables for new passwords (get the %1 argument and use it for all networks)
::
set pw24_1=%newpass%
set pw24_2=%newpass%
set pw24_3=%newpass%
set pw50_1=%newpass%
set pw50_2=%newpass%
set pw50_3=%newpass%
::
:: Set up Commands
::
set cShowNets="nvram show | grep wl..._wpa_psk | sort | sed 's/wl/\r\nwl/g'"
set cCommitNVRAM="nvram commit"
set cRESTARTwifi="service restart_wireless"
set cREBOOT="reboot"
set cSetNet24_1="nvram set %rGUEST24net1%=%pw24_1%"
set cSetNet24_2="nvram set %rGUEST24net2%=%pw24_2%"
set cSetNet24_3="nvram set %rGUEST24net3%=%pw24_3%"
set cSetNet50_1="nvram set %rGUEST50net1%=%pw50_1%"
set cSetNet50_2="nvram set %rGUEST50net2%=%pw50_2%"
set cSetNet50_3="nvram set %rGUEST50net3%=%pw50_3%"
::
:: Show us what we are about to do
::
@echo rHOST: %rHOST%
@echo rPORT: %rPORT%
@echo pw24_1: %pw24_1%
@echo pw50_1: %pw50_1%
::
:: Run COMMANDS through PLINK
::
@echo on
:: If we get this far, it's time to run the commands to the Router
@echo -- ROUTER CHANGE BEG ------- %date%_%time:~0,2%.%time:~3,2%.%time:~6,2% >> %LOGFILE% 2>&1
@echo rHOST: %rHOST%>> %LOGFILE% 2>&1
@echo rPORT: %rPORT%>> %LOGFILE% 2>&1
@echo net24_1: %rGUEST24net1%>> %LOGFILE% 2>&1
@echo net50_1: %rGUEST50net1%>> %LOGFILE% 2>&1
@echo pwd24_1: %pw24_1%>> %LOGFILE% 2>&1
@echo pwd50_1: %pw50_1%>> %LOGFILE% 2>&1
@echo.>> %LOGFILE% 2>&1
:: Show CURRENT Passwords
@echo ----- Guest Networks CURRENT ...>> %LOGFILE% 2>&1
::plink.exe -ssh %rHOST% -P %rPORT% -l %rUSER% -pw %rPASS% -batch %cShowNets% >> %LOGFILE% 2>&1
plink.exe -ssh %rUSER%@%rHOST% -P %rPORT% -i %rKEY% -batch %cShowNets% >> %LOGFILE% 2>&1
@echo.>> %LOGFILE% 2>&1
@echo.>> %LOGFILE% 2>&1
:: Set the Passwords for the desired Networks and Commit NVRAM
@echo ----- SETTING NEW PASSWORDS ...>> %LOGFILE% 2>&1
:: this line currently sets GuestNetwork #1 for both 2.4GHz and 5.0GHz bands and commits them to NVRAM
:: add more Networks if so desired (i.e. cSetNet24_2), and ensure the cCommitNVRAM command is retained
::plink.exe -ssh %rHOST% -P %rPORT% -l %rUSER% -pw %rPASS% -batch %cSetNet24_1% %cSetNet50_1% %cCommitNVRAM% >> %LOGFILE% 2>&1
plink.exe -ssh %rUSER%@%rHOST% -P %rPORT% -i %rKEY% -batch %cSetNet24_1% %cSetNet50_1% %cCommitNVRAM% >> %LOGFILE% 2>&1
@echo.>> %LOGFILE% 2>&1
:: Show UPDATED Passwords
@echo ----- Guest Networks UPDATED ...>> %LOGFILE% 2>&1
::plink.exe -ssh %rHOST% -P %rPORT% -l %rUSER% -pw %rPASS% -batch %cShowNets% >> %LOGFILE% 2>&1
plink.exe -ssh %rUSER%@%rHOST% -P %rPORT% -i %rKEY% -batch %cShowNets% >> %LOGFILE% 2>&1
@echo.>> %LOGFILE% 2>&1
@echo.>> %LOGFILE% 2>&1
:: Restart the WIFI Service for new passwords to take effect
@echo ----- RESTARTING WIFI SERVICE ...>> %LOGFILE% 2>&1
::plink.exe -ssh %rHOST% -P %rPORT% -l %rUSER% -pw %rPASS% -batch %cRESTARTwifi% >> %LOGFILE% 2>&1
plink.exe -ssh %rUSER%@%rHOST% -P %rPORT% -i %rKEY% -batch %cRESTARTwifi% >> %LOGFILE% 2>&1
@echo.>> %LOGFILE% 2>&1
:: End Router Changes
@echo -- ROUTER CHANGE END ------- %date%_%time:~0,2%.%time:~3,2%.%time:~6,2% >> %LOGFILE% 2>&1
@echo off
::
:: We are almost done. Test for Errors.
::
IF [%ERRMSG%] == [] GOTO finished
:ERRORPROCESS
@echo %ERRMSG%
@echo %ERRMSG% >> %LOGFILE% 2>&1
@echo -- BAT END ----------------- %date%_%time:~0,2%.%time:~3,2%.%time:~6,2% >> %LOGFILE% 2>&1
:: Open the LOGFILE
@notepad %LOGFILE%
goto endoffile
:finished
@echo -- BAT END ----------------- %date%_%time:~0,2%.%time:~3,2%.%time:~6,2% >> %LOGFILE% 2>&1
:endoffile
::
:: We are done. Uncomment the following lines if you wish.
::
::@notepad %LOGFILE%
::timeout 2
::pause