Пример #1
0
        public static SshExec SshConnect(string ssh_host, int ssh_port, string ssh_login, string ssh_pass, string ssh_key)
        {
            SshExec exec = null;

            if (ssh_key.Length == 0)
            {
                exec = new SshExec(ssh_host, ssh_login, ssh_pass);
            }
            else
            {
                try
                {
                    exec = new SshExec(ssh_host, ssh_login);
                    if (ssh_pass.Length == 0)
                    {
                        exec.AddIdentityFile(ssh_key);
                    }
                    else
                    {
                        exec.AddIdentityFile(ssh_key, ssh_pass);
                    }
                }
                catch { }
            }
            exec.Connect(ssh_port);
            return(exec);
        }
Пример #2
0
        public static void RunExample()
        {
            SshConnectionInfo input = Util.GetInput();

            try
            {
                SshExec exec = new SshExec(input.Host, input.User);
                if (input.Pass != null)
                {
                    exec.Password = input.Pass;
                }
                if (input.IdentityFile != null)
                {
                    exec.AddIdentityFile(input.IdentityFile);
                }

                Console.Write("Connecting...");
                exec.Connect();
                Console.WriteLine("OK");
                while (true)
                {
                    Console.Write("Enter a command to execute ['Enter' to cancel]: ");
                    string command = Console.ReadLine();
                    if (command == "")
                    {
                        break;
                    }
                    string output = exec.RunCommand(command);
                    Console.WriteLine(output);
                }
                Console.Write("Disconnecting...");
                exec.Close();
                Console.WriteLine("OK");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Пример #3
0
        public bool Test()
        {
            bool    result = false;
            SshExec exec   = new SshExec(host, username);

            // SshShell exec = new SshShell(host, username);
            try
            {
                exec.Password = password;
                if (!string.IsNullOrEmpty(pkFile))
                {
                    exec.AddIdentityFile(pkFile);
                }
                exec.Connect();
                string output = exec.RunCommand(commandText);
                Console.WriteLine(output);
                result = true;
            }
            catch (Exception)
            {
                result = false;
            }
            finally
            {
                try
                {
                    if (exec != null && exec.Connected)
                    {
                        exec.Close();
                    }
                }
                catch
                {
                }
            }
            return(result);
        }