private static Renci.SshNet.PrivateKeyFile[] GetKeyFiles() { var keyDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh"); var keyFilePaths = new List <string>(); foreach (var i in Directory.EnumerateFiles(keyDirectory)) { if (File.Exists(i + ".pub")) { keyFilePaths.Add(i); } } return(keyFilePaths.Select(i => { try { var r = new Renci.SshNet.PrivateKeyFile(i); return r; } catch { return null; } }).Where(i => i != null).ToArray()); }
public static Renci.SshNet.SshClient GetSSHConnection(SSHSecurityOption sshSecurityOption, string machineName, int sshPort, string userName, string password, string privateKeyFile, string passCodeOrPhrase) { Renci.SshNet.SshClient sshClient; if (sshSecurityOption == SSHSecurityOption.Password) { byte[] b = System.Text.UTF8Encoding.UTF8.GetBytes(password.ToCharArray()); Renci.SshNet.AuthenticationMethod am = new Renci.SshNet.PasswordAuthenticationMethod(userName, b); Renci.SshNet.ConnectionInfo ci = new Renci.SshNet.ConnectionInfo(machineName, sshPort, userName, am); sshClient = new Renci.SshNet.SshClient(ci); } else if (sshSecurityOption == SSHSecurityOption.PrivateKey) { Renci.SshNet.PrivateKeyFile[] pkf = new Renci.SshNet.PrivateKeyFile[1]; pkf[0] = new Renci.SshNet.PrivateKeyFile(privateKeyFile, passCodeOrPhrase); Renci.SshNet.PrivateKeyAuthenticationMethod pm = new Renci.SshNet.PrivateKeyAuthenticationMethod(userName, pkf); Renci.SshNet.ConnectionInfo ci = new Renci.SshNet.ConnectionInfo(machineName, sshPort, userName, pm); sshClient = new Renci.SshNet.SshClient(ci); } else { Renci.SshNet.KeyboardInteractiveAuthenticationMethod kauth = new Renci.SshNet.KeyboardInteractiveAuthenticationMethod(userName); Renci.SshNet.PasswordAuthenticationMethod pauth = new Renci.SshNet.PasswordAuthenticationMethod(userName, password); keyBoardPassword = password; kauth.AuthenticationPrompt += new EventHandler <Renci.SshNet.Common.AuthenticationPromptEventArgs>(HandleKeyEvent); sshClient = new Renci.SshNet.SshClient(new Renci.SshNet.ConnectionInfo(machineName, sshPort, userName, pauth, kauth)); } if (machineName.Trim().Length > 0) { sshClient.Connect(); } return(sshClient); }
public LibraryFixture() { var config = base.Config; if (config.Password is not null) { Library = new(config.Host, config.Port, config.Username, config.Password); } else if (config.PathToPrivateKey is not null) { var path = Concrete.Client.FixPath(config.PathToPrivateKey); var privateKey = new Renci.SshNet.PrivateKeyFile(path); Library = new(config.Host, config.Port, config.Username, privateKey); } else { Library = new(config.Host, config.Port, config.Username); } Library.Connect(); }