public SSHSettings GetSshSettings(Options options)
        {
            SSHSettings ssh = null;

            switch (DeploymentSettings.DeploymentType)
            {
            case DeploymentType.RemoteMachine:
            {
                ssh = new SSHSettings()
                {
                    Password = DeploymentSettings.Password,
                    Server   = DeploymentSettings.Host,
                    Username = DeploymentSettings.Username
                };
                break;
            }

            case DeploymentType.ThisMachine:
            {
                ssh = options.ParseSSHConfiguration(DeploymentSettings.ThisMachinePassword);
                break;
            }
            }

            if (ssh == null)
            {
                throw new Exception("I doubt this has ever happened");
            }

            return(ssh);
        }
        public SSHSettings ParseSSHConfiguration(string password)
        {
            if (!VerifyPassword(password))
            {
                return(null);
            }

            var settings = new SSHSettings()
            {
                Password           = SSHPassword,
                KeyFile            = SSHKeyFile,
                AuthorizedKeysFile = SSHAuthorizedKeys,
                KeyFilePassword    = SSHKeyFilePassword,
                Server             = SSHConnection
            };

            if (settings.Server != null)
            {
                var parts = settings.Server.Split(':');
                if (parts.Length == 2 && int.TryParse(parts[1], out int port))
                {
                    settings.Port   = port;
                    settings.Server = parts[0];
                }
                else
                {
                    settings.Port = 22;
                }

                parts = settings.Server.Split('@');
                if (parts.Length == 2)
                {
                    settings.Username = parts[0];
                    settings.Server   = parts[1];
                }
                else
                {
                    settings.Username = "******";
                }
            }
            return(settings);
        }
示例#3
0
        public static async Task <SshClient> ConnectAsync(this SSHSettings sshSettings, CancellationToken cancellationToken = default)
        {
            if (sshSettings == null)
            {
                throw new ArgumentNullException(nameof(sshSettings));
            }
            TaskCompletionSource <SshClient> tcs = new TaskCompletionSource <SshClient>(TaskCreationOptions.RunContinuationsAsynchronously);

            new Thread(() =>
            {
                SshClient sshClient = null;
                try
                {
                    sshClient = new SshClient(sshSettings.CreateConnectionInfo());
                    sshClient.HostKeyReceived += (object sender, Renci.SshNet.Common.HostKeyEventArgs e) =>
                    {
                        e.CanTrust = true;
                    };
                    sshClient.Connect();
                    tcs.TrySetResult(sshClient);
                }
                catch (Exception ex)
                {
                    tcs.TrySetException(ex);
                    try
                    {
                        sshClient?.Dispose();
                    }
                    catch { }
                }
            })
            {
                IsBackground = true
            }.Start();

            using (cancellationToken.Register(() => { tcs.TrySetCanceled(); }))
            {
                return(await tcs.Task);
            }
        }