0 votes
by (140 points)

Hello,

I am using below code but its not working can you please tell me whats wrong In this

scripting.Prompt = "$ ";
scripting.Timeout = 10000;
scripting.SendCommand("ls -ltr");
string response = scripting.ReadUntil(ScriptEvent.Prompt);

Otherwise give me sample code to execute my command on server.

1 Answer

0 votes
by (15.2k points)

Hello,

What protocol are you using? SSH or Telnet?
What do you mean by "it is not working"? Do you get timeout exception? Your sample code actually works fine on my SSH server, so I can only assume that the actual prompt does not end with "$ " string. You can try scripting.DetectPrompt(); instead of scripting.Prompt = "$ ";. Or, the server is not in the state you expected when you called this sample code.

by (140 points)
I am using SSH, Suppose as per your suggestion if I had used scripting.DetectPrompt(); In that case it throws error if it dosent get prompt like i can give you one example in linux SCP command it will ask for password and which is prompt less.
Thats why I am not using scripting.DetectPrompt();
by (15.2k points)
Well, if the server asks for a password, you should read until "password" (or what the question is) string appears on the screen, not until a prompt. The Scripting.Prompt property is useful in a scenario when you are connected and logged in a shell (i.e bash on a  linux server) and a shell gives you that prompt as an indication that the server waits for an input. If the server ask for something else before sending first prompt, you should handle that question separately and cannot read until prompt.

I try to demonstrate it in the sample code below:

using (Ssh ssh = new Ssh())
{
    ssh.Connect(hostname);
    ssh.Login(user, password);

    Scripting scripting = ssh.StartScripting();

    scripting.Prompt = "$ ";
    scripting.Timeout = 10000;

    // assume that the server asks for a question:
    // "Do you want to change your password? (Y/N)"
    // before it sends a prompt and
    // pressing "N" without the enter handles this question
    scripting.ReadUntil("password? (Y/N)");
    scripting.Send("N");

    // after the question we know that the server sends a prompt
    // which ends with "$ "
    scripting.ReadUntilPrompt();

    // do work as usual
    scripting.SendCommand("ls -ltr");
    string response = scripting.ReadUntil(ScriptEvent.Prompt);

    // ...

    ssh.Disconnect();
}
by (140 points)
Thanks for your feedback and I agree with your answer but see I had just given example of password so instead of this think globally , I dnt know what word will come and i cant use anytext property too , Instead of that just give me code for custom prompts with waitfor so that i can take output of that command also and prompts required are $,#,> thats It.
by (70.2k points)
Hmm... Globally, if you don't know what the server is asking, you shouldn't process further.

Let suppose this server question after connect:

"This server is not secured. Do you want to continue? (Y/N): "
or other server:
"This server is not secured. Do you want to quit? (Y/N): "


If you don't know what the server is asking, you cannot handle this automatically.
The solutions can be:

1. Ask the user to reply.
2. Inform user that unknown response from server was received and the program is exiting.


You can do it like this:

    var script = client.StartScripting();
    script.Prompt = "regex:[$#>] "; // matches prompts ended with $,#,>
    script.Timeout = 10 * 1000; // 10 seconds

    var seLogin = new Regex(@"(login:)|(username:)", RegexOptions.IgnoreCase);
    var seChangePassword = new Regex(@"change.*password.*(Y/N)", RegexOptions.IgnoreCase);

    var match = script.WaitFor(seLogin, seChangePassword, ScriptEvent.Prompt, ScriptEvent.Timeout);
    if (match.IsEventMatched(seChangePassword))
    {
        // handle change password
    }
    else if (match.IsEventMatched(seLogin))
    {
        // handle login request
    }
    else if (match.IsPrompt)
    {
        // prompt was received
        // do nothing and continue...
    }
    else
    {
        // handle unexpected server response
        // ask user or quit
    }

    script.SendCommand("ls -ltr");
    string response = script.ReadUntil(ScriptEvent.Prompt);
...