How to wait for scripting response till prompt

0 votes
asked May 9 by Aafrenn (120 points)

As we are trying to execute scripts with some input parameters and depending on input parameters execution time vary. So sometimes it takes 10 min and sometimes it takes more than 20 min, because of this we cant assume the actual time for the execution and we are facing time out exception. Please help me with this.

1 Answer

0 votes
answered May 9 by Pavel Matyska (11,540 points)

Hello,

it depends on how the server responds when executing your command. If the server print out some output while executing the command, you can use our Scripting API and use Scripting.WaitFor(ScriptEvent.Prompt) method accordingly. Please see our Scripting feature pages to read more detail about it and how to use it.
If the server is "silent" when executing your command, you have to set Ssh.Timeout to a greater value than the greatest expected execution time or set it to -1 to not timeout it at all. Then you have to use combination of ScriptEvent.Duration and ScriptEvent.Promp and periodically check which one is matched. The ScriptEvent.Duration is necessary in this scenario because the server send no data and the Scripting API itself can time out when it process no data for a long period of time.

If you are not familiar with our Scripting API, please read the feature pages I mentioned above. If this answer is not enough for you, please provide more information about the issue to us.

Thank you.

commented May 9 by Aafrenn (120 points)
Thank you for a quick response!


Yes, we are executing the silent script, below is the example code we are using.


 using (var ssh = new Rebex.Net.Ssh())
            {
                    ssh.Connect(strIPAddress);
                    ssh.Login(strUsername, strPassword);
                    Scripting scripting = ssh.StartScripting();
                    scripting.Timeout = 40 * 60 * 1000;
                    
                    scripting.SendCommand(command);
                    string response = scripting.ReadUntil("#");
}

Still, we are facing timing exception. Please help me to write better code.
commented May 9 by Pavel Matyska (11,540 points)
Hello, here is your code modified according to my previous answer. Please use it as a starting point for your new code as you know what needs to be done, not as a complete solution.

using (var ssh = new Rebex.Net.Ssh())
{
    ssh.Connect(strIPAddress);
    ssh.Login(strUsername, strPassword);

    // if ssh connection time out after 40 minutes, this timeout should be raised or set to -1
    ssh.Timeout = 40 * 60 * 1000;

    Scripting scripting = ssh.StartScripting();

    // set prompt to enable using ScriptEvent.Prompt
    scripting.Prompt = "#";

    // execute long running silent command
    scripting.SendCommand(command);

    // 30 seconds chunk
    ScriptEvent workInProgress = ScriptEvent.Duration(30 * 1000);
    ScriptMatch match;

    do
    {
        // try to receive prompt in 30 seconds chunks, so the scripting should not time out
        match = scripting.WaitFor(workInProgress, ScriptEvent.Prompt);
    } while (!match.IsPrompt);

    // now the command is finished and prompt should have been received

    // ... do some additional work ...

    scripting.Close();
    ssh.Disconnect();
}
commented May 13 by Aafrenn (120 points)
Thank you for your help!

Thing is working file without an issue but except the zip command. This command is taking more than 20 min to complete the process but we are getting a response before completion of zip command. Please help me out with this issue.

Thank!
commented May 13 by Pavel Matyska (11,540 points)
You can use the same approach as before. If your command takes more time than 20 minutes and your ssh.Timeout is set to 40 minutes, it should be fine.
You should have set your ssh.Timeout to greater value than your expected longest processing time.
Scripting.Timeout is used when scripting operation takes too much time such as waiting for a particular text in a long output. Ssh.Timeout is used as SSH connection timeout such as no data appear on the connection for that long.
...