Use Scripting class in both interactive and script mode

0 votes
asked Jan 24 by ioan.crisan (340 points)

Here is my scenario:
I need to build a web UI displaying a terminal console over a SSH connection. The terminal console has an option to run and debug a shell script, as well as providing an interactive console.

  • In interactive mode: after a successful connection, the end user may issue freely commands against the shell (SendKey() + Process(timeout) loop).
  • In run mode: the script is executed command by command (SendCommand(cmd) and then ReadUntil(event)).
  • In debug mode: the script is executed also command by command but, between two successive commands, the end user may manually issue other commands. Here is where I'm starting to have problems (I suppose) due to the combination of ReadUntil() and Process(), specifically an System.InvalidOperationException: Another operation is pending.

Do you have a suggestion, or some kind of sample link, about how to safely achieve the debug mode scenario? Thanks!

Note: the whole logic occurs on the server using virtual terminals, client side communication is not relevant for this problem.

1 Answer

0 votes
answered Jan 24 by Lukas Matyska (52,730 points)
selected Jan 28 by ioan.crisan
 
Best answer

Yes, it is very probably caused by mixing ReadUntil() and Process(). Those cannot be called simultaneously.

My suggestion is this:

  • Choose two special keys, e.g. F1, F2, for two cases: stop automatic processing and start automatic processing.
  • When F1 key is pressed, wait for ReadUntil(event) is finished = current command is finished. Then allow the user to interact (use Process(timeout) loop).
  • Once the user hits F2, wait for Process(timeout) is finished and continue processing next commands. I suggest you to wait until prompt appear on cursor position (try to use DetectPrompt() method) - if this is not done, automatic processing of next command can fail.

  • When you are processing a command, disable (or buffer) user input and handle only F1.

  • Alternatively, you don't need the F1 key. Just stop automatic processing when any key is pressed.

  • I think, you need some signal from user that application should continue processing commands. It can be a button or shortcut instead of F2 in case you need F2 as valid terminal input.
...