編輯:改寫爲更清楚Perl的分裂停止在第一線
我遇到數據到一個名爲RTR-shpolicymap_751257S0_126.xxx.xxx.xxx_OUTPUT文件的例子:
751257S0;126.xxx;FastEthernet0;up;up;not set;not set;;
751257S0;126.xxx;BRI0;down;down;not set;not set;;
751257S0;126.xxx;BRI0:1;down;down;not set;not set;;
751257S0;126.xxx;BRI0:2;down;down;not set;not set;;
751257S0;126.xxx;ATM0;up;up;not set;not set;;
751257S0;126.xxx;ATM0.835;up;up;not set;not set;;
Perl腳本執行SSH連接到路由器上,並建立連接時,它會調用該函數executeCommands 這裏描述該功能:
sub executeCommands {
open(SSHCONFIG, "/tech/restools/scripts/mynet/RTR-ssh.conf");
while (<SSHCONFIG>) {
@ligne = split(/;/, $_);
$listop = $ligne[0];
$listcmd = $ligne[1];
$fileprefix = $ligne[2];
$parsername = $ligne[3];
$parsername =~ s/\s+\z// ;
@cmd = split(/,/, $listcmd);
#Si l'operateur passe en parametre match l'operateur dans le fichier de config - execution des commandes
if ($listop =~ /$operateur/) {
for ($index = 0; $index <= $#cmd; $index++) {
$command = $cmd[$index];
&changeCommand(\$command);
&sendCommand($command, $fileprefix);
}
}
else {
next;
}
配置文件中包含此:
OBS,9 Cegetel,Altitude;sh policy-map $policyinterface;RTR-shpolicymap;RTR-parser9.pl
現在,我在與功能的煩惱changeCommand
sub changeCommand {
my $arg1 = $_[0];
if ($$arg1=~/^(.*)(\$policyinterface)(.*)/) {
print "recherche de l'interface contenant la policy voice correspondante... \n" ;
$fichier_parser1OUTPUT = `find $WORKINGDIR -type f -name RTR-shipint_\"*$codesite*$ip*OUTPUT\"` ;
@table_fichier_parser1OUTPUT = split(/\n/,$fichier_parser1OUTPUT);
for ($index4 = 0 ; $index4 <= $#table_fichier_parser1OUTPUT ; $index4++) {
$fichierNeeded = $table_fichier_parser1OUTPUT[$index4];
open(DATA,$fichierNeeded) || die ("Erreur d'ouverture de $fichierNeeded\n");
print "file $fichierNeeded have been opened \n";
while (my $lineData=<DATA>) {
print "i am printing this line $lineData \n";
my @lineSplitter = split(/;/, $lineData);
print "3e arg est <<$lineSplitter[2]>>\n";
}
$command = "sh policy-map int " . $interfaceNeeded ;
}
}
}
這個問題是存在的,這是我」會發生什麼m執行我的腳本:
recherche de l'interface contenant la policy voice correspondante...
file /tech/restools/tmp/mynet/RTR-working-dir/RTR-shipint_751257S0_126.108.2.250_OUTPUT have been opened
i am printing this line 751257S0;126.108.2.250;FastEthernet0;up;up;not set;not set;;
3e arg est <<FastEthernet0>>
Where是我的錯誤?是因爲我以前的功能已經使用$ _?
單獨測試一個類似的腳本工作...
#!/usr/bin/perl
use strict ;
my $codesite="751257S0" ;
my $ip = "126.xxx.xxx.xxx" ;
my $WORKINGDIR="/tech/restools/tmp/mynet/RTR-working-dir";
my $fichier_parser1OUTPUT = `find $WORKINGDIR -type f -name RTR-shipint_\"*$codesite*$ip*OUTPUT\"` ;
my @table_fichier_parser1OUTPUT = split(/\n/,$fichier_parser1OUTPUT);
for (my $index4 = 0 ; $index4 <= $#table_fichier_parser1OUTPUT ; $index4++) {
my $fichierNeeded = $table_fichier_parser1OUTPUT[$index4];
open(DATA,$fichierNeeded) || die ("Erreur d'ouverture de $fichierNeeded\n");
print "file $fichierNeeded have been opened \n";
while (my $lineData=<DATA>) {
print "i am printing this line $lineData \n";
my @lineSplitter = split(/;/, $lineData);
print "3e arg est <<$lineSplitter[2]>>\n";
}
}
打印
i am printing this line 751257S0;126.108.2.250;FastEthernet0;up;up;not set;not set;;
3e arg est <<FastEthernet0>>
i am printing this line 751257S0;126.108.2.250;BRI0;down;down;not set;not set;;
3e arg est <<BRI0>>
i am printing this line 751257S0;126.108.2.250;BRI0:1;down;down;not set;not set;;
3e arg est <<BRI0:1>>
...
'print scalar @ table_fichier_parser1OUTPUT;'它只有1個元素嗎? – toolic 2014-10-09 14:16:11
是的,添加打印標量顯示「1」 – 2014-10-09 14:18:03
你在Mac嗎? – simbabque 2014-10-09 14:19:13