The Shell
object is intended to execute simple commands that don't need a pseudo terminal, which is not even requested from the server in ShellMode.WellKnownShell
mode. This means that Shell
object can't be used to run commands such as top
(or mc
, aptitude
, etc.)
Instead, use VirtualTerminal
or TerminalControl
objects - both provide a virtual terminal, making them behave like a common SSH client (such as PuTTY). For example, to execute the top
command with VirtualTerminal
and display its output, the following code might be used:
Ssh client = new Ssh();
client.Connect("192.168.150.129");
// authenticate
client.Login("reza", "1");
// start virtual termina (80x25)
int columns = 80;
int rows = 25;
VirtualTerminal terminal = client.StartVirtualTerminal(null, columns, rows);
// send 'top' command to the server
terminal.SendToServer("top\n");
// the 'top' command will run forever until terminated (by 'q' key, for example)
while (true)
{
// process any available incoming data for 2 seconds
TerminalState state = terminal.Process(2000);
// break if disconnected
if (state == TerminalState.Disconnected)
break;
if (state == TerminalState.DataReceived)
{
// display the current terminal screen content
string[] lines = terminal.Screen.GetRegionText(0, 0, columns, rows);
string result = string.Join("\n", lines);
Debug.WriteLine(result);
}
}