How to access SFTP with SSIS using SSH.NET. I have been looking around for an all encompassing guide to setting up and connecting to an SFTP server with SSIS and have been unable to find such an article. After some trial and error, I have put together how I was able to accomplish accessing an SFTP server with SSIS using SSH.NET by Renci.

Configure SSIS

⦁ Make new SSIS Project
⦁ Next, add script tag to control flow
⦁ Then, edit script
⦁ Right click References and select ‘Manage NuGet Packages’

How to access SFTP with SSIS using SSH.NET - package install

⦁ After that, go to Browse and find the following to packages and versions
⦁ SSH.NET by Renci – 2016.0.0
⦁ SshNet.Security.Cryptography by Renci – 1.2.0

Needed packages

⦁ Right click on the project and select properties – I am using 4.5
c# version
⦁ Add the following name space to the script
⦁ using Renci.SshNet;
⦁ Add the following code to connect to the server and print out the files in the desired directory

Code for SFTP connection with SSH.NET

public void Main()
{
  // SFTP credentials – smart to make these SSIS variables and read them in
  var host = “host.com”; //No “sftp://” prior to host
  var port = 2222;
  var username = “user”;
  var password = “pass”;

  //Directories
  var remoteDir = “/”; //This is the desired remote directory

  //Create client with credientials
  using (var client = new SftpClient(host,port,username,password))
  {
  Console.WriteLine(“Connect to ” + host + ” as ” + username);
  client.Connect();
  Console.WriteLine(“Connected.”);

  //List files in target server directory
  var files = client.ListDirectory(remoteDir);

  Console.WriteLine(“Files in ” + remoteDir + “: “);

   foreach (var file in files)
  { 
    if (client.Exists(remoteDir + file.Name)) //Not needed
    {
       Console.WriteLine(“Exists: ” + file.Name);
     }
  }
}

Dts.TaskResult = (int)ScriptResults.Success;
}

Great,  now we have all the base SSIS and C# looking good let’s try to run it!
Oh no! We get an error in the script!

Error: Could not load file or assembly ‘Renci.SshNet, Version=2016.1.0.0, Culture=neutral, PublicKeyToken=1cee9f8bde3db106’ or one of its dependencies. The system cannot find
the file specified.

Install packages to GAC manually

For some reason, SSIS does not automatically see the packages you installed. We need to manually download the dll’s now for our referenced packages and install them to the GAC (Global assembly cache). Go to the following links and download the packages.

How to access SFTP with SSIS using SSH.NET - packages website
https://www.nuget.org/api/v2/package/SSH.NET/2016.0.0
https://www.nuget.org/packages/SshNet.Security.Cryptography/1.2.0

Once they are downloaded, we can unzip the packages with 7-Zip. In the Downloaded package, ssh.net.2016.0.0 > lib > netstandard1.3, I can see the dll that I want to manually add to the GAC.

Local Dll file
Open ‘Developer Command Prompt for VS 2017’ – and Run as Admin
Dev cmd prompt for VS2017
After that, run the following command for both packages
gacutil /i [PATH TO DLL] /f
How to access SFTP with SSIS using SSH.NET - manual package install to GAC
Now when we run our script we can see that the packages get recognized! In conclusion, we need to install the packages with NuGet, write our code to connect, then manually install the same versions of the dll’s so SSIS can see the packages we wish to use.

Now that we know how to access SFTP with SSIS using SSH.NET, we can write our script to pull files, check if files exist, delete files, push files, and more.

For more SSIS development blogs, read on!

For more tech blogs, subscribe to the code_marks news letter: http://eepurl.com/gZCMQz

Author

5 Comments

  1. Pingback: How to automate SSIS packages in SSMS | Marked Code

  2. Hello Team markedcode,
    many thanks for your post https://markedcode.com/index.php/2020/04/27/how-to-access-sftp-with-ssis-using-ssh-net/
    I follow these steps and works fine in my desktop , the dll is installed and i make a call for renci in my C# code and execution run is success.
    But i still have the problem in cloud , I need to install dll file in IR SSIS (azure) :
    my project is deplyed in Azure server and i should install the dll file (renci) in the global cache of IR SSIS . I follow your steps : I add a folder containing gacutil and the dll file in our specified blob storage , and i add the main.cmd in Azure with this code :
    gacutil\gacutil /i Renci.SshNet.dll /f
    gacutil\gacutil /i SshNet.Security.Cryptography.dll /f
    I don’t need any messages. After I restart IR SSIS and make an execution for my package (with ssis calatog) and still have this error :

    Could not load file or assembly ‘Renci.SshNet, Version=2016.0.0.0, Culture=neutral,
    PublicKeyToken=1cee9f8bde3db106’ or one of its dependencies. The system cannot find the file specified.

    I tried these solutions also with powershell :
    https://stackoverflow.com/questions/45593418/register-dll-in-gac-cmd-or-powershell
    https://www.andrewcbancroft.com/2015/12/16/using-powershell-to-install-a-dll-into-the-gac/
    –> Not working

    Please any help ,
    Ahmed

  3. It’s hard to come by knowledgeable people in this particular topic, but you sound like you know what you’re talking about! Thanks

Write A Comment