Differences

This shows you the differences between the selected revisions of the page.

guide_psftp_script_to_winscp 2015-08-26 guide_psftp_script_to_winscp 2023-10-06 (current)
Line 1: Line 1:
-====== Converting PuTTY PSFTP script to WinSCP script ====== +====== Converting PuTTY PSFTP or OpenSSH SFTP script to WinSCP script ====== 
-This guide explains how to convert existing SFTP file transfer script using [[&url(puttydoc)/Chapter6.html|PuTTY SFTP client]] (''psftp.exe'') to [[scripting|script using WinSCP]], in case you want to use some advanced feature that WinSCP offers, such as [[scriptcommand_synchronize|synchronization]], [[script_upload_multiple_servers|parametrized script]], [[scripting#timestamp|timestamped file names]], [[script_custom_listing_format_csv|custom directory listing format]] and others. +This guide explains primarily how to convert existing SFTP file transfer script using [[&url(puttydoc)/Chapter6.html|PuTTY SFTP client]] (''psftp.exe'') to [[scripting|script using WinSCP]], in case you want to use some advanced feature that WinSCP offers, such as [[scriptcommand_synchronize|synchronization]], [[script_upload_multiple_servers|parametrized script]], [[scripting#timestamp|timestamped file names]], [[script_custom_listing_format_csv|custom directory listing format]] and others.  
 + 
 +As the PSFTP syntax is based on OpenSSH ''sftp'' client,((Both the PSFTP command-line switches and the script commands are subset of OpenSSH ''sftp'' switches and commands.)) you can also use the guide to convert your existing Linux SFTP script to a Windows SFTP script.
===== [[commandline]] Converting Command-Line ===== ===== [[commandline]] Converting Command-Line =====
Line 9: Line 11:
^ Parameter ^ Conversion ^ ^ Parameter ^ Conversion ^
| ''[user@]host'' | Specify the username and the hostname in a [[session_url|session URL]] in an ''[[scriptcommand_open|open]]'' command in the script. \\ Example: ''open %%sftp://user@host/%%'' | | ''[user@]host'' | Specify the username and the hostname in a [[session_url|session URL]] in an ''[[scriptcommand_open|open]]'' command in the script. \\ Example: ''open %%sftp://user@host/%%'' |
-| ''-b file'' | Replace with ''/script=file''. | +| ''-b file'' | Replace with ''[[commandline#scripting|/script=file]]''. | 
-| ''-bc'' | Use an ''[[scriptcommand_option|option echo on]]'' command at the beginning of the script. | +| ''-bc'' | Use an ''[[scriptcommand_option#echo|option echo on]]'' command at the beginning of the script. | 
-| ''-be'' | Use an ''[[scriptcommand_option|option batch on]]'' command at the beginning of the script (to counter [[scripting#using_scripting|the default]] ''option batch abort''). | +| ''-be'' | Use an ''[[scriptcommand_option#batch|option batch on]]'' command at the beginning of the script (to counter [[scripting#using_scripting|the default]] ''option batch abort''). | 
-| ''-load sessname'' | [[ui_import|Import your PuTTY stored session to WinSCP]] and use an ''[[scriptcommand_open|open sessname]]'' command in the script. Though this makes the script dependent on GUI configuration (see [[#ini|below]]). You should better use a [[ui_generateurl|Generate URL function]] and use the generated session URL in an ''[[scriptcommand_open|open]]'' command in the script. |+| ''-load sessname'' | [[ui_import|Import your PuTTY stored session to WinSCP]] and use an ''[[scriptcommand_open|open sessname]]'' command in the script. Though this makes the script dependent on GUI configuration (see [[#ini|below]]). You should better use a [[ui_generateurl|Generate Session URL/Code function]] and use the generated ''[[scriptcommand_open|open]]'' command in the script. |
| ''-l user'' | Specify the username in a [[session_url|session URL]] in an ''[[scriptcommand_open|open]]'' command in the script. \\ Example: ''open %%sftp://user@example.com/%%'' | | ''-l user'' | Specify the username in a [[session_url|session URL]] in an ''[[scriptcommand_open|open]]'' command in the script. \\ Example: ''open %%sftp://user@example.com/%%'' |
| ''-P port'' | Specify the port number in a [[session_url|session URL]] in an ''[[scriptcommand_open|open]]'' command in the script. \\ Example: ''open %%sftp://example.com:port/%%'' | | ''-P port'' | Specify the port number in a [[session_url|session URL]] in an ''[[scriptcommand_open|open]]'' command in the script. \\ Example: ''open %%sftp://example.com:port/%%'' |
-| ''-pw passw'' | Specify the password in a [[session_url|session URL]] in an ''[[scriptcommand_open|open]]'' command in the script. \\ Example: ''open %%sftp://user:passw@example.com/%%'' | +| ''-pw passw'' | Specify the password using ''[[scriptcommand_open#password|-password]]'' switch in an ''[[scriptcommand_open|open]]'' command in the script. \\ Example: ''open %%sftp://user@example.com/%% -password=passw'' | 
-| ''-i key'' | Specify the private key using ''-privatekey'' switch in an ''[[scriptcommand_open|open]]'' command in the script. \\ Example: ''open %%sftp://user@example.com/%% -privatekey=key'' | +| ''-pwfile path'' | Specify the password file path using ''[[scriptcommand_open#password|-password]]'' switch in an ''[[scriptcommand_open|open]]'' command in the script and enable reading the password from the file using [[scriptcommand_open#passwordsfromfiles|''-passwordsfromfiles'']] switch. \\ Example: ''open %%sftp://user@example.com/%% -password=path -passwordsfromfiles'' | 
-| ''%%-hostkey aa:bb:cc:...%%'' | Specify the expected hostkey using ''-hostkey'' switch in an ''[[scriptcommand_open|open]]'' command in the script. WinSCP requires key type and size in the signature. \\ Example: ''%%open sftp://user@example.com/ -hostkey="ssh-rsa 2048 aa:bb:cc...%%"'' \\ (assuming 2048-bit RSA host key) |+| ''-i key'' | Specify the private key using ''[[scriptcommand_open#privatekey|-privatekey]]'' switch in an ''[[scriptcommand_open|open]]'' command in the script. \\ Example: ''open %%sftp://user@example.com/%% -privatekey=key'' | 
 +| ''%%-hostkey aa:bb:cc:...%%'' | Specify the expected hostkey using ''[[scriptcommand_open#hostkey|-hostkey]]'' switch in an ''[[scriptcommand_open|open]]'' command in the script. With WinSCP, you should include key type and size in the signature. While they are not required, it is still recommended to add them as otherwise WinSCP may choose to use a different (better) host key algorithm and the checksum will not match (the same is actually true for PSFTP). \\ Example: ''%%open sftp://user@example.com/ -hostkey="ssh-rsa 2048 aa:bb:cc:...%%"'' \\ (assuming 2048-bit RSA host key) \\ See also [[#hostkey|Verifying the host key]] section below.  |
For example the following PSFTP command-line: For example the following PSFTP command-line:
Line 34: Line 37:
<code winscp> <code winscp>
-open sftp://martin:password@example.com/ -hostkey="ssh-rsa 2048 aa:bb:cc:..."+open sftp://martin@example.com/ -password=password -hostkey="ssh-rsa 2048 aa:bb:cc:..."
</code> </code>
Line 43: Line 46:
WinSCP in a scripting/console mode shares a [[config|configuration]] with a [[interfaces|graphical mode]] by default. Particularly when implementing the script from scratch, it is recommended to [[scripting#configuration|isolate the script from the graphical mode configuration]] to avoid the script breaking, when the GUI configuration changes. WinSCP in a scripting/console mode shares a [[config|configuration]] with a [[interfaces|graphical mode]] by default. Particularly when implementing the script from scratch, it is recommended to [[scripting#configuration|isolate the script from the graphical mode configuration]] to avoid the script breaking, when the GUI configuration changes.
-For that, add an ''/ini=nul'' command-line parameter:+For that, add an ''[[commandline#configuration|/ini=nul]]'' command-line parameter:
<code batch> <code batch>
Line 49: Line 52:
</code> </code>
 +===== [[hostkey]] Verifying the Host Key =====
 +
 +If your ''psftp.exe'' command-line does not use ''-hostkey'' switch, it means that you rely on PuTTY/PSFTP host key cache in Windows registry. That makes your script non-portable.
 +
 +With WinSCP it's recommended to always [[scripting#hostkey|verify the host key explicitly]] using ''[[scriptcommand_open#hostkey|-hostkey]]'' switch of an ''[[scriptcommand_open|open]]'' command.
===== Converting Commands ===== ===== Converting Commands =====
Line 54: Line 62:
The differences are described below. The differences are described below.
 +
 +WinSCP requires the script to end with the ''[[scriptcommand_exit|exit]]'' command for the process to close automatically.
==== cd ==== ==== cd ====

Last modified: by martin