Thank you for the verbose log. It showed what you were doing and what the server replied. The situation is following:
1. The DetectPrompt() method tries to determine current prompt by sending ENTER and comparing two last line. If the lines are equal, the string before cursor is taken as prompt. This is working for you.
2. The WaitFor(Prompt) method reads data from server and returns when currently set prompt is found. This would work for you if the command not changed the displayed prompt.
After connect, your prompt is "[personalUser@r03-n04 ~]$ " (it is successfully detected).
When you execute "sudo -u serviceUser bash" the prompt is changed to "bash-4.2$ ".
When you call DetectPrompt() the prompt is successfully detected to "bash-4.2$ ".
When you execute "exit" the bash is terminated and original prompt is displayed, which is "[personalUser@r03-n04 ~]$ ".
From the knowledge above, I suggest you this solution:
// detect current prompt
scripting.DetectPrompt();
// save original prompt
string originalPrompt = scripting.Prompt;
// run new bash
scripting.SendCommand("sudo -u serviceUser bash");
// wait for prompt or no data are received for 5 seconds
var m = scripting.WaitFor(ScriptEvent.Prompt, ScriptEvent.Delay(5 * 1000));
if (m.IsPrompt)
{
// starting new bash failed -> display error (received data)
Console.WriteLine(scripting.ReceivedData);
return;
}
else
{
// no data for 5 seconds -> it seems new bash was started
// detect prompt of new bash
scripting.DetectPrompt();
}
// execute commands in new bash
//scripting.SendCommand("...");
// wait for responses as usual
//scripting.WaitFor(ScriptEvent.Prompt);
// exit new bash
scripting.SendCommand("exit");
// set original prompt first
scripting.Prompt = originalPrompt;
scripting.WaitFor(ScriptEvent.Prompt);
// execute commands in original bash or finish...