Пример #1
0
        /// <summary>
        /// Loop of single commands entered at console and sent to remote
        /// Each command is essentially executed as a new login
        /// (e.g. remote will always start at same login directory)
        ///
        /// Will List output of the command to console,
        /// and if non-empty, a [[StdErr]] heading and error output
        ///
        /// Use 'exit' command to break the loop
        /// </summary>
        public void ManualSingleCommandLoop()
        {
            string cmdInput;
            var    cmd = new SshCmdBase(_localSsh);

            do
            {
                Console.Write("{0}:{1} > ", _localSsh.HostName, _localSsh.HostPort);
                cmdInput = Console.ReadLine().Trim();
                if (cmdInput.Length > 0 && cmdInput.ToLower() != "exit")
                {
                    Console.WriteLine();
                    cmd.CmdText = cmdInput;
                    _localSsh.ExecuteBaseSingleCommand(cmd);
                    if (cmd.StdOutText.Length > 0)
                    {
                        Console.WriteLine(cmd.StdOutText);
                    }
                    if (cmd.StdErrText.Length > 0)
                    {
                        Console.WriteLine("[[StdErr]]");
                        Console.WriteLine(cmd.StdErrText);
                    }
                }
            } while (cmdInput.ToLower() != "exit");

            Console.WriteLine();
        }
        public void ExecuteSingleCommand_ThrowsApplicationExceptionWithDefaultData(string host, int port, string user, string pass)
        {
            var    sut = new SshSessionBase(host, port, user, pass);
            var    cmd = new SshCmdBase(sut);
            Action act = () => cmd.ExecuteCmd("echo test");

            act.ShouldThrow <ApplicationException>()
            .WithMessage("Connection Info not set!");
        }
        public void ExecuteSingleCommand_ReturnsFalseWithBadIpAddress(string host, int port, string user, string pass)
        {
            var sut = new SshSessionBase(host, port, user, pass);
            var cmd = new SshCmdBase(sut);

            cmd.ExecuteCmd("echo test");

            cmd.IsExecuted.Should().BeFalse();
        }
        public void ExecuteSingleCommand_DoesNotThrowExceptionWithBadIpAddress()
        {
            var    sut = new SshSessionBase("0.0.0.0", 22, "user", "pass");
            var    cmd = new SshCmdBase(sut);
            Action act = () => cmd.ExecuteCmd("echo test");

            act.ShouldNotThrow();
            cmd.CmdText.Should().Be("echo test");
            cmd.StdOutText.Should().BeNull();
            cmd.StdErrText.Should().BeNull();
        }
Пример #5
0
        /// <summary>
        /// Tests several basic SSH commands to make sure the connection is functioning. Prints the commands, stdout, and stderr, with headers for each.
        /// </summary>
        public void TestBasicSshCommandsWithStream()
        {
            {
                // Create a basic command object, using our existing SshSessionBase object
                var cmd = new SshCmdBase(_localSsh);

                cmd.CmdText = "echo \"This is StdOut\"; echo \"This is StdErr\" >&2";
                ExecuteSshCmdWithFullConsoleOutput(cmd);

                Console.WriteLine("---");
                cmd.CmdText = "pwd; cd ..; pwd; echo $USER";
                ExecuteSshCmdWithFullConsoleOutput(cmd);

                Console.WriteLine("---");
                cmd.CmdText = "pwd";
                ExecuteSshCmdWithFullConsoleOutput(cmd);

                Console.WriteLine("---");
                cmd.CmdText = "df -h";
                ExecuteSshCmdWithFullConsoleOutput(cmd);

                Console.WriteLine();
            }
        }