SSH Expect Example

Thursday, 19 February 2009 05:02 by Tamir

The following sample shows how to use expect concept with the SshShell class of the SharpSSH library. This sample is also available in the SharpSSH examples found inside the source package zip file.

SshExpectTest.cs

SshConnectionInfo input = Util.GetInput();
SshShell ssh = new SshShell(input.Host, input.User);
if(input.Pass != null) ssh.Password = input.Pass;
if(input.IdentityFile != null) ssh.AddIdentityFile( input.IdentityFile );

Console.Write("Connecting...");
ssh.Connect();
Console.WriteLine("OK");


Console.Write("Enter a pattern to expect in response [e.g. '#', '$', C:\\\\.*>, etc...]: ");
string pattern = Console.ReadLine();

ssh.ExpectPattern = pattern;
ssh.RemoveTerminalEmulationCharacters = true;

Console.WriteLine();
Console.WriteLine( ssh.Expect( pattern ) );

while(ssh.ShellOpened)
{    
    Console.WriteLine();
    Console.Write("Enter some data to write ['Enter' to cancel]: ");
    string data = Console.ReadLine();
    if(data=="")break;
    ssh.WriteLine(data);

    string output = ssh.Expect( pattern );
    Console.WriteLine( output );
}

Console.Write("Disconnecting...");
ssh.Close();
Console.WriteLine("OK");

Currently rated 3.1 by 83 people

  • Currently 3.07229/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:  
Categories:   SharpSSH
Actions:   E-mail | Permalink | Comments (86) | Comment RSSRSS comment feed

Comments

Comments are closed