Differences
This shows you the differences between the selected revisions of the page.
2007-06-28 | 2007-07-12 | ||
no summary (203.118.53.128) (hidden) | removed (59.144.40.74) | ||
Line 1: | Line 1: | ||
- | ====== Scripting/Automation ====== | ||
- | In addition to [[interfaces|graphical interface]], WinSCP offers scripting/console interface with many [[script_commands|commands]]. The commands can be typed in interactively, or read from script file or another source. | ||
- | ===== Using Scripting ===== | ||
- | See [[commandline|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 ''[[script_commands#option|option batch on]]''. For the batch mode it is recommended to turn off confirmations using ''[[script_commands#option|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 ''[[script_commands#session|session]]'' command to switch between them. | ||
- | |||
- | Note that the first connection to an SSH server requires [[ssh#verifying_the_host_key|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. | ||
- | |||
- | ===== Commands ===== | ||
- | The following commands are implemented: | ||
- | |||
- | ^ Command ^ Description ^ | ||
- | | [[script_commands#call|call]] | Executes arbitrary remote shell command | | ||
- | | [[script_commands#cd|cd]] | Changes remote working directory | | ||
- | | [[script_commands#chmod|chmod]] | Changes permissions of remote file | | ||
- | | [[script_commands#close|close]] | Closes session | | ||
- | | [[script_commands#exit|exit]] | Closes all sessions and terminates the program | | ||
- | | [[script_commands#get|get]] | Downloads file from remote directory to local directory | | ||
- | | [[script_commands#help|help]] | Displays help | | ||
- | | [[script_commands#keepuptodate|keepuptodate]] | Continuously reflects changes in local directory on remote one | | ||
- | | [[script_commands#lcd|lcd]] | Changes local working directory | | ||
- | | [[script_commands#lls|lls]] | Lists the contents of local directory | | ||
- | | [[script_commands#ln|ln]] | Creates remote symbolic link | | ||
- | | [[script_commands#lpwd|lpwd]] | Prints local working directory | | ||
- | | [[script_commands#ls|ls]] | Lists the contents of remote directory | | ||
- | | [[script_commands#mkdir|mkdir]] | Creates remote directory | | ||
- | | [[script_commands#mv|mv]] | Moves or renames remote file | | ||
- | | [[script_commands#open|open]] | Connects to server | | ||
- | | [[script_commands#option|option]] | Sets or shows value of script options | | ||
- | | [[script_commands#put|put]] | Uploads file from local directory to remote directory | | ||
- | | [[script_commands#pwd|pwd]] | Prints remote working directory | | ||
- | | [[script_commands#rm|rm]] | Removes remote file | | ||
- | | [[script_commands#rmdir|rmdir]] | Removes remote directory | | ||
- | | [[script_commands#session|session]] | Lists connected sessions or selects active session | | ||
- | | [[script_commands#synchronize|synchronize]] | Synchronizes remote directory with local one | | ||
- | |||
- | ===== [[console]] 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|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 [[config|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 [[config|INI file]] instead. | ||
- | |||
- | Note that the configuration also includes [[ssh#verifying_the_host_key|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. | ||
- | <code winscp> | ||
- | # 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 | ||
- | </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> | ||
- | |||
- | Instead of using ''open'' command you can also open a session using a [[commandline|command-line parameter]]. | ||
- | <code> | ||
- | winscp.exe /console /script=example.txt user@example.com | ||
- | </code> | ||
- | |||
- | ===== Useful Scripts ===== | ||
- | You can see list of [[scripts]] other users found useful. | ||
- | |||
- | For more tips, see also [[faq#scripting_automation|FAQs on scripting]]. |