0 votes
by (150 points)

Capture SSH virtual terminal as byte array OR image.

Below is the code which I am trying to achieve the requirement (using Save method of VirtualTerminal class).

When I execute the below code I get the exception as Image
saving is supported on Windows via Rebex.Terminal.Control assembly.

private void btnConnect_Click(object sender, EventArgs e)
{
    Ssh lsshClient = new Ssh();
    lsshClient.Timeout = 10000;
    lsshClient.Settings.SshParameters.MinimumDiffieHellmanKeySize = GlobalConfig.MinimumDiffieHellmanKeySize;
    lsshClient.Settings.SshParameters.AuthenticationMethods = Rebex.Net.SshAuthenticationMethod.Any;
    lsshClient.Connect(“Server”, 22);
    if (lsshClient.IsConnected == true)
    {
        lsshClient.Login("AccountName", "AccountPassword");
        VirtualTerminal terminal = new VirtualTerminal(120, 50);
        terminal.Scripting.HoldReceivedData = true;
        terminal.Scripting.AutoMatchPrompt = true;
        terminal.Scripting.TrimReadUntilResponse = true;
        terminal.Scripting.Timeout = 6000;
        terminal.Scripting.Prompt = @"#";
        terminal.Bind(lsshClient);
        try
        {
            Scripting tmpScripting = terminal.Scripting;
            tmpScripting.Send("bash\r");
            tmpScripting.Send("ls -l\r");
            tmpScripting.WaitFor(ScriptEvent.AnyText);
            String tmpReadUntil = tmpScripting.ReadLine();

            tmpReadUntil = tmpScripting.ReadUntil(ScriptEvent.AnyText);
            byte[] CurrentImage = GetTerminalImage(terminal);
        }
        catch (Exception Ex)
        {
            terminal.Unbind();
            terminal.Dispose();
            lsshClient.Disconnect();
        }
    }
}



public static byte[] GetTerminalImage(VirtualTerminal pVirtualTerminal)
{
    MemoryStream TerminalSaveOutputStream = new MemoryStream();
    pVirtualTerminal.Save(TerminalSaveOutputStream, TerminalCaptureFormat.Png);
    return TerminalSaveOutputStream.ToArray();
}

1 Answer

0 votes
by (149k points)
selected by
 
Best answer

To resolve the problem, add reference to Rebex.Terminal.Control.dll assembly to your project, and replace "TerminalCaptureFormat" with "TerminalImageFormat" in the GetTerminalImage method:

public static byte[] GetTerminalImage(VirtualTerminal pVirtualTerminal)
{
    MemoryStream TerminalSaveOutputStream = new MemoryStream();
    pVirtualTerminal.Save(TerminalSaveOutputStream, TerminalImageFormat.Png);
    return TerminalSaveOutputStream.ToArray();
}
...