Batch Script to SFTP Download a Log File, Filename Timestamp, and Remotely Tail Log at the end

Another morph of the previous days utility script – does the following:

  1. Download the Remote Log File using SFTP
  2. Copy downloaded file to a timestamped filename
  3. Tail the Downloaded Log file at the end
@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"
 
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.log"
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.log"
SET "REMOTE_DIRECTORY=/remote/log/directory"
SET "REMOTE_PATH=%REMOTE_DIRECTORY%/%REMOTE_FILENAME%"
SET "REMOTE_PATH_TIMESTAMPED=%REMOTE_DIRECTORY%/%VARIABLE_TIMESTAMP%_%REMOTE_FILENAME%"

SET "MISC_COMMAND=tail -f -n+1 %REMOTE_DIRECTORY%/%REMOTE_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 "%LOCAL_PATH%"
 
REM SFTP - Stage SFTP Commands in Temp Script
SET "TEMP_BATCH_FILE=%SCRIPT_DIRECTORY%\psftp.batch"   
echo cd %REMOTE_DIRECTORY% > "%TEMP_BATCH_FILE%"
echo get "%REMOTE_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 Copy File to Timestamp Name 
copy /B /Y "%LOCAL_PATH%" "%LOCAL_PATH_TIMESTAMPED%"
 
REM Issue Command - Tail Live Log
"%PLINK%" -i "%LOCAL_PRIVATE_KEY%" ^
            "%REMOTE_USERNAME%@%REMOTE_HOSTNAME%" ^
            "%MISC_COMMAND%"

REM ###################################################
REM ### END - Script Actions Using Setup Variables  ###
REM ###################################################

pause

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s