{"id":665,"date":"2020-04-27T19:19:57","date_gmt":"2020-04-27T19:19:57","guid":{"rendered":"http:\/\/markedcode.com\/?p=665"},"modified":"2025-03-03T13:44:34","modified_gmt":"2025-03-03T13:44:34","slug":"how-to-access-sftp-with-ssis-using-ssh-net","status":"publish","type":"post","link":"https:\/\/markedcode.com\/index.php\/2020\/04\/27\/how-to-access-sftp-with-ssis-using-ssh-net\/","title":{"rendered":"How to access SFTP with SSIS using SSH.NET"},"content":{"rendered":"<p>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.<\/p>\n<h2><strong>Configure SSIS<\/strong><\/h2>\n<p>\u2981 Make new SSIS Project<br \/>\n\u2981 Next, add script tag to control flow<br \/>\n\u2981 Then, edit script<br \/>\n\u2981 Right click References and select \u2018Manage NuGet Packages\u2019<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-668 size-medium\" src=\"http:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/NuGet-300x252.png\" alt=\"How to access SFTP with SSIS using SSH.NET - package install\" width=\"300\" height=\"252\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/NuGet-300x252.png 300w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/NuGet-768x646.png 768w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/NuGet.png 990w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>\u2981 After that, go to Browse and find the following to packages and versions<br \/>\n\u2981 SSH.NET by Renci \u2013 2016.0.0<br \/>\n\u2981 SshNet.Security.Cryptography by Renci \u2013 1.2.0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-669 size-medium\" src=\"http:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Packages-300x140.png\" alt=\"Needed packages\" width=\"300\" height=\"140\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Packages-300x140.png 300w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Packages-1024x477.png 1024w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Packages-768x357.png 768w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Packages.png 1287w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>\u2981 Right click on the project and select properties \u2013 I am using 4.5<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-670 size-medium\" src=\"http:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Version-300x114.png\" alt=\"c# version\" width=\"300\" height=\"114\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Version-300x114.png 300w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Version-1024x390.png 1024w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Version-768x293.png 768w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Version-1536x585.png 1536w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><br \/>\n\u2981 Add the following name space to the script<br \/>\n\u2981 using Renci.SshNet;<br \/>\n\u2981 Add the following code to connect to the server and print out the files in the desired directory<\/p>\n<h2><strong>Code for SFTP connection with SSH.NET<\/strong><\/h2>\n<p style=\"text-align: left;\"><strong>public void Main()<\/strong><br \/>\n<strong>{<\/strong><br \/>\n<strong>\u00a0 \/\/ SFTP credentials \u2013 smart to make these SSIS variables and read them in<\/strong><br \/>\n<strong>\u00a0 var host = &#8220;host.com&#8221;; \/\/No \u201csftp:\/\/\u201d prior to host<\/strong><br \/>\n<strong>\u00a0 var port = 2222;<\/strong><br \/>\n<strong>\u00a0 var username = &#8220;user&#8221;;<\/strong><br \/>\n<strong>\u00a0 var password = &#8220;pass&#8221;;<\/strong><\/p>\n<p style=\"text-align: left;\"><strong>\u00a0 \/\/Directories<\/strong><br \/>\n<strong>\u00a0 var remoteDir = &#8220;\/&#8221;; \/\/This is the desired remote directory<\/strong><\/p>\n<p style=\"text-align: left;\"><strong>\u00a0 \/\/Create client with credientials<\/strong><br \/>\n<strong>\u00a0 using (var client = new SftpClient(host,port,username,password))<\/strong><br \/>\n<strong>\u00a0 {<\/strong><br \/>\n<strong>\u00a0 Console.WriteLine(&#8220;Connect to &#8221; + host + &#8221; as &#8221; + username);<\/strong><br \/>\n<strong>\u00a0 client.Connect();<\/strong><br \/>\n<strong>\u00a0 Console.WriteLine(&#8220;Connected.&#8221;);<\/strong><\/p>\n<p style=\"text-align: left;\"><strong>\u00a0 \/\/List files in target server directory<\/strong><br \/>\n<strong>\u00a0 var files = client.ListDirectory(remoteDir);<\/strong><\/p>\n<p style=\"text-align: left;\"><strong>\u00a0 Console.WriteLine(&#8220;Files in &#8221; + remoteDir + &#8220;: &#8220;);<\/strong><\/p>\n<p style=\"text-align: left;\"><strong>\u00a0 \u00a0foreach (var file in files)<\/strong><br \/>\n<strong>\u00a0 {\u00a0<\/strong><br \/>\n<strong>\u00a0 \u00a0 if (client.Exists(remoteDir + file.Name)) \/\/Not needed<\/strong><br \/>\n<strong>\u00a0 \u00a0 {<\/strong><br \/>\n<strong>\u00a0 \u00a0 \u00a0 \u00a0Console.WriteLine(&#8220;Exists: &#8221; + file.Name);<\/strong><br \/>\n<strong>\u00a0 \u00a0 \u00a0}<\/strong><br \/>\n<strong>\u00a0 }<\/strong><br \/>\n<strong>}<\/strong><\/p>\n<p><strong>Dts.TaskResult = (int)ScriptResults.Success; <\/strong><br \/>\n<strong>}<\/strong><\/p>\n<p>Great,\u00a0 now we have all the base SSIS and C# looking good let\u2019s try to run it!<br \/>\nOh no! We get an error in the script!<\/p>\n<p><strong>Error: Could not load file or assembly &#8216;Renci.SshNet, Version=2016.1.0.0, Culture=neutral, PublicKeyToken=1cee9f8bde3db106&#8217; or one of its dependencies. The system cannot find <\/strong><br \/>\n<strong>the file specified.<\/strong><\/p>\n<h2><strong>Install packages to GAC manually<\/strong><\/h2>\n<p>For some reason, SSIS does not automatically see the packages you installed. We need to manually download the dll\u2019s now for our referenced packages and install them to the GAC (Global assembly cache). Go to the following links and download the packages.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-671 size-medium\" src=\"http:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DownloadDLL-300x181.png\" alt=\"How to access SFTP with SSIS using SSH.NET - packages website\" width=\"300\" height=\"181\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DownloadDLL-300x181.png 300w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DownloadDLL-1024x616.png 1024w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DownloadDLL-768x462.png 768w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DownloadDLL-1536x924.png 1536w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><br \/>\n<a href=\"https:\/\/www.nuget.org\/api\/v2\/package\/SSH.NET\/2016.0.0\">https:\/\/www.nuget.org\/api\/v2\/package\/SSH.NET\/2016.0.0<\/a><br \/>\n<a href=\"https:\/\/www.nuget.org\/packages\/SshNet.Security.Cryptography\/1.2.0\">https:\/\/www.nuget.org\/packages\/SshNet.Security.Cryptography\/1.2.0<\/a><\/p>\n<p>Once they are downloaded, we can unzip the packages with 7-Zip. In the Downloaded package, ssh.net.2016.0.0 &gt; lib &gt; netstandard1.3, I can see the dll that I want to manually add to the GAC.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-672 size-medium\" src=\"http:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DllFile-300x52.png\" alt=\"Local Dll file\" width=\"300\" height=\"52\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DllFile-300x52.png 300w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DllFile-1024x178.png 1024w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DllFile-768x134.png 768w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DllFile-1536x267.png 1536w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DllFile.png 1720w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><br \/>\nOpen \u2018Developer Command Prompt for VS 2017\u2019 \u2013 and Run as Admin<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-673 size-medium\" src=\"http:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DevCmdVS2017-277x300.png\" alt=\"Dev cmd prompt for VS2017\" width=\"277\" height=\"300\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DevCmdVS2017-277x300.png 277w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DevCmdVS2017-945x1024.png 945w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DevCmdVS2017-768x833.png 768w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/DevCmdVS2017.png 964w\" sizes=\"auto, (max-width: 277px) 100vw, 277px\" \/><br \/>\nAfter that, run the following command for both packages<br \/>\ngacutil \/i [PATH TO DLL] \/f<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-674 size-medium\" src=\"http:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/GACInstall-300x59.png\" alt=\"How to access SFTP with SSIS using SSH.NET - manual package install to GAC\" width=\"300\" height=\"59\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/GACInstall-300x59.png 300w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/GACInstall-1024x200.png 1024w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/GACInstall-768x150.png 768w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/GACInstall.png 1445w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><br \/>\nNow 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&#8217;s so SSIS can see the packages we wish to use.<\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/markedcode.com\/index.php\/2020\/04\/17\/how-to-automate-ssis-packages-in-ssms\/\">For more SSIS development blogs, read on!<\/a><\/p>\n<p>For technical questions, please visit <a href=\"https:\/\/d365techsupport.com\" data-type=\"link\" data-id=\"https:\/\/d365techsupport.com\">https:\/\/d365techsupport.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7,20],"tags":[21],"class_list":{"0":"post-665","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-d365","7":"category-ssis","8":"tag-how-to-access-sftp-using-ssh-net-renci-ssis-c-code-microsoft-sql"},"_links":{"self":[{"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/posts\/665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/comments?post=665"}],"version-history":[{"count":3,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/posts\/665\/revisions"}],"predecessor-version":[{"id":1338,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/posts\/665\/revisions\/1338"}],"wp:attachment":[{"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/media?parent=665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/categories?post=665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/tags?post=665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}