0 votes
by (330 points)

Hello, i have a question about the Scripting functionality.

Since bash 5.1, bracketed paste mode is now enabled by default:

2021-07-29 16:20:49.911 VERBOSE Ssh(1)[14] SSH: Received packet SSH_MSG_CHANNEL_DATA (17 bytes).
0000 |5E-00-00-00-00-00-00-00 08-1B-5B-3F-32-30-30-34| ^.........[?2004
0010 |68                                             | h
2021-07-29 16:20:49.911 VERBOSE Ssh(1)[14] SSH: Received packet SSH_MSG_CHANNEL_DATA (73 bytes).
0000 |5E-00-00-00-00-00-00-00 40-1B-5D-30-3B-61-6E-64| ^.......@.]0;and
0010 |72-65-79-40-75-62-75-6E 74-75-2D-73-65-72-76-65| rey@ubuntu-serve
0020 |72-2D-32-31-2D-30-34-3A 20-7E-07-61-6E-64-72-65| r-21-04: ~.andre
0030 |79-40-75-62-75-6E-74-75 2D-73-65-72-76-65-72-2D| y@ubuntu-server-
0040 |32-31-2D-30-34-3A-7E-24 20                     | 21-04:~$ 
2021-07-29 16:20:49.915 DEBUG Ssh(1)[12] Info: Received 72 bytes of data.
2021-07-29 16:20:49.915 VERBOSE Ssh(1)[12] Info: Received data: 
0000 |1B-5B-3F-32-30-30-34-68 1B-5D-30-3B-61-6E-64-72| .[?2004h.]0;andr
0010 |65-79-40-75-62-75-6E-74 75-2D-73-65-72-76-65-72| ey@ubuntu-server
0020 |2D-32-31-2D-30-34-3A-20 7E-07-61-6E-64-72-65-79| -21-04: ~.andrey
0030 |40-75-62-75-6E-74-75-2D 73-65-72-76-65-72-2D-32| @ubuntu-server-2
0040 |31-2D-30-34-3A-7E-24-20                        | 1-04:~$ 

For example, Ubuntu 21.04 contains pre-installed version of bash 5.1:

andrey@ubuntu-server-21-04:~$ dpkg -l | grep bash
ii  bash                                  5.1-2ubuntu1                                                         amd64        GNU Bourne Again SHell
ii  bash-completion                       1:2.11-2ubuntu1                                                      all          programmable completion for the bash shell
ii  command-not-found                     20.10.1                                                              all          Suggest installation of packages in interactive bash sessions
andrey@ubuntu-server-21-04:~$ bind -v | grep bracketed
set enable-bracketed-paste on
andrey@ubuntu-server-21-04:~$

When i use this code, i get a not entirely clean result.

using (var ssh = new Ssh())
{
    ssh.Connect(host);
    ssh.Login(user, password);
    var shell = ssh.StartScripting();
    shell.WaitFor(ScriptEvent.Duration(2000));//wait welcome text and first prompt string
    shell.Send(FunctionKey.Enter);
    shell.WaitFor(ScriptEvent.Duration(2000));//wait second prompt string
    var output = shell.ReceivedData;
    ssh.Disconnect();
}

My output:

\r\n\randrey@ubuntu-server-21-04:~$ 

I have an one additional unwanted CR symbol before the prompt string instead bracketed-paste control symbols or something else. I get the same result if i try to send a simple shell command:

shell.SendCommand("date");
shell.WaitFor(ScriptEvent.Duration(2000));//wait second prompt string

var output = shell.ReceivedData;

Output:

\rFri Jul 30 09:10:19 UTC 2021\r\nandrey@ubuntu-server-21-04:~$ 

Does the Scripting feature have internal functionality to get a cleaner reliable result or additional output control options for a given case?

How safe would it be to manually try to clear the output data from additional CR characters?

Thank you.

2 Answers

+1 vote
by (15.2k points)
selected by
 
Best answer

Hello,

I tried to reproduce the issue with bracketed paste mode on our server enabled before I answered to you.

thank you for the log. It shows what I was expecting.

2021-08-02 11:17:05.012 VERBOSE Ssh(1)[12] Info: Sent data:
0000 |64-61-74-65-0D | date.
2021-08-02 11:17:05.012 DEBUG VirtualTerminal(0)[12] Scripting: WaitFor(Line)
2021-08-02 11:17:05.012 VERBOSE VirtualTerminal(0)[12] Scripting: WaitFor: polling data for max 60000 ms.
2021-08-02 11:17:05.013 VERBOSE Ssh(1)[14] SSH: Received packet SSHMSGCHANNEL_DATA (24 bytes).
0000 |5E-00-00-00-00-00-00-00 0F-64-61-74-65-0D-0A-1B| ^........date...
0010 |5B-3F-32-30-30-34-6C-0D | [?2004l.
2021-08-02 11:17:05.013 VERBOSE VirtualTerminal(0)[12] Scripting: WaitFor: data received, reset last data received time stamp.
2021-08-02 11:17:05.013 VERBOSE VirtualTerminal(0)[12] Scripting: WaitFor: processing received data.
2021-08-02 11:17:05.013 DEBUG Ssh(1)[12] Info: Received 15 bytes of data.
2021-08-02 11:17:05.013 VERBOSE Ssh(1)[12] Info: Received data:
0000 |64-61-74-65-0D-0A-1B-5B 3F-32-30-30-34-6C-0D | date...[?2004l.

When your server echoes back the date command, it also sends \r\n (that is also part of that echo and it is expected), then it sends one of the escape sequence for bracketing mode (that is also expected and the escape sequence is stripped off of the ReceivedData response string) and at the end it sends \r which is that additional characted in your output. I beleive it is sent from your server to ensure good looking output in a terminal screen. After that it sends the command output.

Our shell.ReceiveData contains all text output from a server, stripped off of all excape sequences. So if the server decides to move cursor around the screen using escape sequences, these are not included in the output string, while moving cursor using \r and \n characters appear in the output string.

If you do not need whitespaces before and after a meaningful data, you can trim them safely yourself.

by (330 points)
Hello. Thank you for answers very much.

No more questions.
+1 vote
by (15.2k points)

Hello,

can you please share a verbose log for your code snippet?

shell.SendCommand("date");
shell.WaitFor(ScriptEvent.Duration(2000));//wait second prompt string

I would like to see sending of the command and what the server response look like.

I tried your code on our server (Ubuntu 18.04.4 LTS) and I got no additional \r on my responses. So it probably originates from your server to make sure that the cursor is in correct possition before printing an output.

by (330 points)
To reproduce the case you need:
1. Ubuntu 21.04 with bracketed paste mode enabled by default.
Or
2. Enable bracketed paste mode for your user in Ubuntu 18.04.4 LTS:
a. Create file .inputrc in home directory of the user if not exists.
b. Add line to the file:
      set enable-bracketed-paste on
c. Relogin
d. Check the mode is enabled:
      bind -v | grep bracketed

Full log for separate user - test
https://drive.google.com/file/d/1tpHfnBa7RXk3u0QLt2SVv7Pfqt15zhzP/view?usp=sharing
...