http://www.perlmonks.org?node_id=590615

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

Hi , I am required to execute a command from my windows machine on to a Linux machine. Currently I am using Net::Telnet package for that. <pr> Now that I want to check the status of that command on my Windows machine. Is there some way where in I can return a magic string to my windows machine on the basis of value of $? variable on Linux machine. I am attaching my code here.
$username="imail1"; $passwd="imail1"; use Net::Telnet (); $t = new Net::Telnet (Timeout => 10); $t->open("172.16.14.74"); $t->print($username); $t->print($passwd); sleep(5); open(FHH, ">output.txt"); print FHH $t->cmd("cp opwv/config/config.db ./"); $flag=1; close(FHH); open(FH,"<output.txt"); while(<FH>) { if($_ =~ "ERROR"){ $flag = 0; } } if ($flag == 0) { print "Fail"; } else { print "Pass"; } close(FH); unlink("output.txt");
Please let me know whether I can modify the same code or I need to use some totally different module than Net::Telnet..
Prompt reply would be appreciated.. :)

Replies are listed 'Best First'.
Re: Want to communicate status of the last command to Windows machine
by vladdrak (Monk) on Dec 19, 2006 at 07:20 UTC
    The environment variable %ERRORLEVEL% typically contains the exit status of the last running process. I say typically as it's up to the executable to properly exit with an actual error code.
      Right vladdrak So the problem is that how to bring that exit status back to my Windows machine, through the code above I am just telnetting to a remote machine and executing a command der Now I want to bring back the status of the process back to the Windows machine and that is the issue....!
        This code works for me against a debian etch machine -- should be very similar to another flavor of Linux, though. (You also might consider the Net::SSH module..!):
        #!/usr/bin/perl use strict; use warnings; use Net::Telnet; my $host = "somehost"; my $username = "test"; my $password = "password"; my $t = new Net::Telnet; $t->dump_log('trace.log'); # detailed packet trace $t->open($host) or die "could not connect to $host: $!\n"; $t->print($username); $t->waitfor('/ssword[:] $/'); $t->print($password); $t->waitfor('/\$ *$/'); $t->print("cp a b"); $t->getline(); # read prompt my $stdout = $t->getline(); print "stdout $stdout\n"; $t->print("echo \$?"); $t->getline(); # read prompt my $exitcode = $t->getline(); print "exit $exitcode\n"; exit($exitcode);