Example #1
0
        void LoadSshClientConfig(string server, out string host, out int port, out string username, out string keyFileName)
        {
            string  homeDir        = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string  configFileName = Path.Combine(homeDir, ".ssh/config");
            var     config         = SshConfigParser.SshConfig.ParseFile(configFileName);
            SshHost result         = config.Compute(server);

            host        = (string)result ["Host"];
            port        = int.Parse((string)result ["Port"]);
            username    = (string)result ["User"];
            keyFileName = (string)result ["IdentityFile"];
            if (keyFileName.StartsWith("~/", StringComparison.InvariantCulture))
            {
                keyFileName = Path.Combine(homeDir, keyFileName.Substring(2));
            }
        }
Example #2
0
        public SshClient CreateClient(TerminalOptions opts)
        {
            SshClient?.Dispose();
            host = opts.Parameters[0];
            SshConfig config     = SshConfig.ParseFile(opts.ConfigFile);
            SshHost   hostConfig = config.Compute(host);

            keypath = opts.Keypath ?? hostConfig.IdentityFile;
            if (keypath.Contains("~"))
            {
                keypath = keypath.Replace("~", homePath);
            }

            host     = hostConfig.HostName;
            username = opts.User ?? hostConfig.User ?? Environment.UserName;
            password = opts.Password;
            ushort port = opts.Port;

            if (opts.Port == 0)
            {
                port = hostConfig["Port"] is string portStr?ushort.Parse(portStr) : (ushort)22;
            }

            bool PasswordAuthUsed = false;

            if (keypath == null)
            {
                if (password != null)
                {
                    PasswordAuthUsed = true;
                }
                else
                {
                    PasswordAuthUsed = hostConfig["PasswordAuthentication"] as string == "Yes";
                }
            }

            if (PasswordAuthUsed)
            {
                return(new SshClient(host, port, username, password));
            }

            PrivateKeyFile privateKeyFile = new PrivateKeyFile(keypath, password);

            return(new SshClient(host, port, username, privateKeyFile));
        }