ssh.net More Info and samples for streams

Feb 10, 2015 at 6:10 PM
Edited Feb 10, 2015 at 6:18 PM
New User here,
Can I get a more recent example of how to use a stream and expect to get streams to work...using Cisco which want to disconnect after every command. I have partially been able to use 'expect' with success but still not quite sure what it is exactly doing.


Many examples seem years old.

What is the best way to run multiple long output commands with examples please (FOR C#)

Thank you
Feb 11, 2015 at 5:25 PM
Edited Feb 11, 2015 at 5:30 PM
Here is what I have that works
client.Connect();

using (ShellStream shellStream = client.CreateShellStream("dumb", 80, 24, 800, 600, 1024))
{
 var reader = new StreamReader(shellStream);
 var writer = new StreamWriter(shellStream);

 writer.AutoFlush = true;

 while (shellStream.Length == 0)
 {
     Thread.Sleep(500);
 }

 Regex userPromptMatch = new Regex(@"#");
 
 string prompt = shellStream.Expect(userPromptMatch, new TimeSpan(0, 1, 0));
 WriteStream("term length 0", writer, shellStream);
 Console.WriteLine(prompt);
 
 string result = shellStream.Expect("term length 0", new TimeSpan(0, 1, 0));
 Console.WriteLine(result); 
 WriteStream("show int desc", writer, shellStream);
 
 result = shellStream.Expect("show int desc", new TimeSpan(0, 1, 0));
 Console.WriteLine(result);

 string final = shellStream.Expect(prompt, new TimeSpan(0, 1, 0));
 Console.WriteLine(final);

using (writer = File.AppendText(@"C:\Users\test.txt"))
{
 writer.WriteLine(final);
}

}

 client.Disconnect();
                                    
}

 private static void WriteStream(string cmd, StreamWriter writer, ShellStream stream)
        {
            writer.WriteLine(cmd);
            while (stream.DataAvailable == false)
            {
                Thread.Sleep(1000);
            }

        }