예제 #1
0
        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);
        }
예제 #2
0
 public void SaveToXmlElementAttr(System.Xml.XmlElement node)
 {
     node.SetAttributeValue("sshSecOpt", SSHSecurityOption.ToString());
     node.SetAttributeValue("machine", ComputerName);
     node.SetAttributeValue("sshPort", SSHPort);
     node.SetAttributeValue("userName", UserName);
     node.SetAttributeValue("password", Password);
     node.SetAttributeValue("privateKeyFile", PrivateKeyFile);
     node.SetAttributeValue("passPhrase", PassPhrase);
     node.SetAttributeValue("persistent", Persistent);
 }