0 votes
by (120 points)

Telnet client = new Telnet("Host");
var terminal = client.StartVirtualTerminal();

  1. How to Read the current terminal screen text without terminator?
    Means I need to all screen text.

  2. How to send "Delete" key to command?

  3. Getting connection closed frequently, how to fix that?

1 Answer

0 votes
by (73.5k points)
  1. Read content of the terminal screen like this:

    string[] lines = terminal.Screen.GetRegionText(0, 0, terminal.Screen.Columns, terminal.Screen.Rows);
    
  2. Send Delete or Backspace keys like this:

    terminal.Scripting.Send(FunctionKey.Delete);
    terminal.Scripting.Send(FunctionKey.Backspace);
    
  3. First diagnose the cause of the closed connections. When you know the cause, we can help you to solve it.

  • Is the connection closed by the client?
  • Is the connection closed by the server?
  • At which point is the connection closed?
    -- When the client issues some command?
    -- When the client is inactive for X seconds?

    You can set the logger to see what is going on. It can be done like this:

    client.LogWriter = new FileLogWriter("c:/data/telnet.log", LogLevel.Debug);
    
by (120 points)
I have tried these keys , but it seems nothing happened.
Kindly advise.
terminal.Scripting.Send(FunctionKey.Delete);
terminal.Scripting.Send(FunctionKey.Backspace);
by (73.5k points)
Please note that the Send() method only sends data.

To display changes on the terminal screen, you have to process incoming data, typically by calling one of scripting.ReadUntil(), scripting.WaitFor(), scripting.Process() methods.

Also please note, that you have to call terminal.Screen.GetRegionText() every time you want to get updated data.

If you are in doubt, please create communication log to see what is going on:

  client.LogWriter = new FileLogWriter("c:/data/telnet.log", LogLevel.Debug);
by (120 points)
Thank You !, Noticed,
terminal.Scripting.Send(FunctionKey.Backspace);
this one working as "Left Arrow Key", Just moving Curser, Not deleting.

Could you help.
by (73.5k points)
Please note that reaction to function keys depends on the server.
However, I don't think the Backspace triggered the "Left Arrow Key". I guess that the server sent <BACKSPACE> <SPACE> sequence (0x08 0x20) - which is  standard way how to delete one character on the terminal screen. You can check the communication log in VERBOSE level, to see raw data send/received from the server.

It seem you are doing something else than what you need.
Can you please describe what do you want to achieve?
by (120 points)
My Requirement is in my terminal screen Need to delete 4 characters from the curser.
I have checked in Terminal Screen manually we need to send Backspace key four times for deleting 4 chars.

But I tried same here, it not deleting chars just moving curser to left while send BackSpace key.
by (73.5k points)
Please create the VERBOSE communication log and post it here or send it to support@rebex.net for analysis. It can be done like this:

client.LogWriter = new FileLogWriter("c:/data/telnet.log", LogLevel.Verbose);

I have tested this on my telnet connection:

    scripting.Send("echo abcd");
    scripting.Send(FunctionKey.Backspace);
    scripting.Send(FunctionKey.Backspace);
    scripting.Send(FunctionKey.Backspace);
    scripting.Send(FunctionKey.Enter);

I got the expected output:

$ echo a
a
$

Corresponding VERBOSE log entries looks like this:

VERBOSE Telnet(1)[1] Info: Sent data:
 0000 |65-63-68-6F-20-61-62-63 64                     | echo abcd
 0000 |08                                             | .
 0000 |08                                             | .
 0000 |08                                             | .
 0000 |0D                                             | .
VERBOSE Telnet(1)[1] Info: Received data:
 0000 |65                                             | e
 0000 |63-68-6F-20-61-62-63-64 08-1B-5B-4B-08-1B-5B-4B| cho abcd..[K..[K
 0010 |08-1B-5B-4B-0D-0A-61-0D 0A-24-20               | ..[K..a..$
...