This is an old revision of the document!
Scripting/Automation
In addition to graphical interface, WinSCP offers scripting/console interface with many commands. The commands can be typed in interactively, or read from script file or another source.
- Using Scripting
- 指令
- The Console Interface Tool
- Running a Script under a Different Account
- Example
- Useful Scripts
Advertisement
Using Scripting
See Command-line parameters to learn how to enter the console/scripting mode.
For automation, commands can be read from a script file specified by /script
switch, from standard input or passed from the command-line using the /command
switch.
By default an interactive mode is used (the user is prompted in the same way as in GUI mode). To switch to a batch mode (all prompts are automatically answered negatively) use the command option batch on
. For the batch mode it is recommended to turn off confirmations using option confirm off
to allow overwrites (otherwise the overwrite confirmation prompt would be answered negatively, making overwrites impossible).
Multiple sessions can be opened simultaneously. Use the session
command to switch between them.
Note that the first connection to an SSH server requires verification of the host key, which cannot be automated by scripting. Therefore, some provision for accepting keys must be made prior to running the script to connect to a new host. Never attempt to make the script verify the host key automatically.
WinSCP returns exit code 1, when any command is interrupted due to an error or any prompt is answered Abort (even automatically in batch mode). Otherwise it returns the exit code 0.
指令
WinSCP 支援下列指令:
指令 | 指令描述 |
---|---|
call | Executes arbitrary remote shell command |
cd | 切換遠端工作資料夾 |
chmod | 切換遠端檔案權限 |
close | 結束工作階段連線 |
exit | 結束所有工作階段連線並關閉程式 |
get | 從遠端資料夾下載檔案到本機資料夾 |
help | 求助 |
keepuptodate | 同步 |
lcd | 切換本機工作資料夾 |
lls | 列出本機資料夾內容 |
ln | 建立遠端符號連結 |
lpwd | 顯示本機工作資料夾 |
ls | 顯示遠端資料夾內容 |
mkdir | 建立遠端資料夾 |
mv | 移動遠端檔案 |
open | 連線到伺服器 |
option | 設定或顯示 script 選項 |
put | 從本機資料夾上傳檔案到遠端資料夾 |
pwd | 顯示遠端工作資料夾 |
rm | 刪除遠端檔案 |
rmdir | 刪除遠端資料夾 |
session | 顯示已連線之工作階段連線或選擇連線中之工作階段連線 |
synchronize | 同步遠端資料夾至本機資料夾 |
Advertisement
The Console Interface Tool
As WinSCP.exe
is a GUI application, it cannot inherit the console window when run from another console application (such as the Windows command-prompt). To allow this, run WinSCP using the console interface tool WinSCP.com
(you can find WinSCP.com
in the main installation package).
With console interface tool you can also use input/output redirection and pipes.
Running a Script under a Different Account
If you are going to run the script under a different account (for example using the Windows scheduler), note that WinSCP may store its configuration to the user part of Windows Registry by default. So you may need to either transfer the configuration from your account registry to the other account registry or use the INI file instead.
Note that the configuration also includes verified SSH host keys, which you may need to transfer as well (or confirm manually under the account) to allow automatic execution of the script.
Example
The example below connects to example.com
server with account user
, downloads file and closes the session. Then it connects to the same server with the account user2
and uploads the file back.
# Automatically answer all prompts negatively not to stall # the script on errors option batch on # Disable overwrite confirmations that conflict with the previous option confirm off # Connect using a password # open user:password@example.com # Connect open user@example.com # Change remote directory cd /home/user # Force binary mode transfer option transfer binary # Download file to the local directory d:\ get examplefile.txt d:\ # Disconnect close # Connect as a different user open user2@example.com # Change the remote directory cd /home/user2 # Upload the file to current working directory put d:\examplefile.txt # Disconnect close # Exit WinSCP exit
Advertisement
Save the script to the file example.txt
. To execute the script file use the following command. As the script connects the session itself, using the command open
, omit the session
command-line parameter.
winscp.exe /console /script=example.txt
Instead of using open
command you can also open a session using a command-line parameter.
winscp.exe /console /script=example.txt user@example.com
Useful Scripts
You can see list of scripts other users found useful.
For more tips, see also FAQs on scripting.