Differences
This shows you the differences between the selected revisions of the page.
2012-05-31 | 2012-06-07 | ||
faq_script_hostkey (martin) | no summary (114.121.195.94) (hidden) | ||
Line 14: | Line 14: | ||
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. | 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 ''[[scriptcommand_option|option batch abort]]''. For the batch mode it is recommended to turn off confirmations using ''[[scriptcommand_option|option confirm off]]'' to allow overwrites (otherwise the overwrite confirmation prompt would be answered negatively, making overwrites impossible). | + | By default an interactive mode is used (the user is prompted in the sam |
- | + | ||
- | Multiple sessions can be opened simultaneously. Use the ''[[scriptcommand_session|session]]'' command to switch between them. | + | |
- | + | ||
- | Note that the first connection to an SSH server requires [[scripting#hostkey|verification of the host key]]. | + | |
- | Also the first connection to FTPS host with [[ftps#certificate|certificate]] signed by untrusted authority requires verification of the certificate. | + | |
- | + | ||
- | ~~AD~~ | + | |
- | + | ||
- | ===== 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.((When checking WinSCP exit code from batch file, make sure you are using [[executables|winscp.com]].)) | + | |
- | + | ||
- | To further analyze results of scripted operations, you will find [[logging_xml|XML logging]] useful. | + | |
- | + | ||
- | ===== [[syntax]] Commands Syntax ===== | + | |
- | All WinSCP commands have syntax: | + | |
- | <code> | + | |
- | command -switch -switch2 parameter1 parameter2 ... parametern | + | |
- | </code> | + | |
- | + | ||
- | Command parameters that include space(s) have to be enclosed in double-quotes. To use double-quote literally, double it: | + | |
- | <code winscp> | + | |
- | put "file with spaces and ""quotes"".html" | + | |
- | </code> | + | |
- | + | ||
- | You can use environment variables in the commands, with syntax ''%NAME%'' ((Generally do enclose reference to double-quotes to cope properly with spaces in its value.)): | + | |
- | <code winscp> | + | |
- | put "%FILE_TO_UPLOAD%" | + | |
- | </code> | + | |
- | + | ||
- | You can reference script arguments (passed on command-line using parameter ''[[commandline#scripting|/parameter]]'') using syntax ''%N%'', where ''N'' is ordinal number of argument ((Generally do enclose reference to double-quotes to cope properly with spaces in its value.)): | + | |
- | <code winscp> | + | |
- | put "%1%" | + | |
- | </code> | + | |
- | + | ||
- | Note that WinSCP treats filenames in case sensitive manner. So even if your server treats filenames in case insensitive manner, make sure you specify case properly((This is important particularly for FTP sessions.)). | + | |
- | + | ||
- | To insert comments into the script file, start the line with ''#'' (hash): | + | |
- | <code winscp> | + | |
- | # Connect to the server | + | |
- | open mysession | + | |
- | </code> | + | |
- | + | ||
- | ===== Commands ===== | + | |
- | The following commands are implemented. | + | |
- | + | ||
- | To see help for the command, read respective documentation article below or type command ''[[scriptcommand_help|help <command>]]'' directly in console. | + | |
- | + | ||
- | ^ Command ^ Description ^ | + | |
- | | [[scriptcommand_call|call]] | Executes arbitrary remote shell command | | + | |
- | | [[scriptcommand_cd|cd]] | Changes remote working directory | | + | |
- | | [[scriptcommand_chmod|chmod]] | Changes permissions of remote file | | + | |
- | | [[scriptcommand_close|close]] | Closes session | | + | |
- | | [[scriptcommand_echo|echo]] | Prints message onto script output | | + | |
- | | [[scriptcommand_exit|exit]] | Closes all sessions and terminates the program | | + | |
- | | [[scriptcommand_get|get]] | Downloads file from remote directory to local directory | | + | |
- | | [[scriptcommand_help|help]] | Displays help | | + | |
- | | [[scriptcommand_keepuptodate|keepuptodate]] | Continuously reflects changes in local directory on remote one | | + | |
- | | [[scriptcommand_lcd|lcd]] | Changes local working directory | | + | |
- | | [[scriptcommand_lls|lls]] | Lists the contents of local directory | | + | |
- | | [[scriptcommand_ln|ln]] | Creates remote symbolic link | | + | |
- | | [[scriptcommand_lpwd|lpwd]] | Prints local working directory | | + | |
- | | [[scriptcommand_ls|ls]] | Lists the contents of remote directory | | + | |
- | | [[scriptcommand_mkdir|mkdir]] | Creates remote directory | | + | |
- | | [[scriptcommand_mv|mv]] | Moves or renames remote file | | + | |
- | | [[scriptcommand_open|open]] | Connects to server | | + | |
- | | [[scriptcommand_option|option]] | Sets or shows value of script options | | + | |
- | | [[scriptcommand_put|put]] | Uploads file from local directory to remote directory | | + | |
- | | [[scriptcommand_pwd|pwd]] | Prints remote working directory | | + | |
- | | [[scriptcommand_rm|rm]] | Removes remote file | | + | |
- | | [[scriptcommand_rmdir|rmdir]] | Removes remote directory | | + | |
- | | [[scriptcommand_session|session]] | Lists connected sessions or selects active session | | + | |
- | | [[scriptcommand_stat|stat]] | Retrieves attributes of remote file | | + | |
- | | [[scriptcommand_synchronize|synchronize]] | Synchronizes remote directory with local one | | + | |
- | + | ||
- | ===== [[console]] The Console Interface Tool ===== | + | |
- | Learn about ''[[executables|winscp.com]]'', the console interface tool. | + | |
- | + | ||
- | ===== [[hostkey]] Verifying the Host Key or Certificate in Script ===== | + | |
- | The first connection to an SSH server requires [[ssh#verifying_the_host_key|verification of the host key]]. To automate the verification in script, use ''hostkey'' switch of ''[[scriptcommand_open|open]]'' command to accept the expected hostkey automatically. | + | |
- | + | ||
- | You can find the key fingerprint on [[ui_fsinfo|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. //Learn more about [[faq_script_hostkey|obtaining host key fingerprint]]//. | + | |
- | + | ||
- | [[ftps#certificate|FTPS certificate]] signed by untrusted authority may also need to be verified. To automate the verification in script, use ''certificate'' switch of ''[[scriptcommand_open|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 [[config|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 [[config|INI file]] instead. | + | |
- | + | ||
- | Note that the configuration also includes [[scripting#hostkey|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. | + | |
- | <code winscp> | + | |
- | # 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 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 | + | |
- | </code> | + | |
- | 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. | + | |
- | <code> | + | |
- | winscp.exe /console /script=example.txt | + | |
- | </code> | + | |
- | + | ||
- | For simple scripts you can specify all the commands on [[commandline|command-line]] using ''/command'' switch: | + | |
- | <code> | + | |
- | winscp.exe /console /command "option batch abort" "open user@example.com" "get examplefile.txt d:\" "exit" | + | |
- | </code> | + | |
- | + | ||
- | Instead of using ''open'' command you can also open a session using a [[commandline|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. | + | |
- | <code> | + | |
- | winscp.exe /console /script=example.txt user@example.com | + | |
- | </code> | + | |
- | + | ||
- | ===== Further Reading ===== | + | |
- | * [[guide_automation|Guide to scripting]]; | + | |
- | * [[faq#scripting_automation|FAQs about scripting]]; | + | |
- | * [[scripts|Useful example scripts]]; | + | |
- | * [[library|WinSCP .NET assembly]]. | + |