Results 1 to 5 of 5

Thread: Telnet module does not wait for remote script to return

  1. #1
    Join Date
    Feb 2009
    Posts
    38

    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...

  2. #2
    Join Date
    Apr 2008
    Posts
    3,339

    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.

  3. #3
    Join Date
    Feb 2008
    Posts
    2,635

    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.

  4. #4
    Join Date
    Jan 2006
    Posts
    3,792

    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 ...

  5. #5
    Join Date
    Jan 2006
    Posts
    4,221

    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.

Similar Threads

  1. How to get Argosy HV335T remote codes in telnet console?
    By Sumeet Vishwakarma in forum Portable Devices
    Replies: 6
    Last Post: 21-03-2012, 08:16 PM
  2. Replies: 10
    Last Post: 11-02-2011, 12:15 AM
  3. Remote management module 3 is not responding on HTTP
    By Tarank in forum Networking & Security
    Replies: 5
    Last Post: 28-10-2010, 12:26 AM
  4. Linux bash script for telnet connection
    By Rubero in forum Software Development
    Replies: 5
    Last Post: 23-08-2010, 09:08 PM
  5. Connecting to a remote computer using TELNET
    By Erubiel in forum Networking & Security
    Replies: 3
    Last Post: 28-11-2009, 12:58 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,896,196.76904 seconds with 16 queries