Technical Recipes

How to write shell script to sync files automatically using WinSCP

0 Read from a script file specified by /script switch
 
    > winscp.com [/script=script_file] [/command command1 command2 ...] [/parameter param1 ...]
e.g.,
    > winscp.com /script="C:\Users\martin\Documents\myscript.txt"
    > winscp.exe /console /script="myscript.txt"
 
    All WinSCP commands have syntax:
    > command -switch -switch2 parameter1 parameter2 ... parameter

1 Switch to a batch mode
 
    By default an interactive mode is used. 
    To switch to batch mode (All prompts are automatically answered negatively), use the command 
    winscp> option batch abort
    For the batch mode it is recommended to turn off confirmations use
    winscp> option confirm off
    to allow overwrites (otherwise the overwrite confirmation prompt would be answered negatively, making overwrites impossible).

2 Verify the Host Key or Certificate
 
    Use 'hostkey' switch of 'open' command to accept the expected hostkey automatically. Establishes connection to given host. Use session URL or name of the site (stored session) . To open site, stored in folder, use path syntax “folder/site”. Using session URL is preferred as it makes your script independent on the persisted configuration.
 
    winscp> open <session_url>
    winscp> open <site>
 
3 Session URL
 
    The only mandatory part is host. You should also always explicitly specify a protocol (sftp, ftp or scp, the ftps protocol is FTP over TLS/SSL).1) Most URL's will include also username. The port needs to be specified only, when it differs from the default port for the protocol (22 for sftp/scp, 21 for ftp and 990 for implicit ftps).
 
    [ sftp|ftp|ftps|scp :// ][ <username> [ : <password> ] [ ; <fingerprint> ] @ ] <host> [ : <port> ]
    
	e.g.,
	
    open sftp://martin:mypassword@example.com -hostkey="ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"

4 Configuration file

    An ideal situation is when you are able to configure all the options you need using script commands only (option command, switches of other commands, session URL and raw site settings). Then you can force scripting mode to start with the default configuration using /ini=nul, avoiding need for the INI file.
 
    With /ini parameter you may specify local path to configuration INI file. It effectively disables using registry as configuration storage. If the file does not exist, default configuration will be used and the file will be created.

    winscp.exe /ini="C:\Users\martin\Documents\myconfig.ini"

    Use nul instead of path to force WinSCP start with its default configuration and not save the configuration on exit.
 
5 Close the session
	# Disconnect
    winscp> close
    # Exit WinSCP
    winscp> exit
	
6 Execute the script
	winscp.exe /console /script=example.txt
	
    For debugging with "echo on" in the script file
    
    winscp.com /default /script="winscp_blog.txt" /ini=nul
	
7 Script file example
	# Automatically abort script on errors
    option batch abort
    # Disable overwrite confirmations that conflict with the previous
    option confirm off
    # Connect using a password
    # open sftp://user:password@example.com -hostkey="ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
    # Connect
    open sftp://user@example.com -hostkey="ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
    # 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 sftp://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