What's new

HOW TO- Windows batch file to log into your router with PuTTY.

  • Thread starter Deleted member 27741
  • Start date
  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

D

Deleted member 27741

Guest
I just find this so useful, if you want to log into your router via PuTTY, you can create a batch file that first browses to the folder with the location of putty.exe, then runs a command. For example, here is the text for my batch file (putty.exe located at d:\putty):

d:
cd putty
putty.exe yourusername@yourserverlocationIusemyddns -pw yourpassword

If you don't know how to make a batch file, paste the above text into notepad and modify it to your settings. Then rename the file from a .txt file to a .bat file (you may need to go into folder properties/options and UNCHECK hide extensions for known file types). Now double click on it to run.

You can also run commands (reboot comes to mind) with this method. Just add -m c:\your\location\textfilewithcommands.txt to the end of the login command in a batch file and save the commands in another file (textfilewithcommands.txt or whatever you name it, obviously).

d:
cd putty
putty.exe yourusername@yourserverlocationIusemyddns -pw yourpassword -m c:\your\location\textfilewithcommands.txt

Note there is no carriage return after password above, it is all on the same line.
 
A bit more elaborate... change the password on Guest Networks with this BAT file (WIFIchange.bat) from the command-line, using:

Code:
WIFIchange.bat newpassword

Code:
:: 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
 
Cool, Akro, I will give this a try. Looks like it would be nice with guests around, I will have to download PLINK.

If there has not been one already I think I would like to start a thread on people's favorite scripts they use on their asus-wrt routers. Has there been a thread like this (here) before?
 

Similar threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top