Use SFTP to send file with public/private keys along passphrase

WinSCP is a great open source tool to use sftp protocol to send files.
Besides you can use user name and password, you can use public key and private key pair to authenticate to the site.
With newer version (>=5.6), you can automate an agent job to run an SSIS package with Script Task to include passphrase used to generate the key pair.
The following code snippet is an example:

string logname = “E:\\temp\\log\\” + DateTime.Now.ToString(“yyyyMMddHHmmssfff”) + “thislog.xml”;
//// Run hidden WinSCP process
Process winscp = new Process();
winscp.StartInfo.FileName = “c:\\Program Files (x86)\\WinSCP\\”;

winscp.StartInfo.Arguments = “/log=\”” + logname + “\””;
winscp.StartInfo.UseShellExecute = false;
winscp.StartInfo.RedirectStandardInput = true;
winscp.StartInfo.RedirectStandardOutput = true;
winscp.StartInfo.CreateNoWindow = true;

//// Feed in the scripting commands

winscp.StandardInput.WriteLine(“option batch abort”);
winscp.StandardInput.WriteLine(“option confirm off”);

winscp.StandardInput.WriteLine(“open theuser@ -privatekey=E:\\keys\\mykeys\\myprivate.ppk -passphrase=MySecretPhrase “);
winscp.StandardInput.WriteLine(“put E:\\output\\exportfile.csv”);


string output = winscp.StandardOutput.ReadToEnd();

// Wait until WinSCP finishes
Dts.TaskResult = (int)ScriptResults.Success;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s