SimpleShell - SSH simple shell
Executes simple commands through an SSH shell.
This utility connects to the specified SSH server and initializes a shell session. Then it sends any command entered by the user to the server and reads the server respose line by line. Check out Scripting for more information.
This sample shows:
- Using Ssh and Scripting classes.
- Starting a shell session.
- Sending a command to the server.
- Reading a response from the server.
C#
// start scripting Scripting scripting = ssh.StartScripting(); // detect prompt automatically scripting.DetectPrompt(); // main loop ScriptMatch result; do { // output a prompt to a console Console.Write("Enter shell command: "); // read the command from a console string command = Console.ReadLine(); // send the command to the server scripting.SendCommand(command); // read the response line by line do { // read until a line is received (or prompt, or the connection is closed) string line = scripting.ReadUntil(ScriptEvent.Line, ScriptEvent.Prompt, ScriptEvent.Closed); // get the result of the ReadUntil method result = scripting.LastResult; // display the line Console.WriteLine(line); } while (result.IsLine); } while (!result.IsClosed);
VB.NET
' start scripting Dim scripting As Scripting = ssh.StartScripting() ' detect prompt automatically scripting.DetectPrompt() ' main loop Dim result As ScriptMatch Do ' output a prompt to a console Console.Write("Enter shell command: ") ' read the command from a console Dim command As String = Console.ReadLine() ' send the command to the server scripting.SendCommand(command) ' read the response line by line Do ' read until a line is received (or prompt, or the connection is closed) Dim line As String = scripting.ReadUntil(ScriptEvent.Line, ScriptEvent.Prompt, ScriptEvent.Closed) ' get the result of the ReadUntil method result = scripting.LastResult ' display the line Console.WriteLine(line) Loop While result.IsLine Loop While Not result.IsClosed