2014-06-17 46 views
1

我正在使用PERL的NET::OpenSSH模块。我在简单的脚本上试过了,它的工作完美。openSSH - 无法调用方法捕获

现在我包括它在一个更大的脚本:

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"; 
    } 
} 

我有2人的情况与此类似,只有“案例‘OBS’,”正在发生变化,它的密码。 变量$ ssh从来没有未初始化。

而且在脚本中,我写了这个:

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; 
    } 
} 

虽然有一些在“ssh.conf”文件,脚本应该执行该文件中给出的命令。 但是,启动时,脚本与此错误停止:

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

我的$ listcmd变量是不是空的,因为你可以看到。 为什么不能调用捕获方法? 谢谢。

回答

1

变量$sshcase "OBS"块的末尾超出范围。

那种错误的,可以很容易地抓住你的脚本的开头使狭窄:

use strict; 
use warnings; 

BTW,不要使用Switch,它被打破,通过设计和可能引入很难找到的bug在你的脚本上。

相关问题