How to create an automatic file download and upload tool using the WinSCP command
Publication Date:December 9, 2020
INFOMARTION > How to create an automatic file download and upload tool using the WinSCP command
summary
Create an automatic file download and upload tool using the WinSCP command. Execute the commands provided by WinSCP using the command prompt. Not surprisingly, WinSCP provides a command. You usually use "WinSCP.exe", but "WinSCP.com" is also available. Since you usually open the folder from the shortcut, you will almost never see the "C:\Program Files (x86)\WinSCP" folder in the shortcut destination, but if you look at the shortcut destination, "WinSCP.com" should be stored there as well. This is where we will create the tool.
Table of Contents
- What is the WinSCP command?
- 1-1. What you can do.
- 1-2. What you can't do
- Automatic Download Tool
- 2-1. making
- 2-2. implementation preparation
- 2-3. execution (e.g. program)
- Automatic Upload Tool
- 3-1. making
- 3-2. implementation preparation
- 3-3. execution (e.g. program)
- Other commands that may be used frequently
- 4-1. key authentication
- 4-2. Shell Calls
- summary
1. What is the WinSCP command?
The WinSCP command is a command that allows you to perform the WinSCP operations that you normally perform in the GUI. This operation, which is usually performed on the screen, is executed by command.
1-1. What you can do.
You can download and upload files, create folders, invoke shell commands, and much more.
Commands that may be used frequently are as follows
open・・・Used for connection.
get・・・Used to download files.
put・・・Used to upload files.
exit・・・Used for cutting.
1-2. What you can't do
Basic file downloads and uploads are possible, but after using the system, it seems difficult to control the details.
For example, it seemed difficult to do something like "check the modified date of a file and fetch a file with a specified modified date.
If you want to add complex conditions to the file to be manipulated, you can call the shell with the "call" command, and it will be possible to achieve this using Windows commands, Linux commands, etc.
2. Automatic Download Tool
We would like to create a tool that will actually automatically download logs placed on a Linux server.
2-1. making
Create a tool with the following conditions
・Execution environment is Windows 10
・The destination server is Linux
・IP address is 192.168.50.10
First, create a bat file that will serve as the starting point for the startup.
@setlocal enabledelayedexpansion
@set time2=%time: =0%
@set MKDIR_NAME=%date:~0,4%%date:~5,2%%date:~8,2%%time2:~0,2%%time2:~3,2%%time2:~6,2%
@set FOLDER=%~dp0%MKDIR_NAME%
@mkdir %FOLDER%
@"C:\Program Files (x86)\WinSCP\WinSCP.com" /console /script=%~dp0ftp.txt /parameter %FOLDER%
Except for the last line, the rest of the process is a Windows command. Contents.
@setlocal enabledelayedexpansion
This is a setting for delayed environment variables. Simply put, it is a statement that prevents a value from being set to a variable that may not be reflected.
@set time2=%time: =0%
@set MKDIR_NAME=%date:~0,4%%date:~5,2%%date:~8,2%%time2:~0,2%%time2:~3,2%%time2:~6,2%
@set FOLDER=%~dp0%MKDIR_NAME%
The process of obtaining yyyymmddhmmss from the current time and finally putting it into the FOLDER variable.
@mkdir %FOLDER%
Create a folder "yyyymmddhmmss" directly under the folder where the batch was executed.
@"C:\Program Files (x86)\WinSCP\WinSCP.com" /console /script=%~dp0ftp.txt /parameter %FOLDER%
Run "WinSCP.com (WinSCP command)". The execution executes the commands listed in "ftp.txt" in the same folder where the batch file is stored. Pass the folder path you just created as an argument.
This completes the creation of the caller file. Then, the WinSCP command is described in "ftp.txt".
option batch on
option transfer binary
open test:testpassword@192.168.50.10
get /var/log/httpd/access_log %1%\
close
exit
Contents.
option batch on
For processes that require querying, the system automatically assumes that "No" has been selected and automatically executes the process. If you put this in, it will automatically overwrite any duplicate file names. (I will create a folder "yyyymmddhmmss" and put it there, so there will be no duplication of file names.)
option transfer binary
The setting is to transfer in binary mode.
open test:testpassword@192.168.50.10
User Name:Password@IP Address". Connect to IP address "192.168.50.10".
get /var/log/httpd/access_log %1%\
Store "/var/log/httpd/access_log" in the "yyyymmddhhmmss" folder.(%1%" is the argument passed in file_get.bat)
Regular expressions can also be used, so if you write "/var/log/httpd/*", you can retrieve the entire log.
close
exit
This is a cutting process.
This completes the creation of the tool.
2-2. implementation preparation
Prior to execution, the fingerprints are checked in advance.
If the server has never been connected to via WinSCP, it will check to see if it is safe to connect. If you press "Yes", a record of the "Yes" press will be kept in a place called the Windows registry area. This prevents the system from being checked a second time. Before running the tool, it is necessary to do this work and save the information that the server is safe to connect to in the registry area, so run the following command at the command prompt.
"C:\Program Files (x86)\WinSCP\WinSCP.com"
open test:testpassword@192.168.50.10 ⇒Enter "y" when prompted for a response
close
exit
exit
Please replace "test:testpassword@192.168.50.10" with "username:password@IP address".
This completes the preliminary preparation.
2-3. execution (e.g. program)
Now it's time to execute. Double-click "file_get.bat".
If the folder "yyyymmddhhmmss" is created and the log is stored in the folder, it is successful.
3. Automatic Upload Tool
Next, we would like to create a tool to place files on a Linux server. The basic construction will be the same as the download tool.
3-1. making
Create a tool with the following conditions
・Execution environment is Windows 10
・The destination server is Linux
・IP address is 192.168.50.10
First, create a bat file that will serve as the starting point for the startup.
@setlocal enabledelayedexpansion
@set FOLDER_NAME="put_files\*"
@set FOLDER=%~dp0%FOLDER_NAME%
@set PUT_FOLDER="/tmp/"
@"C:\Program Files (x86)\WinSCP\WinSCP.com" /console /script=%~dp0put_ftp.txt /parameter %FOLDER% %PUT_FOLDER%
This is explained in the automatic download tool section, so I will omit a few explanations, but the process is to store the files in the "put_files" folder in the same folder where the batch file is executed into "/tmp/". The actual storing process is described in "put_ftp.txt".
option batch on
option transfer binary
open test:testpassword@192.168.50.10
put %1% %2%
close
exit
The "put %1%\ %2%" command uploads files according to the "source file" and "destination folder" passed as arguments, although we omit a little explanation since this is also explained in the automatic download tool.
3-2. implementation preparation
As with the automatic download tool, fingerprints are checked in advance before execution.(This is not necessary if you have already run the program once with the automatic download tool.)
Execute the following command at the command prompt
"C:\Program Files (x86)\WinSCP\WinSCP.com"
open test:testpassword@192.168.50.10 ⇒Enter "y" when prompted for a response
close
exit
exit
Please replace "test:testpassword@192.168.50.10" with "username:password@IP address".
This completes the preliminary preparation.
3-3. execution (e.g. program)
Now it's time to execute. Double-click "file_put.bat".
If the files stored in "put_files" are transferred to the "/tmp/" folder, it is a success.
4. Other commands that may be used frequently
We have introduced basic downloading and uploading, but would like to describe other commands that you may use frequently.
4-1. key authentication
Earlier, I logged in with password authentication using the following description.
open test:testpassword@192.168.50.10
open username:password@IP address". In the case of key authentication, the following applies
open test@192.168.50.10 -privatekey=id_rsa.ppk
The "-privatekey=id_rsa.ppk" is the path to the key. Since the paths are relative, it is assumed that the key (id_rsa.ppk) is also in the folder where the executable (bat file) is located. For example, if the key is located directly under the C drive, write "-privatekey=C:\id_rsa.ppk
4-2. Shell Calls
To invoke the shell using the WinSCP command, write as follows
option batch on
option transfer binary
open test:testpassword@192.168.50.10
call sh /tmp/test.sh
close
exit
call sh /tmp/test.sh" will execute "test.sh". It is also possible to pass arguments. When passing arguments, write something like "call sh /tmp/test.sh param1".
5. summary
We have described the WinSCP command.
If you normally use WinSCP's GUI to log and upload files on a daily basis, you can automate this process by creating the tools described in this article. It may be a minor task, but it takes up a lot of time every day. We encourage you to create tools to improve efficiency.
Thank you for taking the time to read this to the end.
■INFORMATION
Please click here to go to the top page of INFORMATION.
■PROFILE
Please click here to view the profile.
■For inquiries, please contact