Search code examples
perlsshcaptureopenssh

openSSH - can't call method capture


I'm working around with the NET::OpenSSH module from PERL. I tried it on simple scripts and it's working perfectly.

Now i'm including it in a bigger script :

switch ($ARGV[2]) {

case "OBS" {

    my $ssh = Net::OpenSSH->new("$user:$passwdobs\@$ARGV[1]");
    if ($ssh->error) {
        print ERR_REPORT_SSH "Echec ssh sur " . $ARGV[1] . " erreur a gerer plus tard\n";
        die "Echec du ssh sur " . $ARGV[1] . "\n";
    }
}

I have 2 others cases similar to this one, only the "case "OBS"" is changing, and the password with it. the variable $ssh is NEVER uninitialized.

Further in the script, i wrote this :

open(SSHCONFIG, "/tech/gtr/scripts/osm/environnement_qualif/scan-rh2/bin/ssh.conf");
while (<SSHCONFIG>) {
    @ligne = split(/;/, $_ );
    $listop = $ligne[0];
    $listcmd = $ligne[1];
    $fileprefix = $ligne[2];

    if ($listop =~ /$operateur/) {
        print "l'operateur match\n" . "commande : " . $listcmd . "\n";
        #sendCommand($listcmd, $fileprefix);
        my $out1 = $ssh->capture($listcmd);
        print $out1 ;
    }
    else {
        next;
    }
}

While there's something in the "ssh.conf" file, the script should execute the command given in the file. But, when started, the script stops with this error :

l'operateur match
commande : show arp
Can't call method "capture" on an undefined value at sshscript.pl line 65, <SSHCONFIG> line 1.

My $listcmd variable is not empty as you can see. Why can't it call the capture method? Thanks.


Solution

  • The variable $ssh goes out of scope at the end of the case "OBS" block.

    That kind of errors can be easily caught enabling strictures at the beginning of your script:

    use strict;
    use warnings;
    

    BTW, don't use Switch, it is broken-by-design and may introduce hard to find bugs on your scripts.