private static PrivateKeyFile GetPrivateKeyFile(SshConnectionConfig config) { PrivateKeyFile pk; if (!string.IsNullOrEmpty(config.KeyPassphrase)) { pk = new PrivateKeyFile(config.PrivateKeyPath, config.KeyPassphrase); } else { pk = new PrivateKeyFile(config.PrivateKeyPath); } return(pk); }
public static SshConnectionConfig GetConnectionConfig(DeploymentTaskConfig config, Dictionary <string, string> credentials) { int port = 22; var host = config.TargetHost; if (config.TargetHost?.Contains(":") == true) { try { var components = config.TargetHost.Split(':'); port = int.Parse(components[1].Trim()); host = components[0].Trim(); } catch { } } var sshConfig = new SshConnectionConfig { Host = host, Port = port }; credentials.TryGetValue("username", out var username); if (username != null) { sshConfig.Username = username; } credentials.TryGetValue("password", out var password); if (password != null) { sshConfig.Password = password; } credentials.TryGetValue("privatekey", out var privatekey); if (privatekey != null) { sshConfig.PrivateKeyPath = privatekey; } credentials.TryGetValue("key_passphrase", out var passphrase); if (passphrase != null) { sshConfig.KeyPassphrase = passphrase; } return(sshConfig); }
public static ConnectionInfo GetConnectionInfo(SshConnectionConfig config) { var authMethods = new List <AuthenticationMethod>(); if (!string.IsNullOrEmpty(config.PrivateKeyPath)) { // public key auth via private key var privateKey = GetPrivateKeyFile(config); var keyauth = new PrivateKeyAuthenticationMethod(config.Username, privateKey); authMethods.Add(keyauth); } if (!string.IsNullOrEmpty(config.Password)) { // https://stackoverflow.com/questions/15686276/unable-to-connect-to-aixunix-box-with-ssh-net-library-error-value-cannot-b var kbdi = new KeyboardInteractiveAuthenticationMethod(config.Username); kbdi.AuthenticationPrompt += new EventHandler <AuthenticationPromptEventArgs>( (Object sender, AuthenticationPromptEventArgs e) => { foreach (AuthenticationPrompt prompt in e.Prompts) { if (prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) != -1) { prompt.Response = config.Password; } } }); // password auth authMethods.Add(new PasswordAuthenticationMethod(config.Username, config.Password)); // keyboard interactive auth authMethods.Add(kbdi); } // TODO : proxy support? return(new ConnectionInfo(config.Host, config.Port, config.Username, authMethods.ToArray())); }
public SftpClient(SshConnectionConfig config) { _config = config; }