Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have a problem using the Net::Ftp and Net::Telnet. When i telnet to some other machine and run a perl script there, how would i know the status of the perl script. The perl scirpt which is running there(in the remote machine) has error handling and i need to know the messages that it outputs in the remote machine to the present machine from where i am doing the telnet. Is it possible and if yes then how. Regards, live
  • Comment on how to get back a exit status using Net::Telnet

Replies are listed 'Best First'.
Re: how to get back a exit status using Net::Telnet
by saintmike (Vicar) on Jan 17, 2005 at 07:37 UTC
    Regarding Net::FTP, you can't run a perl script on the remote server, but you can certainly apply error checking to any Net::FTP method you're calling (from the Net::FTP manpage):
    $ftp->cwd("/pub") or die "Cannot change working directory ", $ftp->message;
    Regarding Net::Telnet, a command executed on the remote side will return its output lines (from the Net::Telnet manpage):
    @lines = $t->cmd("who");
    If your script on the remote side prints messages when it encounters an error, just check if @lines matches this pattern. Additionally, you could add echo $? to have the remote side print the exit code of the remote command/script right after it's done:
    @lines = $t->cmd('who; echo status=$?');
    Then grep for /status=\d+/ in @lines.
      I tend to pop() off, for example:
      @list = $t->cmd('who; echo $?'); $return_code_scalar=pop(@list);
      rather than grep for a pattern in the output list.
      Squibbie Pooh Ski Doo.

      Actually FTP supports running commands on remote side, check RFCs, and that was one of the earliest usage scenarios for FTP.

Re: how to get back a exit status using Net::Telnet
by belden (Friar) on Jan 18, 2005 at 00:31 UTC

    If you're logging in to a Unix(-like) machine, you can ask the shell the exit status of your last command. If you are using csh, then you'll need to echo $status. If it's bash, ksh, or sh, then echo $?. If those don't work, then you can always read the man page for your shell to find out how to get this info from your shell.

    If you are logging in to a Windows machine via telnet, then you may need to make your remote program print "exiting status 2" just before exiting.