0

I'm using qx() to run a command on a remote windows machine through rsh. I need to access the exit code of the remote command. I followed the instructions here "Get return code and output from command in Perl", but using $? always returns 0 - seems like it is the exit code of the rsh command instead of the command run through rsh.

However when I use ssh, then $? actually returns the exit code of the command run through ssh.

So, how can I access the return value of the command run through rsh on a remote windows machine using qx ?

qx(rsh -l $username $host perl a.pl);     # say I run a perl script on remote machine 
my $returnValue =                         # need the return value of 'perl a.pl' here
him
  • 487
  • 3
  • 12
  • I cannot reproduce on Ubuntu 19.04. On my machine `rsh` returns the exit code of the last command executed and hence `$?` is set correctly according to that value after running `qx()` – Håkon Hægland Jun 26 '19 at 08:46
  • Is that value - exit code or rsh or exit code of command run through rsh? What I want , is the exit code of command run through rsh. – him Jun 26 '19 at 08:52
  • Yes I checked it is the exit value of the command (not `rsh`). Maybe `rsh` behaves differently on a windows machine? – Håkon Hægland Jun 26 '19 at 08:57
  • 1
    Actually through a bit of Google search, I found that SSH returns the exit code of the command it ran while RSH doesn't. For RSH see this link https://docs.oracle.com/cd/E36784_01/html/E36870/rsh-1.html – him Jun 26 '19 at 09:04
  • Yes I checked, on my computer `rsh` is simply a symbolic link to `/usr/bin/ssh` so that should explain the differences. Did you find a workaround? – Håkon Hægland Jun 26 '19 at 09:08

1 Answers1

1

Here is a workaround (in the case you cannot use ssh) that saves the exit code in a temp file:

my $output = qx(rsh -l $username $host "perl a.pl; echo \\\$? > exitcode.txt");
my $exitcode = qx(rsh -l $username $host "cat exitcode.txt");
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • 1
    Another workaround (which worked for me) is when I run the remote command on windows, and there is some abnormal exit, then it prints ` returned `. So I can use the string returned by `qx` and parse it to get the return value. And if there is no such string, the return value is 0. However, I'm not sure if this is universal or just for my case only. – him Jun 26 '19 at 12:45