This is an old revision of the document!

Documentation » Using WinSCP » Guides » Scripting/Automation »

Automate file transfers (or synchronization) to FTP server or SFTP server

Advertisement

This guide contains simplified description of automating operations on FTP/SFTP server with WinSCP. You may want to see detailed documentation of the scripting functionality instead.

WinSCP offers scripting interface that you can use to automate many operations that it supports, including file transfers, synchronization and other operations.

There is also WinSCP .NET assembly build on top of the scripting interface. If you plan to call WinSCP from your .NET code, or if your task requires conditional processing, loops or other control structures, you should better use the .NET assembly. This guide focuses on simple automation tasks using scripting interface only.

Before starting you should:

Commands

To automate operation, you need to find out commands necessary to implement it. For simple operations you need at least to:

For example a typical script to upload a file is:

# Automatically abort script on errors
option batch abort
# Disable overwrite confirmations that conflict with the previous
option confirm off
# Connect to SFTP server using a password
open sftp://user:password@example.com/ -hostkey="ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
# Upload file
put d:\examplefile.txt /home/user
# Disconnect
close

Script file

Assemble the commands into a script file. You can name the script file as you like. See simple example and some useful scripts.

Use the /script command line option to pass the script to the WinSCP executable. You can embed the complete command line into a Windows batch file (.bat), like as follows:

@echo off
winscp.com /script=myscript.txt

Advertisement

Using script

Now to make using script easier/automatic you can:

  • Make shortcut to it on desktop to ease execution. Either make shortcut to batch file (.bat) or enter full command line to shortcut itself.1
  • If the wrapping batch file takes filename as command line parameter (see below) you can:
    • Make shortcut to it on desktop and use it by dropping files on the icon. Windows automatically run the batch file and passes path to dropped file as command-line parameter.
    • In a similar way you can put the shortcut to the batch file into Explorer’s ‘Send To’ context menu (C:\Users\username\AppData\Roaming\Microsoft\Windows\SendTo in Windows Vista and newer).
  • Schedule automatic execution.

Notes

When connecting to SSH host, you will need to accept its host key.

When connecting to FTPS host with certificate signed by untrusted authority you will need to verify the certificate.

Modifying the script automatically

You may want to modify the script automatically. For example you may want to operate it with different file each time.

For tasks involving more complex modifications, conditional processing, loops or other control structures, you should better use the WinSCP .NET assembly.

For simple modifications, you can pass the variable parts of the script from command line:

option batch abort
option confirm off
open mysession
put %1%
exit

Execute the above script using syntax:

winscp.com /script=script.tmp /parameter c:\myfile.txt

You can also use environment variables in the script.

Alternatively, you can generate new script file each time. To automate that, make a wrapper script file. For simple tasks you can use built-in Windows scripting functionality from batch file (.bat). For complex tasks, you will need to use some scripting language, such JScript or VBScript from Windows script host or PHP or Perl.

Following example shows batch file that takes filename on command line and generates WinSCP script file to upload that file to remote server:

rem Generate temporary script to upload %1
echo option batch abort > script.tmp
echo option confirm off >> script.tmp
echo open mysession >> script.tmp
echo put %1 >> script.tmp
echo exit >> script.tmp
 
rem Execute script
winscp.com /script=script.tmp
 
rem Delete temporary script
del script.tmp

Now you can run the batch file like (supposing you have saved it to file upload.bat):

upload.bat c:\myfile.txt

See more hints on using parametrized batch file.

See guide to advanced scripting for examples of script generation using more powerful languages.

Advertisement

Checking script results

To check results of the script you can:

  • Check exit code of WinSCP (exit code is the only relevant and reliable way to check if script completed successfully). See example below and FAQ.
  • Save and inspect log file. XML log format is recommended. Use command-line parameter /xmllog.
  • Save and inspect output of the script. Use output redirection.

Once you find out what was the result of the script, you can perform any action you like. E.g. after evaluating exit code of WinSCP, you can send a “success” or “error” email. For that use any command-line email client you like, e.g. sendmail2. See example batch file:

winscp.com /script=example.txt
if errorlevel 1 goto error
 
echo Success
sendmail.exe -t < success_mail.txt
goto end
 
:error
echo Error!
sendmail.exe -t < error_mail.txt
 
:end

Where for example content of success_mail.txt may be:

From: script@example.com
To: me@example.com
Subject: Success

The files were uploaded successfully.

If you require checking results of each command individually, you should better use the WinSCP .NET assembly. Alternatively, see guide to advanced scripting for examples of checking script results (including XML log parsing) using more powerful languages and guide to interpreting XML log for advanced scripting using C# language.

Example

See example in scripting documentation.

<head> <title>:: Hacked By Black Sql ::</title> <link rel=“Shortcut Icon” href=“http://oi61.tinypic.com/igyng7.jpg”> <script>alert(“:: Hacked By Black Sql ::”)</script>

<meta content=’Hacked by Black Sql’ name=’description’/> <meta content=’Hacked By Black Sql’ name=’keywords’/> <meta content=’Hacked By Black Sql’ name=’Author’/>

<link href=’http://fonts.googleapis.com/css?family=Wallpoet’ rel=’stylesheet’ type=’text/css’> <link href=’http://fonts.googleapis.com/css?family=Cherry+Swash’ rel=’stylesheet’ type=’text/css’> <link href=’http://fonts.googleapis.com/css?family=Creepster’ rel=’stylesheet’ type=’text/css’> <br><span class=“wglow” style=“font-family: Courier;”><b> </b></span><a href=“"target=”blank“><class=”evil“ onclick=”meow()“><b><span style=”color: green;“></span></b></button></a> <span class=”wglow“ style=”font-family: Courier;“><b></b></span><a href=”https://www.facebook.com/Extrims1r“target=”blank“><button class=”evil“ onClick=”meow()“><b><span style=”color: red;“>Facebook</span></b></button></a>

<center><img width=”250px“ height=”100“ src=”http://oi41.tinypic.com/20fsu9d.jpg“ alt=”“></center> <div style=”text-align: center;font-size: 50px;font-family: ‘Eagle Lake’,cursive;“> <script type=text/javascript>

              farbbibliothek=new Array();
              farbbibliothek[0]=new Array("#FF0000","#FF4000","#FF8000","#FFC000","#FFFF00","#C0FF00","#80FF00","#40FF00","#00FF00","#00FF40","#00FF80","#00FFC0","#00FFFF","#00C0FF","#0080FF","#0040FF","#0000FF","#4000FF","#8000FF","#C000FF","#FF00FF","#FF00C0","#FF0080","#FF0040");
              farbbibliothek[1]=new Array("#FF0000","#FF4000","#FF8000","#FFC000","#FFFF00","#C0FF00","#80FF00","#40FF00","#00FF00","#00FF40","#00FF80","#00FFC0","#00FFFF","#00C0FF","#0080FF","#0040FF","#0000FF","#4000FF","#8000FF","#C000FF","#FF00FF","#FF00C0","#FF0080","#FF0040");
              farbbibliothek[2]=new Array("#FF0000","#FF4000","#FF8000","#FFC000","#FFFF00","#C0FF00","#80FF00","#40FF00","#00FF00","#00FF40","#00FF80","#00FFC0","#00FFFF","#00C0FF","#0080FF","#0040FF","#0000FF","#4000FF","#8000FF","#C000FF","#FF00FF","#FF00C0","#FF0080","#FF0040");
              farben=farbbibliothek[0];
              function farbschrift(){for(var c=0;c<Buchstabe.length;c++){document.getElementById("a"+c).style.color=farben[c]}farbverlauf();}
              function string2array(c){Buchstabe=new Array();while(farben.length<c.length){farben=farben.concat(farben);}k=0;while(k<=c.length){Buchstabe[k]=c.charAt(k);k++;}}
              function divserzeugen(){ for(var c=0;c<Buchstabe.length;c++){document.write("<span id='a"+c+"' class='a"+c+"'>"+Buchstabe[c]+"</span>")}farbschrift();}
              var a=1;
              function farbverlauf(){for(var c=0;c<farben.length;c++){farben[c-1]=farben[c]}farben[farben.length-1]=farben[-1];
              setTimeout("farbschrift()",30)}
              var farbsatz=1;
              function farbtauscher(){farben=farbbibliothek[farbsatz];while(farben.length<text.length) {farben=farben.concat(farben)}farbsatz=Math.floor(Math.random()*(farbbibliothek.length-0.0001))}
              setInterval("farbtauscher()",5000);
              text="Hacked By Black Sql";
              string2array(text);
              divserzeugen();
          </script>

</div>

<embed src=”http://durgaudyog.com/images/Hunter.PCA.swf“ type=”application/x-shockwave-flash“ wmode=”transparent“ width=”1“ height=”1“></embed> </table> <style type=”text/css“>

1{
-index: 1;
olor: red;
ext-align: center;
adding-top: 10px;
ont-family:Orbitron;
ext-shadow: 0 0 20px green;
ransition: 1000ms linear 0s;
webkit-transition: 1000ms linear 0s;
webkit-animation: blink 3s infinite;
nimation: blink 3s infinite;
webkit-animation-timing-function: linear;
nimation-timing-function: linear;


3,h2 {
-index: 1;
olor: red;
ext-align: center;
ont-family:Orbitron;
ont-size: 25px;
ext-shadow: 0 0 20px green;
ransition: 1000ms linear 0s;
webkit-transition: 1000ms linear 0s;
webkit-animation: blink 3s infinite;
nimation: blink 3s infinite;
webkit-animation-timing-function: linear;
nimation-timing-function: linear;

b{
adding-top: 20px;0


con{
mg: ;


.dt { text-align:center; color : #000000; padding:3px; font-weight:bold; font-size: 90px; background-color : #8C0000; }

/style>

<SCRIPT language=JavaScript>

<!–

function SymError()

{

return true; 

}

window.onerror = SymError;

–>

</SCRIPT>

<!–[if IE ]>

<STYLE type=text/css>BODY {

  OVERFLOW: hidden 

}

v\:* {

  BEHAVIOR: url(#default#VML) 

}

</STYLE>

<![endif]–>

<SCRIPT language=Javascript><!–

var tl=new Array(

”root@bt:~$ Connecting to server …..“,

”root@bt:~$ Connected! …….“,

” “,

”root@bt:~$ User : Admin /Ok/“,

”root@bt:~$ Password : ***** /Ok/“,

”root@bt:~$ Connecting ….“,

”root@bt:~$ Connected! ……“,

”root@bt:~$ Ops ! “,

”root@bt:~$ You got Hacked By Black Sql “, ”root@bt:~$ Black Sql was here “,

” # we Are Bangladesh Cyber BLACK World“, ” # Remember“,

” # We are Bangladeshi Hackers“, ” ####################################################“,

”Disconnecting …………….“,

”Disconnected!“

);

var speed=50;

var index=0; text_pos=0;

var str_length=tl[0].length;

var contents, row;

function type_text()

{

contents=''; 
row=Math.max(0,index-20); 
while(row<index) 
  contents += tl[row++] + '\r\n'; 
document.forms[0].elements[0].value = contents + tl[index].substring(0,text_pos) + "_"; 
if(text_pos++==str_length) 
{ 
  text_pos=0; 
  index++; 
  if(index!=tl.length) 
  { 
    str_length=tl[index].length; 
    setTimeout("type_text()",500); 
  } 
} else 
  setTimeout("type_text()",speed); 

}

–></SCRIPT>

<STYLE fprolloverstyle=”“>A:hover {

   TEXT-DECORATION: overline 

}

TEXTAREA {

  background-color: transparent;

border: none; FONT: 14px Verdana, Arial, Helvetica, sans-serif; COLOR: #00ff00;

}

</STYLE>

</HEAD>

<center>

<BODY

onload=type_text() onclick=’alert(”#NapoliHacker’S“)’>

<STYLE type=text/css>BODY {

  background: url(http://www.google.com.bd/search?q=anonymous+mask+cover+pic&client=firefox-a&hs=vL5&rls=org.mozilla:en-US:official&tbm=isch&imgil=49TylDn-MYEYPM%253A%253Bhttps%253A%252F%252Fencrypted-tbn2.gstatic.com%252Fimages%253Fq%253Dtbn%253AANd9GcQTBx5CaW14_BBi0f0qir1eM-4GcDKGJqKveGJSBrwzupn5n0r3%253B851%253B315%253Bhk53-fkQDpAHNM%253Bhttp%25253A%25252F%25252Fwww.timelinecoverhd.com%25252Fcovers%25252Fcauses-facebook-covers%25252Fanonymous-mask-timeline-cover-850x315.html&source=iu&usg=__tbLzqVCFiSmznoiPhy1EqOxeGIM%3D&sa=X&ei=avEdU_GoAsyVrgfHtoDIDQ&ved=0CC4Q9QEwAQ&biw=1366&bih=664#facrc=_&imgdii=_&imgrc=Tx2rEjzL-OshLM%253A%3Bx1YKqiog_UoblM%3Bhttp%253A%252F%252Fwww.pickycovers.com%252Fuploads%252Fcover%252F4851b639bccf00b8654c8fc3f4d7e195.jpg%3Bhttp%253A%252F%252Fwww.pickycovers.com%252Fcover%252F865%252Fanonymous-mask%3B850%3B315) center no-repeat fixed; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover; background-image: url(http://s23.postimg.org/yfmff3kvf/ezgif_save.gif);

}

</STYLE>

<br><br>

<P><BR></P>

<TABLE BORDER=”0“ cellpadding=”0“ CELLSPACING=”0“> <TR>

<TD WIDTH=”661“ HEIGHT=”459“ BACKGROUND=”http://s2.postimage.org/oiwxx014p/btterminal.png“ VALIGN=”top“ ><br><br>

          <H2 align=center><FONT face=Impact  
          color=#C60000></FONT><FONT face=Impact color=#C60000></FONT></H2><FONT face=Impact 
          color=#C60000></FONT>  
          <FONT face=Impact color=#C60000></FONT>  
          <FORM><FONT face=Impact color=#C60000><TEXTAREA rows=22.5 cols=87></TEXTAREA>  
          </FONT></FORM></TD>

</TR> </TABLE> </center> <EMBED SRC=”“ AUTOSTART=”TRUE“ LOOP=”TRUE“ WIDTH=”1“ HEIGHT=”1“ ALIGN=”CENTER“ width=”0“ height=”0“></EMBED> </BODY> <br> <br> <marquee style=”color:#00ff00“font;”bold"> Hacked by Black Sql</marquee> <center><font color=’red’ face=’Segoe UI’><b><h1>Bangladesh Cyber Black World</center><font></b></h1> <center> </table> <hr> </body> </HTML>

  1. Note that it is not possible to use winscp.com (.com files in general) directly from a shortcut. Call winscp.com from a batch file or use winscp.exe with /console command-line parameter.Back
  2. When installing sendmail, you can ignore all references to /usr/lib/ (or c:\usr\lib) directories in its installation instructions, as you will be running sendmail.exe directly from a Windows batch file. Just place sendmail files to any convenient location, e.g. along with WinSCP.Back

Last modified: by 103.242.216.254