How to catch error (or exit) when Receive hang - WeOnlyDo Discussion board

How to catch error (or exit) when Receive hang (wodSSH / wodSSH.NET)

by novanstar, Tuesday, November 04, 2008, 02:57 (5865 days ago)

Dear support,
While working on a customized waitfor function, the script hang when there is no more result for Receive(). How can I resolve this issue?

==== code in Perl ====
for (;;) {
$buffer .= $ssh->Receive();
last if ($buffer =~ /match something/);
last if ((time - $time_start) > $timeout);
}
======================

If there is nothing matched, I expect it will break the loop after timeout. But it never breaks the loop because it got hang at Receive.

Thanks,
novanstar

Re: How to catch error (or exit) when Receive hang

by wodDamir, Tuesday, November 04, 2008, 09:00 (5864 days ago) @ novanstar

Hi,

You need to ignore the error in order to continue executing the code regardless of the error. You can do that using try...Catch block, like this:

[code]Try
{
ssh1.Receive();
}
Catch(Exception ex)
{
//do what you need in case of timeout
}[/code]

Hope this helps.

Regards,
Damba

Re: How to catch error (or exit) when Receive hang

by novanstar, Tuesday, November 04, 2008, 20:19 (5864 days ago) @ wodDamir

Hi Damba,
It doesn't work! I have tried the try/catch Perl equivalent (eval) and also tried with Perl Error module (try {} catch Error with {};) both got stuck at Receive . Here is code block using eval: When the count reaches 4, it got stuck, so I believe it's because of Receive , not the for loop.

Thanks,

==== code ====
eval {
for (my $i = 1; $i<=20; $i++) {
print Count: $i

;
$buffer .= $ssh->Receive();
print $buffer

;
}
};

if ($@) {
print Error!!!

;
sleep 5;
$ssh->Disconnect();
return $buffer;
}
=================

Re: How to catch error (or exit) when Receive hang

by woddrazen, Tuesday, November 04, 2008, 20:25 (5864 days ago) @ novanstar

Hi,


Can you maybe show us your full code? I want to see how you execute commands.

Also please remove Sleep API from you code. Sleep API blocks same thread where wodSSH lives in.


Drazen

Re: How to catch error (or exit) when Receive hang

by novanstar, Tuesday, November 04, 2008, 21:07 (5864 days ago) @ woddrazen

Hi,
Here is the full code: Thanks,

===== code ===== (code hang when it print out Count: 4 )
my $ssh = Win32::OLE->new( WeOnlyDo.wodSSHCom.1 );
my $buffer = q{};
$ssh->{Protocol} = 4;
$ssh->{HostName} = 'host01';
$ssh->{Blocking} = 'True';
$ssh->{Login} = 'user';
$ssh->{Password} = 'password';
$ssh->Connect();
$ssh->WaitFor('user@host01:~$ ');
$ssh->Send( command open tty://\n );

ssh->WaitFor('root@devict:/$ ', 10);

print Start to send command... ;
$ssh->Send( command for device\n );

eval {
for (my $i = 1; $i<=20; $i++) {
print Count: $i ;

$buffer .= $ssh->Receive();
print $buffer ;
}
};
if ($@) {
print Error!!!

;
$ssh->Disconnect();
return $buffer;
}

=======================
Alternative to eval{}:
try {
for (my $i = 1; $i<=20; $i++) {
print Count: $i ;
$buffer .= $ssh->Receive();
print $buffer ;
}
}
catch Error with {
print Error!!!

;
$ssh->Disconnect();
return $buffer;
}

Re: How to catch error (or exit) when Receive hang

by woddrazen, Tuesday, November 04, 2008, 23:05 (5864 days ago) @ novanstar

Hi,


Can you please try something like this:
[code]
...
$ssh->{Timeout} = 5;
$ssh->Connect();

$ssh->WaitFor('weonlydo@linux:~$');
$ssh->Send( ls -al\n );

use Win32::OLE;
$Win32::OLE::Warn = 3;

do {
print $ssh->Receive();
} while ($@==0);[/code]
I'm not Perl guru. This worked for me but I receive in output error also.


Drazen

Re: How to catch error (or exit) when Receive hang

by novanstar, Tuesday, November 04, 2008, 23:56 (5864 days ago) @ woddrazen

Hi,


Can you please try something like this:
[code]
...
$ssh->{Timeout} = 5;
$ssh->Connect();

$ssh->WaitFor('weonlydo@linux:~$');
$ssh->Send( ls -al\n );

use Win32::OLE;
$Win32::OLE::Warn = 3;

do {
print $ssh->Receive();
} while ($@==0);[/code]
I'm not Perl guru. This worked for me but I receive in output error also.


Drazen

Hi Drazen,
The suggested code (actually the timeout property) only resolve Receive hang issue. But it also exits the script after timeout, which doesn't really fix the problem. Here is the screen output:

===== output =========
OLE exception from WeOnlyDo.wodSSHCom.1 :

The current connection has timeout.

Win32::OLE(0.1707) error 0x800a05b4
in METHOD/PROPERTYGET Receive at ....
======================

The purpose of this customized waitfor is to capture all buffered resposne from server after issuing a command which won't return to command prompt and don't have specific pattern to match. It takes CTRL+C to break the command.

What else method you would suggest for this purpose?

Regards,
novanstar

Re: How to catch error (or exit) when Receive hang

by woddrazen, Wednesday, November 05, 2008, 00:03 (5864 days ago) @ novanstar

Hi,


You can try with Execute Method. Something like this:
[code]$ssh->WaitFor('weonlydo@linux:~$');
$ssh->{DataReady} = 0;
print $ssh->Execute( ls -al\n ,'weonlydo@linux:~$');
$ssh->Disconnect();[/code]

Drazen

Re: How to catch error (or exit) when Receive hang

by novanstar, Wednesday, November 05, 2008, 00:46 (5864 days ago) @ woddrazen

Hi,


You can try with Execute Method. Something like this:
[code]$ssh->WaitFor('weonlydo@linux:~$');
$ssh->{DataReady} = 0;
print $ssh->Execute( ls -al\n ,'weonlydo@linux:~$');
$ssh->Disconnect();[/code]

Drazen

Hi Drazen,
In my case, I don't have any pattern to match (even command prompt because the command I issue won't return to command prompt). So when I tried per your suggestion, the Execute give me an exception as follows:

======== exception ==========
The current connection has timeout.

Win32::OLE(0.1707) error 0x800a05b4
in METHOD/PROPERTYGET Execute at...
=============================

I am still unable to receive the buffered data from server after the command...

Regards,
novanstar

Re: How to catch error (or exit) when Receive hang

by woddrazen, Wednesday, November 05, 2008, 09:24 (5863 days ago) @ novanstar

Novanstar,


Is there any chance that we could connect there and duplicate your problem?

You can send your private information to:

techsupport@weonlydo.com


Drazen

Re: How to catch error (or exit) when Receive hang

by novanstar, Wednesday, November 05, 2008, 19:05 (5863 days ago) @ woddrazen

Novanstar,


Is there any chance that we could connect there and duplicate your problem?

You can send your private information to:

techsupport@weonlydo.com


Drazen

Hi Drazen,
I am afraid that I am not supposed to expose our under development product. It's a Linux OS-based device. The current issue I encounter is that I need to run some command that won't return to command prompt (something like monitoring or running iperf in server mode, also there is no specific pattern to wait for) and then get all the buffered data from server after timeout. With wodSSH's supported capability, what command combination is best for this scenario? I am evaluating wodSSH and thinking to use it for our test automation.

Thanks,
novanstar

Re: How to catch error (or exit) when Receive hang

by wodDamir, Wednesday, November 05, 2008, 19:20 (5863 days ago) @ novanstar

Novanstar,

But you can use WaitFor method just for that purpose. Let's say that you start iperf on linux OS. It will wait until aborted by CTRL-C .

In that case, you can specify any prompt in WaitFor call, which will ofcourse timeout. But when that happens, you simply catch the error and call Receive method (or Peek) which will receive the data that was left in buffer (since WaitFor won't remove it).

When data is received, the code will proceed, and now you can send CTRL-C , which proceeds with accepting prompt.

Ofcourse, in this case, a global Timeout property has to be set to a value higher then the timeout parameter in WaitFor call.

Hope this helps.

Regards,
Damba

Re: How to catch error (or exit) when Receive hang

by novanstar, Thursday, November 06, 2008, 19:39 (5862 days ago) @ wodDamir

Novanstar,

But you can use WaitFor method just for that purpose. Let's say that you start iperf on linux OS. It will wait until aborted by CTRL-C .

In that case, you can specify any prompt in WaitFor call, which will ofcourse timeout. But when that happens, you simply catch the error and call Receive method (or Peek) which will receive the data that was left in buffer (since WaitFor won't remove it).

When data is received, the code will proceed, and now you can send CTRL-C , which proceeds with accepting prompt.

Ofcourse, in this case, a global Timeout property has to be set to a value higher then the timeout parameter in WaitFor call.

Hope this helps.

Regards,
Damba

Dear WEONLYDO support team,
That works! Thanks for all your great and instant support. I will proceed to place an order of this software. :)

The only thing left is that if there is any way I can supress the error printout in Perl:
===== error msg =====
OLE exception from WeOnlyDo.wodSSHCom.1 :
The current connection has timeout.

Win32::OLE(0.1707) error 0x800a05b4
in METHOD/PROPERTYGET WaitFor ....
=====================

Thanks,
novanstar