This is an old revision of the document!

Scripting/Automation

This article contains detailed description of scripting/automation functionality. You may want to see simplified guide to the functionality instead.

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.

= = Skriptů pouziti viz Parametry příkazového řádku SE dozvíte, Jak Koncoví spotřebitelé Prázdný košík do konzole / scripting režimu. Pro automatizaci, mohou Být Příkazy Číst ZE skript určený/ scriptswitch, ZE standardního vstupu Nebo z příkazového řádku přešel pomoči/ příkaz switch. Ve výchozím nastavení JE Interaktivní režim (Uživatel JE vyzván stejným způsobem JAKO v režimu GUI). Chcete-li dávkové zpracování Přejít na (vsechny výzvy jsou automaticky odpověděl záporně) a zadejte příkazMožnost dávku na. Pro dávkovém režimu SE doporučuje Vypnout potvrzení pomočimožnost potvrdit off, ABY přepíše (jinak výzvu k potvrzení přepisu do odpověděl záporně, takže přepíše nemožné). Více relací mohou Být otevřený současně. Použijtesessionpříkaz k přepínání Mezi Nimi. Všimněte si, ZE prvním připojení k Serveru SSH vyžaduje overeni klíč Počítače. Vezměte První spojení na počítač, FTPS s Certifikát podepsán nedůvěryhodným orgán požaduje overeni certifikátu.

Checking Results

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.1

To further analyze results of scripted operations, you will find XML logging useful.

Commands

The following commands are implemented:

Command Description
call Executes arbitrary remote shell command
cd Changes remote working directory
chmod Changes permissions of remote file
close Closes session
exit Closes all sessions and terminates the program
get Downloads file from remote directory to local directory
help Displays help
keepuptodate Continuously reflects changes in local directory on remote one
lcd Changes local working directory
lls Lists the contents of local directory
ln Creates remote symbolic link
lpwd Prints local working directory
ls Lists the contents of remote directory
mkdir Creates remote directory
mv Moves or renames remote file
open Connects to server
option Sets or shows value of script options
put Uploads file from local directory to remote directory
pwd Prints remote working directory
rm Removes remote file
rmdir Removes remote directory
session Lists connected sessions or selects active session
synchronize Synchronizes remote directory with local one

The Console Interface Tool

Learn about winscp.com, the console interface tool.

Verifying the Host Key or Certificate in Script

The first connection to an SSH server requires verification of the host key. To automate the verification in script, use hostkey switch of open command to accept the expected hostkey automatically.

You can find the key fingerprint on Server and Protocol Information Dialog. You can also copy the key fingerprint to clipboard from the confirmation prompt on the first (interactive) connection using Copy Key button.

FTPS certificate signed by untrusted authority may also need to be verified. To automate the verification in script, use certificate switch of open command to accept the expected certificate automatically.

Running a Script under a Different Account (e.g., Using a Scheduler)

If you are going to run the script under a different account (for example using the Windows scheduler), don’t forget that WinSCP still needs to access its configuration. Note that when using registry as configuration storage, the settings are accessible only for your Windows account, so in such a case you may need to either transfer the configuration from your account registry to the other account’s registry or use the INI file instead.

Note that the configuration also includes verified SSH host keys.

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

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

For simple scripts you can specify all the commands on command-line using /command switch:

winscp.exe /console /command "option batch on" "open user@example.com" "get examplefile.txt d:\" "exit"

Instead of using open command you can also open a session using a command-line parameter. Note that in that case session is opened yet before script starts. Particularly option commands do not apply yet. Generally you should avoid using this method.

winscp.exe /console /script=example.txt user@example.com

Further Reading

  1. When checking WinSCP exit code from batch file, make sure you are using winscp.com.Back

Last modified: by davidvojj