private void WaitForPrompt(ShellStream stream, bool timeOut) { if (_verbose) { if (timeOut) { stream.Expect(TimeSpan.FromSeconds(5), new Renci.SshNet.ExpectAction("#", (output) => { if (_console != null) _console.Log.Write(output); })); } else { stream.Expect(new Renci.SshNet.ExpectAction("#", (output) => { if (_console != null) _console.Log.Write(output); })); } } else { if (timeOut) stream.Expect("#", TimeSpan.FromSeconds(5)); else stream.Expect("#"); } }
/// <summary> /// Dump the input until we see a particular string in the returning text. /// </summary> /// <param name="s"></param> /// <param name="refreshTimeout">If we see something back from the host, reset the timeout counter</param> /// <param name="p"></param> /// <param name="failNow">Function that returns true if we should throw out right away</param> private void DumpTillFind(ShellStream s, string matchText, Action<string> ongo = null, bool dontdumplineofmatch = true, int secondsTimeout = 60*60, bool refreshTimeout = false, Func<bool> failNow = null ) { var lb = new LineBuffer(ongo); if (dontdumplineofmatch) { lb.Suppress(matchText); } var timeout = DateTime.Now + TimeSpan.FromSeconds(secondsTimeout); bool gotmatch = false; while (timeout > DateTime.Now) { s.Expect(TimeSpan.FromMilliseconds(100), new ExpectAction(matchText, l => { lb.Add(l); gotmatch = true; })); gotmatch = gotmatch || lb.Match(matchText); if (gotmatch) break; var data = s.Read(); if (data != null && data.Length > 0 && refreshTimeout) { timeout = DateTime.Now + TimeSpan.FromSeconds(secondsTimeout); } lb.Add(data); if (failNow != null && failNow()) { throw new SSHCommandInterruptedException("Calling routine requested termination of command"); } } if (!gotmatch) { throw new TimeoutException(string.Format("Waiting for '{0}' back from host and it was not seen inside of {1} seconds.", matchText, secondsTimeout)); } lb.DumpRest(); }
private void SSHForm_Load(object sender, EventArgs e) { client = new SshClient(server, port, user,pwd); client.Connect(); string reply = string.Empty; shellStream = client.CreateShellStream("dumb", 80, 24, 800, 600, 1024); reply = shellStream.Expect(new Regex(@":.*>#"), new TimeSpan(0, 0, 3)); shellStream.DataReceived += ShellStream_DataReceived; richTextBox1.Text = "server connected\n"; richTextBox1.SelectionStart = richTextBox1.Text.Length; richTextBox1.ScrollToCaret(); }