1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

バッチファイルでサーバーのファイルを取得する

Posted at
@echo off
setlocal

REM full path for teraterm macro
set TTMACRO_PATH="C:\Program Files (x86)\teraterm5\ttpmacro.exe"

REM full path for winscp
set WINSCP_PATH="C:\Program Files (x86)\WinSCP\WinSCP.com"

REM server address, account
set HOSTNAME=127.0.0.1
set PORT=22
set USERNAME=root
set PASSWORD=root

REM execute command on server
set COMMAND="echo 'abc' > /home/root/file_1234"


REM create output directory
setlocal enabledelayedexpansion
for /f "tokens=2 delims==" %%i in ('"wmic os get localdatetime /value"') do set datetime=%%in
set YEAR=!datetime:~0,4!
set MONTH=!datetime:~4,2!
set DAY=!datetime:~6,2!
set HOUR=!datetime:~8,2!
set MINUTE=!datetime:~10,2!
set SECOND=!datetime:~12,2!
set FORMAT_DATETIME=%YEAR%%MONTH%%DAY%_%HOUR%%MINUTE%%SECOND%
set WORKDIR=%~dp0
set OUTDIR=%WORKDIR%out_%FORMAT_DATETIME%\




REM connect ssh server and execute command
%TTMACRO_PATH% sshv2_login_and_command.ttl %HOSTNAME% %PORT% %USERNAME% %PASSWORD% %COMMAND%



REM params for winscp command
set REMOTE_PATH=/home/root/file*
set LOCAL_PATH=%OUTDIR%file*

If not exist %LOCAL_PATH% mkdir %LOCAL_PATH%

REM execution winscp command
%WINSCP_PATH% /command ^
    "open sftp://%USERNAME%:%PASSWORD%@%HOSTNAME%" ^
    "get %REMOTE_PATH% %LOCAL_PATH%" ^
    "exit"

start explorer "%OUTDIR%"

::set LOG_PATH=C:\work\workspace\tools\ssh_fileget\file.log
::%WINSCP_PATH% /log=%LOG_PATH% /command ^
::    "open sftp://%USERNAME%:%PASSWORD%@%HOSTNAME%" ^
::    "get %REMOTE_PATH% %LOCAL_PATH%" ^
::    "exit"

endlocal
;=====================================================================
; connect information
HOSTADDR = params[2]
PORT     = params[3]
USERNAME = params[4]
PASSWORD = params[5]

; timeout for execution command
timeout = 3
;=====================================================================


; make connect command
CONNECT_CMD = HOSTADDR
strconcat CONNECT_CMD ':'
strconcat CONNECT_CMD PORT
strconcat CONNECT_CMD ' /ssh /2 /auth=password /user='
strconcat CONNECT_CMD USERNAME
strconcat CONNECT_CMD ' /passwd='
strconcat CONNECT_CMD PASSWORD

; connect to hostaddress
connect CONNECT_CMD
wait '#'

; execution command
sendln params[6]
wait '#'

; termination 
sendln 'exit'

end
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?