|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, August 14, 2012 1:10 PM
Points: 15,
Visits: 113
|
|
awesome. this was something that I was looking for. Adding 3rd party apps to my ETL is a little mickey mouse for me.
Thank You very much.
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: 2 days ago @ 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Saturday, January 12, 2013 7:04 AM
Points: 610,
Visits: 425
|
|
An alternative method would be to set up a "virtual" FTP server using stunnel (www.stunnel.org) pointing to the remote sftp server, which you then connect using normal ftp uploads, and sftp handles the SSL element.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, January 08, 2013 4:14 PM
Points: 5,
Visits: 30
|
|
Interesting article, but I chose a slightly different path in handling things. I am using the portable version of WinSCP, meaning there is no installation needed. They also expose Command Line arguments, meaning that all the stuff stored in the batch file is passed along as variables in the package. So I can store the variables in a database somewhere, call them when the package loads, then pass them along to the command line options. Consider the following written in C#...)
//Attaching the debugger in a 64-bit instance of SSIS will not work.
//MessageBox.Show("Attach the Debugger", "Attach the Debugger"); // TODO: Add your code here ScriptResults TaskResult;
//jf 04132010 Created objects to hold variables set at package level String WinSCPPath = Dts.Variables["WinSCPPath"].Value.ToString(); String WinSCPCom = Dts.Variables["WinSCPCom"].Value.ToString(); ; String WinSCPini = Dts.Variables["WinSCPini"].Value.ToString(); String WinSCPLoggingPath = Dts.Variables["WinSCPLoggingPath"].Value.ToString(); String WinSCPLogFileName = Dts.Variables["WinSCPLogFileName"].Value.ToString(); String LoginName = Dts.Variables["LoginName"].Value.ToString(); String LoginPassword = Dts.Variables["LoginPassword"].Value.ToString(); String SiteURI = Dts.Variables["SiteURI"].Value.ToString(); String HostKey = Dts.Variables["HostKey"].Value.ToString(); String FileToPush = Dts.Variables["FileToPush"].Value.ToString(); String UploadDirectory = Dts.Variables["UploadDirectory"].Value.ToString();//UploadDirectory
//jf 04132010 Created object to hold reference for FireAgain of the FireProgress Control bool fireAgain = false;
try { //jf 04132010 Attach winscp to the process within the Package System.Diagnostics.Process winscp = new System.Diagnostics.Process(); Dts.Events.FireProgress("WinSCP process Created", 0, 0, 100, "WinSCP", ref fireAgain);
//jf 04132010 Pass variables into the Process object //Tell the Process where to find the WinSCP object for loading winscp.StartInfo.FileName = String.Format("{0}{1}" , WinSCPPath , WinSCPCom);
//Pass the location of the ini file and the logging xml file to the Process winscp.StartInfo.Arguments = String.Format("/log={3}{1}" , WinSCPPath , WinSCPLogFileName , WinSCPini , WinSCPLoggingPath); Dts.Events.FireProgress("Assigned Logging File", 10, 0, 100, "WinSCP", ref fireAgain);
//Do not launch a new shell "cmd" window winscp.StartInfo.UseShellExecute = false;
//Redirect the input and output to the Logging Component winscp.StartInfo.RedirectStandardInput = true; winscp.StartInfo.RedirectStandardOutput = true;
//Do not show a cmd window winscp.StartInfo.CreateNoWindow = true;
//Start the Process Thread winscp.Start(); Dts.Events.FireProgress("WinSCP process Launched", 20, 0, 100, "WinSCP", ref fireAgain);
//Pass configuration information to WinSCP winscp.StandardInput.WriteLine("option batch abort"); winscp.StandardInput.WriteLine("option confirm off");
//Pass Login information to WinSCP for Authentication winscp.StandardInput.WriteLine(String.Format("open {0}:{1}@{2} -hostkey=\"{3}\"" , LoginName , LoginPassword , SiteURI , HostKey)); Dts.Events.FireProgress("Remote SFTP Connection Established", 40, 0, 100, "WinSCP", ref fireAgain);
//Execute WinSCP Commands winscp.StandardInput.WriteLine("ls");
//--change to the needed directory winscp.StandardInput.WriteLine(String.Format("cd {0}", UploadDirectory)); Dts.Events.FireProgress("Remote SFTP File Action", 60, 0, 100, "WinSCP", ref fireAgain);
//--List the directory winscp.StandardInput.WriteLine("ls"); //--push the file to the server winscp.StandardInput.WriteLine(String.Format("put {0}", FileToPush)); Dts.Events.FireProgress("Process Finished", 80, 0, 100, "WinSCP", ref fireAgain); Dts.Events.FireProgress(String.Format("put {0}", FileToPush), 0, 0, 0, "WinSCP", ref fireAgain);
//Close input to the file winscp.StandardInput.Close(); Dts.Events.FireProgress("Remote SFTP Disconnected, WinSCP Closed", 100, 0, 100, "WinSCP", ref fireAgain);
//wait for WinSCP to Exit winscp.WaitForExit(); MessageBox.Show(winscp.StandardOutput.ReadToEnd(), "WinSCP Output"); TaskResult = ScriptResults.Success; } catch (Exception e) { //If there is problem, fail the event and output the exception Message... Dts.Events.FireError(999, e.Message, "WinSCP", string.Empty, 0); TaskResult = ScriptResults.Failure; //MessageBox.Show(e.Message); }
Dts.TaskResult = (int)TaskResult;
Dts.TaskResult = (int)TaskResult;
Notice that we can also fire events that bubble up a message within the context of the package. This means no third party installation on the server, no batch file to be overwritten and most important of all, everything is saved in a script task in the package.
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Today @ 2:08 PM
Points: 79,
Visits: 600
|
|
Pretty cool.
You should submit that as an article. I did what I did because it's the only thing I could figure out.
Your way is better.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, January 08, 2013 4:14 PM
Points: 5,
Visits: 30
|
|
Stan, That wasn't my intent. I think what you have is a good article, I just don't want to come across as pithy or superior. What if we combine our content? We are both talking about the same topic. shoot me an email and let's talk about it.
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Today @ 2:08 PM
Points: 79,
Visits: 600
|
|
I wasn't being snarky either. I like what your way better than mine, but if I hadn't put my way out there, I would never have learned about yours.
It's synergy!
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Saturday, May 11, 2013 9:06 PM
Points: 52,
Visits: 184
|
|
If you don't want usernames and passwords, you can always use certificate based authentication.... Of course then, the issue with the key on the server changing becomes that much harder because now you have a client key to manage as well. I think that in a better world, this type of integration would be managed by a system that has first class support for SFTP and would allow this type of connectivity without worries such as the username and password being stored in a file in clear text... just my 1.4 cents worth.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, April 16, 2013 12:04 PM
Points: 6,
Visits: 46
|
|
| This works great when I use it to send 1 file at a time. How can I edit it to make it loop through a list of files in a folder and send them all by the same sftp?
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Today @ 2:08 PM
Points: 79,
Visits: 600
|
|
Basically, you would just get the list of files using this code:
http://www.thescarms.com/dotnet/listfiles.aspx
Imports System.IO Dim strFileSize As String = "" Dim di As New IO.DirectoryInfo("C:\temp") Dim aryFi As IO.FileInfo() = di.GetFiles("*.txt") Dim fi As IO.FileInfo
For Each fi In aryFi strFileSize = (Math.Round(fi.Length / 1024)).ToString() Console.WriteLine("File Name: {0}", fi.Name) Console.WriteLine("File Full Name: {0}", fi.FullName) Console.WriteLine("File Size (KB): {0}", strFileSize ) Console.WriteLine("File Extension: {0}", fi.Extension) Console.WriteLine("Last Accessed: {0}", fi.LastAccessTime) Console.WriteLine("Read Only: {0}", (fi.Attributes.ReadOnly = True).ToString) Next
Instead of writing to the console, write to a 2-dimensional variable, then you just loop through them.
If this is not enough to get you there, let me know an I will try to write some custom code, but it might take a few days to find the time.
|
|
|
|