SSH: Escaping special characters in prompt string after DetectPrompt().

0 votes
asked Sep 24, 2014 by Melhior (260 points)
edited Sep 30, 2014

Hello.

I have some problems using DetectPrompt() method. My real prompt string is "[username@servername ~]$ " (without ").

Case 1:

var ssh = new Ssh();

ssh.Connect(servername);
ssh.Login(username, password);

Scripting scripting = ssh.StartScripting();

//Autodetecting
scripting.DetectPrompt();
//scripting.Prompt returns "regex:[username@servername ~][$] $" value (without ")

scripting.SendCommand("whoami");

string response = scripting.ReadUntilPrompt(); //freeze and timeout exception !!!

Case 2:

ssh.Connect(servername);
ssh.Login(username, password);

Scripting scripting = ssh.StartScripting();

//Manual setting value from Case 1.
scripting.Prompt="regex:[username@servername ~][$] $"

string response = scripting.ReadUntilPrompt(); //Freeze and timeout exception !!!

Case 3:

ssh.Connect(servername);
ssh.Login(username, password);

Scripting scripting = ssh.StartScripting();

//Setting value from Case 1, but manual escaping some characters.
scripting.Prompt="regex:\\[username@servername ~\\][\\$] $"

string response = scripting.ReadUntilPrompt(); //ALL GOOD!!! Response returns welcome message: "Last login ...".
commented Sep 24, 2014 by Melhior (260 points)
edited Sep 29, 2014

Case 4 (changing real prompt string to "servername:~ # "):

var ssh = new Ssh();

ssh.Connect(servername);
ssh.Login(username, password);

Scripting scripting = ssh.StartScripting();

//Autodetecting
scripting.DetectPrompt();
//scripting.Prompt returns "regex:servername:~ # $" value (without ")

scripting.SendCommand("whoami");

string response = scripting.ReadUntilPrompt(); //ALL GOOD !!!

5 Answers

+1 vote
answered Sep 24, 2014 by Pavel Matyska (10,840 points)
edited Sep 26, 2014
 
Best answer

Hello,

thank you for bringing this bug to our attention! I have added support for these prompt scenarios. You can download the hotfix here: download hotfix

commented Sep 26, 2014 by Pavel Matyska (10,840 points)
edited Sep 26, 2014

I have just fixed the hotfix download link.

0 votes
answered Sep 26, 2014 by Melhior (260 points)
edited Sep 26, 2014

Hello. Thanks for the hotfix! But now, i have some problems with assemblies while using new Rebex.Common.dll from this hotfix.

var ssh = new Ssh();

FileLoadException: Could not load file or assembly 'Rebex.Common, Version=2.0.5298.0, Culture=neutral, PublicKeyToken=1c4638788972655d' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040).

Hotfix net-4.0:

  1. Rebex.Common 2.0.5381.1
  2. Rebex.Time 3.0.5381.1

My current installed trial version net-4.0:

  1. Rebex.Common 2.0.5298.0
  2. Rebex.Networking 3.0.5298.0
  3. Rebex.SshShell 1.0.5298.0
  4. Rebex.Terminal 1.0.5298.0
commented Sep 26, 2014 by Pavel Matyska (10,840 points)
edited Sep 26, 2014

Hello,

please remove all references to Rebex dll's from your project and add them again targeting the hotfix assemblies.

commented Sep 26, 2014 by Tomas Knopp (58,890 points)
edited Sep 26, 2014

Sorry, there was a mistake on our side. We will post a Virtual Terminal hotfix here within minutes for you to download.

commented Sep 26, 2014 by Pavel Matyska (10,840 points)
edited Sep 26, 2014
commented Sep 26, 2014 by Melhior (260 points)
edited Sep 26, 2014

The last hotfix works perfectly! Thank you very much.

0 votes
answered Sep 29, 2014 by Melhior (260 points)
edited Sep 29, 2014

Hello, i have new problem using DetectPrompt() method.

I modified the file /home/username/.bashrc on my linux system.

I added next strings:

sleep 10 seconds

sleep 10

print some text after standard welcome message, for example:

Last login: Mon Sep 29 10:54:19 2014 from 192.168.0.21

test

echo "test"

Case 1:

var ssh = new Ssh();

ssh.Timeout = 200000; //200 seconds

ssh.Connect(servername);
ssh.Login(username, password);

Scripting scripting = ssh.StartScripting();

//scripting.Timeout have default value: 60000
scripting.DetectPrompt();

The ssh timeout value > the scripting timeout value > the script sleep value. I got:

Rebex.TerminalEmulationException: Unable to detect prompt

in Rebex.TerminalEmulation.Scripting.KC()
in Rebex.TerminalEmulation.Scripting.DetectPrompt()

Case 2 (script sleep value modified to 3 seconds):

var ssh = new Ssh();

ssh.Timeout = 1000; //1 second

ssh.Connect(servername);
ssh.Login(username, password);

Scripting scripting = ssh.StartScripting();
scripting.Timeout = 1000
scripting.DetectPrompt(); // WORKS !!!

Is it correct?

+1 vote
answered Sep 29, 2014 by Pavel Matyska (10,840 points)
edited Sep 29, 2014

Hello, Yes, it is correct. Scripting.DetectPrompt() tries to read some input in short chunks of time and when nothing is read, it throws this exception. If you need to wait before Scripting.DetectPrompt() you can use

// wait for 10 seconds
scripting.WaitFor(ScriptEvent.Delay(10 * 1000));

// now try to detect prompt
scripting.DetectPrompt();

or if the 'sleep' time is not known but you know there is some, you can try to wait for first character and then try to detect prompt

// wait for first character
// this will timeout after Scripting.Timeout period of time
scripting.WaitFor(ScriptEvent.AnyText);

// now try to detect prompt
scripting.DetectPrompt();
+1 vote
answered Sep 30, 2014 by Pavel Matyska (10,840 points)
edited Sep 30, 2014

Hello, yes, it is correct. As I said before, the DetectPrompt() method try to read from the server in short chunks of time. None of these read attempts does timeout because the chunks are smaller then 1 second. DetectPrompt is in fact small script using Scripting API and the Scipting.Timeout property affects its parts, not the whole method.

commented Sep 30, 2014 by Melhior (260 points)
edited Sep 30, 2014

Thanks for answers.

...