Issue
I am trying to use Perl system command to ssh to another server and run the mkdir command but it always returns
mkdir /tmp/test.xxxxx: No such file or directory
while if I manually ssh and run the same command it works fine. Here is the code I wrote:
my $TEMPDIR = '/tmp/test.xxxxx';
system "$SSH $SSH_USER\@$TESTSVR \"mkdir $TEMPDIR\"" or die " $!\n";
Solution
system
doesn't return false on error. And $!
isn't necessarily set on error, depending on what you call an error.
system(...);
die "Can't launch child: $!\n" if $? == -1;
die "Child killed by signal ".( $? & 0x7F )."\n" if $? & 0x7F;
die "Child exited with error ".( $? >> 8 )."\n" if $? >> 8;
It's worth noting your have multiple code injection bugs which are easily fix by using the following:
system($SSH, "$SSH_USER\@$TESTSVR", "mkdir", $TEMPDIR)
Answered By - ikegami