1

I'm running a Perl script in which I use the command system to execute a SSH connection.

It looks like this :

$test_result = '/home/ergaconfigs/sauvegarde-configs/configs/test';

sub ssh_connect {
        my $test = system (`ssh -i /home/ergaconfigs/sauvegarde-configs/rsa_keys/key_config_saver -o "StrictHostKeyChecking no" ergaconfigs\@10.252.0.27 "show configuration | display set" > $test_result `);

        print "resultat de la commande : $test \n le code d erreur est $!";
}

But I want to make sure that the SSH connection succeeded. This is why I'm looking at the return code of system.
As the SSH connection succeed and the file is correctly written, the output is:

resultat de la commande : 65280
le code d erreur est 

And whatever the result of the ssh connection is (network unreachable etc..) I have the same output. Can somebody explain me why?

serenesat
  • 4,611
  • 10
  • 37
  • 53
user3745776
  • 17
  • 1
  • 6
  • Why have you got both backticks (which execute a command, and return its output) and a `system` call? – Alnitak Dec 16 '15 at 10:30
  • Because at first, i wasn't using "system" but just a variable in which I executed the command with the backquotes. As I need a return code, I tried with "system" – user3745776 Dec 16 '15 at 10:37

2 Answers2

2

I don't know why you are using system and backticks both in your command.

See this to understand system and backticks: https://stackoverflow.com/a/800105/4248931

If you want store output of your command, use backticks and if you don't care about output use system.

Remove the system from your code and check the output:

$test_result = '/home/ergaconfigs/sauvegarde-configs/configs/test';

sub ssh_connect
{
    my $test = `ssh -i /home/ergaconfigs/sauvegarde-configs/rsa_keys/key_config_saver -o "StrictHostKeyChecking no" ergaconfigs\@10.252.0.27 "show configuration | display set" > $test_result`;
    print "resultat de la commande : $test \n le code d erreur est $!";
}
Community
  • 1
  • 1
serenesat
  • 4,611
  • 10
  • 37
  • 53
1

As written, the backticks in your code are what's actually running the ssh command, and then the (presumably empty) stdout output from that is what's being passed to the system command.

I expect this is not what you intended. If so, replace the backticks with normal double quote marks. You would then need to change any double quotes that are inside the command - single quotes should do.

Alnitak
  • 334,560
  • 70
  • 407
  • 495