Telnet module does not wait for remote script to return
I am trying to run a script on a remote system using the telnet module. The remote system runs the script but returns control immediately (or after the wait time set) to the telnet module. It does not wait for the script to terminate. Instead, I have to wait for the script to terminate. User needs to run other telnet commands in sequence, but only after this command has finished.
Why is this problem happening ? and how can I overcome this ? Please suggest your ideas regarding the same...
Re: Telnet module does not wait for remote script to return
If you are running a monitoring application it is often useful to simulate a telnet connection from your program to a remote machine using telnet, then issues one or a couple of commands and process output to generate alerts or status report.
the simplest way to accomplish this is to use Expect (or Perl Expect.pm) but you can use the CPAN module Net::Telnet too:
use Net::Telnet;
$t = Net::Telnet->new( Timeout => 10,
Prompt => '/%/',
Host => $hostname );
$t->login($username, $password);
@files = $t->cmd("ls");
$t->print("top");
(undef, $process_string) = $t->waitfor('/\d+ processes/');
$t->close;
Net::Telnet provides an interface to the telnet protocol. If has fake and generally unnecessary object-oriented flavor: you first need to create a connection with Net::Telnet->new, and then interact with the remote machine using method calls on the resulting object.
Re: Telnet module does not wait for remote script to return
The telnet module only waits for a fixed amout of time (determined by the wait time setting) for each command to run. It issues the subsequent command as soon as this wait time finishes. It does open streams for feedback from the server, but cannot detect the end of a script run.
Re: Telnet module does not wait for remote script to return
You can download and use Automize 5.3 or later. This version has a Telnet(Adv) module with advanced telnet capabilities ...
Re: Telnet module does not wait for remote script to return
Another important option is Prompt.
When you log in or run a command, Net::Telnet uses the Prompt pattern to determine when the login or command has completed.
The default Prompt is:
/[\$%#>] $/
which matches the common shell prompts. If the prompt on the remote machine doesn't match the default pattern, you have to specify your own. Remember to include the slashes.
Timeout lets you control how long (in seconds) network operations wait before they give up. The default is 10 seconds.
If an error or timeout occurs in the Net::Telnet module, the default behavior is to raise an exception, which, if uncaught, prints a message to STDERR and exits. To change this, pass a subroutine reference to new in the Errmode argument. If instead of a code subroutine, you specify the string "return" as the Errmode, methods return undef (in scalar context) or an empty list (in list context) on error, with the error message available via the errmsg method:
$telnet = Net::Telnet->new( Errmode => sub { main::log(@_) }, ... );
The login method is used to send a username and password to the remote machine. It uses the Prompt to decide when the login is complete and times out if the machine doesn't reply with a prompt:
$telnet->login($username, $password)
or die "Login failed: @{[ $telnet->errmsg() ]}\n";
To run a program and gather its output, use the cmd method. Pass it the string to send, and it returns the output of the command. In list context, it returns one line per list element. In scalar context, it returns one long line. It waits for the Prompt before returning.
You can separate the sending of the command from the reception of its output with the print and waitfor methods, as we do in the Solution. The waitfor method takes either a single string containing a Perl regular expression match operator:
$telnet->waitfor('/--more--/')
or named arguments. Timeout lets you specify a timeout to override the default, Match is a string containing a match operator as above, and String is a literal string to find:
$telnet->waitfor(String => 'greasy smoke', Timeout => 30)
In scalar context, waitfor returns true if the pattern or string was found. If it is not found, the Errmode action is performed. In list context, it returns two strings: all the text before the match, and the text that matched.