If you follow my blog you might have seen a similar blog post the other day where I wrote a script to automatically execute an SSH command and SFTP download a file – using Putty Utilities.
https://garrett.dev/2020/12/09/automatic-script-for-ssh-command-and-sftp-download-in-git-bash/
Now I have retooled that same exact script, except this time it is written in Windows Batch Script format.
The script does the following:
- Opens a hard-coded URL in the default web browser and waits for user to press key to continue
- Practical use case would be downloading a file from OneDrive or any service that doesn’t support direct URL download via curl or wget
- Moves the file to the predefined script directory – copies it to a timestamped name of YYYY-MM-DD_filename.extension
- Uploads the file using SFTP to the remote server
- Issues an SSH Command to copy the remote file to a timestamped name like in previosu step
- Issues an SSH Command of your choosing
Overall – this is going to save me loads of time personally with a few tedious tasks and was fun writing it.
@echo off
REM ######################################################
REM #### BEGIN - Do Not Touch - Misc Setup Variables: ####
REM ######################################################
REM SCRIPT_DIRECTORY is defined as the directory that this script currently exists in
SET "SCRIPT_DIRECTORY=%~dp0"
SET "SCRIPT_DIRECTORY=%SCRIPT_DIRECTORY:~0,-1%"
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set VARIABLE_DATETIME=%%a
set VARIABLE_YEAR=%VARIABLE_DATETIME:~0,4%
set VARIABLE_MONTH=%VARIABLE_DATETIME:~4,2%
set VARIABLE_DAY=%VARIABLE_DATETIME:~6,2%
set VARIABLE_HOUR=%VARIABLE_DATETIME:~8,2%
set VARIABLE_MINUTE=%VARIABLE_DATETIME:~10,2%
set VARIABLE_SECOND=%VARIABLE_DATETIME:~12,2%
set VARIABLE_TIMESTAMP=%VARIABLE_YEAR%-%VARIABLE_MONTH%-%VARIABLE_DAY%
REM ####################################################
REM #### END - Do Not Touch - Misc Setup Variables: ####
REM ####################################################
REM #######################################################
REM ### BEGIN - Customized Remote/Local Setup Variables ###
REM #######################################################
REM PLINK and PSFTP Application Paths
SET "PLINK=c:/Program Files/PuTTY/plink.exe"
SET "PSFTP=c:/Program Files/PuTTY/psftp.exe"
REM Setup - Remote Linux Host, Username, and Potential Command to Run
SET "REMOTE_HOSTNAME=hostname.com"
SET "REMOTE_USERNAME=username"
SET "REMOTE_COMMAND=bash -c 'cd /myCoolDirectory/subDirectory && ls -al'"
REM Setup - Private Key for Authentication
SET "LOCAL_PRIVATE_KEY=%SCRIPT_DIRECTORY%\private_key.ppk"
REM Setup - Local File To Transfer - Local Filename Variables including Name, Path, and Timestamp
SET "LOCAL_FILENAME=filename.extension"
SET "LOCAL_DIRECTORY=%SCRIPT_DIRECTORY%\"
SET "LOCAL_PATH=%LOCAL_DIRECTORY%\%LOCAL_FILENAME%"
SET "LOCAL_PATH_TIMESTAMPED=%LOCAL_DIRECTORY%\%VARIABLE_TIMESTAMP%_%LOCAL_FILENAME%"
REM Setup - Final Transfer Path to Remote Server - Remote Directory and Final Filename to Transmit
SET "REMOTE_FILENAME=filename.extension"
SET "REMOTE_DIRECTORY=/myCoolDirectory/subDirectory"
SET "REMOTE_PATH=%REMOTE_DIRECTORY%/%REMOTE_FILENAME%"
SET "REMOTE_PATH_TIMESTAMPED=%REMOTE_DIRECTORY%/%VARIABLE_TIMESTAMP%_%REMOTE_FILENAME%"
REM Setup - Default Download Name from Web Browser - Local Filename That Gets Downloaded From Web Browser
SET "DOWNLOAD_DIRECTORY=%USERPROFILE%\Downloads"
SET "DOWNLOAD_FILENAME=filename.extension"
SET "DOWNLOAD_PATH=%DOWNLOAD_DIRECTORY%\%DOWNLOAD_FILENAME%"
REM #####################################################
REM ### END - Customized Remote/Local Setup Variables ###
REM #####################################################
REM #####################################################
REM ### BEGIN - Script Actions Using Setup Variables ###
REM #####################################################
REM File Download and Staging - Remove Current Copies of Local File
del /f "%DOWNLOAD_PATH%"
del /f "%LOCAL_PATH%"
REM File Download and Staging - Open URL in Chrome for Download
REM
REM If URL contains % paste in Notepad++ - find replace % with %% to properly escape
REM
SET "URL_TO_OPEN=https://sharepoint.com/filename.extension%%tokenThing%%ect"
start "" "%URL_TO_OPEN%"
echo "Press any key when file is finished downloading..."
pause
REM File Download and Staging - Move Downloaded File to Script Directory
move /y "%DOWNLOAD_PATH%" "%LOCAL_PATH%"
copy /B /Y "%LOCAL_PATH%" "%LOCAL_PATH_TIMESTAMPED%"
REM SFTP - Stage SFTP Commands in Temp Script
SET "TEMP_BATCH_FILE=%SCRIPT_DIRECTORY%\psftp.batch"
echo cd %REMOTE_DIRECTORY% > "%TEMP_BATCH_FILE%"
echo put "%LOCAL_FILENAME%" >> "%TEMP_BATCH_FILE%"
REM SFTP - Execute Transfer
"%PSFTP%" -i "%LOCAL_PRIVATE_KEY%" ^
-b "%TEMP_BATCH_FILE%" ^
"%REMOTE_USERNAME%@%REMOTE_HOSTNAME%"
REM SFTP - Remove Temp Script
del /f "%TEMP_BATCH_FILE%"
REM Issue Command - Remote Copy of Transferred File to Timestamp File
SET "REMOTE_TIMESTAMP_COMMAND=cp '%REMOTE_PATH%' '%REMOTE_PATH_TIMESTAMPED%'"
"%PLINK%" -i "%LOCAL_PRIVATE_KEY%" ^
"%REMOTE_USERNAME%@%REMOTE_HOSTNAME%" ^
"%REMOTE_TIMESTAMP_COMMAND%"
REM Issue Command - Execute Misc Command
"%PLINK%" -i "%LOCAL_PRIVATE_KEY%" ^
"%REMOTE_USERNAME%@%REMOTE_HOSTNAME%" ^
"%REMOTE_COMMAND%"
REM ###################################################
REM ### END - Script Actions Using Setup Variables ###
REM ###################################################