[Solved] C# Validate sftp connection or check the access, list directories, upload files to the sftp server using Renci.SshNet and ftp server by using sftpClient.

Solution

There is a simple way to validate the sftp server in c#, follow the below instructions to validate the SFTP server using c#.

Install the packages

Use NuGet package manager to install the SSH.NET api to our application as shown in the below image.

Renci.SshNet package installation

Procedure to validate the sftp connection.

To check sftp connections we can use test https://test.rebex.net/ ftp and sftp URL. And, use the below code snippet for the reference to validate the sftp connection.

using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            List<string> dirNames = new List<string>();
            string url = "test.rebex.net";
            string usn = "demo";
            string pwd = "password";
            string dir = "testdir";
            int status = 0;
            status = IsValidSFTPConnection(url, dir, usn, pwd);
            if (status > 0)
            {
                // status==1 --> Invalid Server/Host Name
                // status==2 -->  Invalid Directory Name
                // status==3 --> user name password is wrong
            }
            else
            {
                //sftp connection is sucessful
            }
            Console.ReadLine();
        }
      
        internal static int IsValidSFTPConnection(string sftpServerName, string sftpDirectory, string userId, string password)
        {
            int status = 0;
            try
            {
                if (string.IsNullOrEmpty(sftpServerName))
                {
                    return status = 1;
                }
                if ((string.IsNullOrEmpty(userId)) || (string.IsNullOrEmpty(password)))
                {
                    return status = 3;
                }

                using (var client = new SftpClient(sftpServerName, 22, userId, password))
                {
                    client.Connect();

                    client.Disconnect();
                }
            }
            catch (Exception ex)
            {
                //Log(ex.ToString(), 1, "FTP File Connection Not Successful.");
                status = 4;
                if (ex.Message.Contains("No such host is known"))
                {
                    status = 1;
                }
                if (ex.Message.Contains("Path not found") || ex.Message.Contains("No such file"))
                {
                    status = 2;
                }
                if (ex.Message.Contains("Permission denied (password)"))
                {
                    status = 3;
                }
                if (ex.Message.Contains("established connection failed because connected host has failed to respond"))
                {
                    status = 5;
                }
                return status;
            }
            return status;
        }

    }
}

Summary

We can Renci.SshNet package using NuGet package manager to validate the FTP credentials and the server.

Shivaraju M
Shivaraju M

Over 3+ years experience in IT industry, good knowledge in . Net-based Web applications, windows applications, web services, and SPAs. I have played
multiple roles related to technical delivery in multiple domains while working on over 10+ technologies. I am
currently focused on continuous improvement and agile project management.

One comment

Leave a Reply

%d bloggers like this: