예제 #1
0
        public static TElSimpleSFTPClient CreateClient(Uri uri, bool useKeyAuthentication = false)
        {
            var builder = new UriBuilder(uri);

            if (!builder.Scheme.Equals("sftp", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new ArgumentException(String.Format("Expecting Secure FTP Scheme, but instead got '{0}'.", uri.Scheme));
            }

            var client = new TElSimpleSFTPClient
            {
                Address = builder.Host,
                Port    = builder.Port != -1
          ? builder.Port
          : 22,
                Username = uri.UserInfo,
                Password = builder.Password
            };

            client.OnKeyValidate += OnKeyValidate;

            if (useKeyAuthentication)
            {
                client.KeyStorage          = KeyStorage;
                client.AuthenticationTypes = SBSSHConstants.__Global.SSH_AUTH_TYPE_PUBLICKEY;
            }


            try
            {
                client.Open();

                return(client);
            }
            catch (Exception exception)
            {
                Console.WriteLine("Connection failed due to exception: " + exception.ToString());
                Console.WriteLine("If you have ensured that all connection parameters are correct and you still can't connect,");
                Console.WriteLine("please contact EldoS support as described on http://www.eldos.com/sbb/support-tech.php");
                Console.WriteLine("Remember to provide details about the error that happened.");

                if (client.ServerSoftwareName.Length > 0)
                {
                    Console.WriteLine("Server software identified itself as: " + client.ServerSoftwareName);
                }

                try
                {
                    client.Dispose();
                    client = null;
                }
                catch
                {
                }
            }

            return(client);
        }
예제 #2
0
        public static TElSimpleSFTPClient GetSFTPClient(FtpInfo info)
        {
            TElSimpleSFTPClient client = new TElSimpleSFTPClient();

            client.Address = info.FtpAddress.Replace("ftp://", "");
            client.Port    = 22;

            client.Username = info.FtpUserName;
            client.Password = info.FtpPassword;

            client.OnKeyValidate += new TSSHKeyValidateEvent(client_OnKeyValidate);

            try
            {
                client.Open();
                return(client);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Connection failed due to exception: " + e.Message);
                System.Console.WriteLine("If you have ensured that all connection parameters are correct and you still can't connect,");
                System.Console.WriteLine("please contact EldoS support as described on http://www.eldos.com/sbb/support-tech.php");
                System.Console.WriteLine("Remember to provide details about the error that happened.");
                if (client.ServerSoftwareName.Length > 0)
                {
                    System.Console.WriteLine("Server software identified itself as: " + client.ServerSoftwareName);
                }
                try
                {
                    client.Close(true);
                    return(null);
                }
                catch
                {
                }
            }


            return(client);
        }