Skip to content

Instantly share code, notes, and snippets.

@cybergavin
Last active November 15, 2020 02:49
Show Gist options
  • Select an option

  • Save cybergavin/be28addea0bde33f9ec00d685151bc5c to your computer and use it in GitHub Desktop.

Select an option

Save cybergavin/be28addea0bde33f9ec00d685151bc5c to your computer and use it in GitHub Desktop.
SCP files from remote machine - Windows Batch script
@echo OFF
:: ---------------------------------------------------------------------
:: Script : fetchMyfiles.bat
:: Description : SCP files from a remote machine.
:: Requires the OpenSSH client Windows Capability to be installed (Windows Server 2019)
:: ---------------------------------------------------------------------
::
:: Variables
::
set rootdir=D:\myfiles
set sshdir=%rootdir%\ssh
set pvtkey=%sshdir%\id_rsa
:: Set OpenSSH client config to prefer public key authentication
set sshconfig=%sshdir%\sshconfig.txt
set datadir=%rootdir%\data
set logfile=%rootdir%\myfiles.log
set remotehost=abc.contoso.com
set remoteuser=cybergavin
::
:: Main
::
echo %DATE% : %TIME% : INFO : Starting fetchMyfiles.bat >> %logfile%
scp.exe -i %pvtkey% -F %sshconfig% %remoteuser%@%remotehost%:/Inbox/myfiles* %datadir% >> %logfile%
if %ERRORLEVEL% equ 0 (
echo %DATE% : %TIME% : INFO : Fetched myfiles >> %logfile%
) else (
echo %DATE% : %TIME% : ERROR : Failed to fetch myfiles >> %logfile%
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment