Exemple #1
0
 public void ExpectActionConstructorTest1()
 {
     Regex expect = null; // TODO: Initialize to an appropriate value
     Action<string> action = null; // TODO: Initialize to an appropriate value
     ExpectAction target = new ExpectAction(expect, action);
     Assert.Inconclusive("TODO: Implement code to verify target");
 }
        private void processShellStream(ShellStream shellStream, String expectedRegexPattern, StreamWriter writer, string input, bool isOptional, ICollection <string> messages)
        {
            bool            wasExecuted = false;
            Action <string> action      = (x) => {
                if (writer != null && input != null)
                {
                    writer.WriteLine(input);
                }
                ;
                wasExecuted = true;
                messages.Clear();
            };
            var expectAction = new ExpectAction(new Regex(expectedRegexPattern, RegexOptions.IgnoreCase), action);

            shellStream.Expect(TimeSpan.FromSeconds(Timeout), expectAction);
            // Shell output is always null when the action was not executed.
            if (!(isOptional || wasExecuted))
            {
                var message = messages.LastOrDefault();
                if (messages.Count > 1)
                {
                    message = string.Format("{0} / {1}", messages.First(), messages.Last());
                }
                throw new Exception(String.Format("Error changing password. Got '{0}' from shell which didn't match the expected '{1}' regex pattern.", message, expectedRegexPattern));
            }
        }
Exemple #3
0
        public void ExpectActionConstructorTest1()
        {
            Regex           expect = null; // TODO: Initialize to an appropriate value
            Action <string> action = null; // TODO: Initialize to an appropriate value
            ExpectAction    target = new ExpectAction(expect, action);

            Assert.Inconclusive("TODO: Implement code to verify target");
        }
        public void Constructor_RegexAndAction()
        {
            var expect = new Regex("^.*");
            Action<string> action = Console.WriteLine;

            var target = new ExpectAction(expect, action);

            Assert.AreSame(expect, target.Expect);
            Assert.AreSame(action, target.Action);
        }
        public void Constructor_RegexAndAction()
        {
            var             expect = new Regex("^.*");
            Action <string> action = Console.WriteLine;

            var target = new ExpectAction(expect, action);

            Assert.AreSame(expect, target.Expect);
            Assert.AreSame(action, target.Action);
        }
        public void Constructor_StringAndAction()
        {
            var expect = new Random().Next().ToString(CultureInfo.InvariantCulture);
            Action<string> action = Console.WriteLine;

            var target = new ExpectAction(expect, action);

            Assert.IsNotNull(target.Expect);
            Assert.AreEqual(expect, target.Expect.ToString());
            Assert.AreSame(action, target.Action);
        }
        public void Constructor_StringAndAction()
        {
            var             expect = new Random().Next().ToString(CultureInfo.InvariantCulture);
            Action <string> action = Console.WriteLine;

            var target = new ExpectAction(expect, action);

            Assert.IsNotNull(target.Expect);
            Assert.AreEqual(expect, target.Expect.ToString());
            Assert.AreSame(action, target.Action);
        }
        public static async Task ExpectAsync(this Session session, string pattern, Match match, Action <string> handler,
                                             CancellationToken cancellationToken = default)
        {
            var action = new ExpectAction(new ActionContext(session.Expectable, session.ItemsCache))
            {
                Pattern = pattern,
                Match   = match,
                Handler = handler
            };

            await new ExpectActionHandler().Handle(action, cancellationToken).ConfigureAwait(false);
        }
Exemple #9
0
        /// <summary>
        /// Dump the input until we see a particular string in the returning text.
        /// </summary>
        /// <param name="s">The ssh stream to run against</param>
        /// <param name="matchText">The text to match - command is done and we return when we see it</param>
        /// <param name="failNow">Function that returns true if we should throw out right away</param>
        /// <param name="ongo">When we see a complete line, call this function. Defaults to null</param>
        /// <param name="secondsTimeout">How long should there be before we have a timeout.</param>
        /// <param name="refreshTimeout">If we see something back from the host, reset the timeout counter</param>
        /// <param name="crlfExpectedAtEnd">If the crlf is expected, then eat it when we see it. A command line prompt, for example, will not have this.</param>
        /// <param name="seeAndRespond">Dictionary of strings to look for and to respond to with further input.</param>
        private static async Task DumpTillFind(ShellStream s,
                                               SshClient client,
                                               string matchText,
                                               Action <string> ongo   = null,
                                               int secondsTimeout     = 60 *60,
                                               bool refreshTimeout    = false,
                                               Func <bool> failNow    = null,
                                               bool crlfExpectedAtEnd = false,
                                               Dictionary <string, string> seeAndRespond = null
                                               )
        {
            /// <summary>
            /// How long to go before checking for update data from the stream. The actual timeout accuracy
            /// when refreshtimeout is set will depend on this.
            /// </summary>
            TimeSpan TimeoutGranularity = TimeSpan.FromSeconds(5);

            // Send back text we see if there is someone watching.
            // Don't send back the matched text.
            var lb = new LineBuffer();

            if (ongo != null)
            {
                lb.AddAction(l => { if (!l.Contains(matchText))
                                    {
                                        ongo(l);
                                    }
                             });
            }

            // Set when we see the text we are trying to match.
            bool gotmatch = false;

            // The Shell Stream works by looking for strings, and when it matches, performing a call out and returning.
            // The most obvious string to wait for is our matched string.
            var expectedMatchText = matchText + (crlfExpectedAtEnd ? LineBuffer.CrLf : "");
            var matchText_action  = new ExpectAction(expectedMatchText, l =>
            {
                Trace.WriteLine($"DumpTillFill: Found expected Text: '{SummarizeText(l)}'");
                lb.Add(l);
                gotmatch = true;
            });

            Trace.WriteLine($"DumpTillFind: searching for text: {matchText} (with crlf: {crlfExpectedAtEnd})");

            // Next is the end-of-line action.
            var matchEOL_action = new ExpectAction(new Regex("^.*" + LineBuffer.CrLf + "$"), l => {
                if (!gotmatch)
                {
                    lb.Add(l);
                }
            });

            // Finally, there are some see-and-then-send actions that might be present. When we see a match, send back some text.
            // This is an easy way to deal with a conversation (like a password request).
            var seeAndRespond_actions = seeAndRespond == null
                        ? Enumerable.Empty <ExpectAction>()
                        : seeAndRespond
                                        .Select(sr => new ExpectAction(sr.Key, whatMatched =>
            {
                Trace.WriteLine($"DumpTillFill: Found seeAndRespond: {whatMatched}");
                lb.Add(whatMatched);
                s.WriteLine(sr.Value);
            }));

            if (seeAndRespond != null)
            {
                foreach (var item in seeAndRespond)
                {
                    Trace.WriteLine($"DumpTillFind:  -> also looking for '{item.Key}' and will respond with '{item.Value}'");
                }
            }

            // All the actions together
            var expect_actions = new[] { matchText_action, matchEOL_action }
            .Concat(seeAndRespond_actions)
            .ToArray();

            // When is time up? If we are supposed to refresh the timeout then everytime the data changes in the buffer, we will want
            // to update the timeout.
            var timesUp          = DateTime.Now + TimeSpan.FromSeconds(secondsTimeout);
            var streamDataLength = new ValueHasChanged <long>(() => s.Length);

            streamDataLength.Evaluate();

            while (timesUp > DateTime.Now)
            {
                // Make sure the connection hasn't dropped. We won't get an exception later on if that hasn't happened.
                if (!client.IsConnected)
                {
                    throw new SSHConnectionDroppedException($"Connection to {client.ConnectionInfo.Host} has been dropped.");
                }

                // Wait for some sort of interesting text to come back.
                var howLongToWait = MinTime(timesUp - DateTime.Now, TimeoutGranularity);
                var seenText      = await Task.Factory.FromAsync(
                    s.BeginExpect(howLongToWait, null, null, expect_actions),
                    s.EndExpect);

                if (gotmatch)
                {
                    break;
                }

                // Reset the timeout if data has come in and we are doing that.
                if (refreshTimeout && streamDataLength.HasChanged)
                {
                    timesUp = DateTime.Now + TimeSpan.FromSeconds(secondsTimeout);
                }

                // Check to see if we should fail
                if (failNow != null && failNow())
                {
                    throw new SSHCommandInterruptedException("Calling routine requested termination of command");
                }
            }

            // Done. IF there is anything that doesn't include a complete line in there, then dump it.
            lb.DumpRest();
            if (!gotmatch)
            {
                var tmpString = lb.ToString();
                Debug.WriteLine($"Waiting for '{matchText}' back from host and it was not seen inside of {secondsTimeout} seconds. Remaining in buffer: '{tmpString}'");
                throw new TimeoutException($"Waiting for '{matchText}' back from host and it was not seen inside of {secondsTimeout} seconds. Remaining in buffer: '{tmpString}'");
            }
        }
Exemple #10
0
        private string ReadResponse(Regex CustomPrompt = null, ResponseDelegate CustomPromptDelegate = null)
        {
            string Response = "";

            bool End = false;

            //Regular bash prompt
            var RegularBashPrompt = new ExpectAction(new Regex(@"\w+@\w+\:[\w~\/\\]+\$"), (Input) =>
            {
                End       = true;
                Response += Input;
            });

            //Root bash prompt
            var RootBashPrompt = new ExpectAction(new Regex(@"\w+@\w+\:[\w~\/\\]+#"), (Input) =>
            {
                End       = true;
                Response += Input;
            });

            //Vyatta configure prompt
            var VyattaConfigurePrompt = new ExpectAction(new Regex(@"\w+@\w+#"), (Input) =>
            {
                End       = true;
                Response += Input;
            });


            //tail/head paging prompt
            var PagingPrompt = new ExpectAction(new Regex(@"\n\:"), (Input) =>
            {
                Response += Input;
                Stream.Write(" ");
            });

            var CustomPromptAction = CustomPrompt != null ? new ExpectAction(CustomPrompt, (Input) =>
            {
                Response += Input;
                Stream.Write(" ");

                if (CustomPromptDelegate != null)
                {
                    if (CustomPromptDelegate(Response, Stream))
                    {
                        End = true;
                    }
                }
            }) : null;

            while (!End)
            {
                if (CustomPromptAction != null)
                {
                    Stream.Expect(
                        RegularBashPrompt,
                        RootBashPrompt,
                        VyattaConfigurePrompt,
                        PagingPrompt,
                        CustomPromptAction
                        );
                }
                else
                {
                    Stream.Expect(
                        RegularBashPrompt,
                        RootBashPrompt,
                        VyattaConfigurePrompt,
                        PagingPrompt
                        );
                }
            }

            return(Response);
        }