遇到变量失去其值的问题

发布于 2024-12-09 11:37:07 字数 6615 浏览 1 评论 0原文

#################### SUR JUNIPER FUNCTIONS ######################################################################
sub hardware {
my $session = shift(@_);
my @data_results = shift(@_);
my $sur_fpc;
my $sur_pic;
my $sur_port;
my $hostname_sur;
my $model;
my $version;
my $interface_admin_sur;
my $interface_phy_sur;
my $hostname_cisco;
my $cisco_version;
my $module_state_sur;
my @interface_error_dump;
my $msc;
my $hostname_msc;
my $device;
print @data_results;
my %hw_cmd_jun = (
        "show_ver" => "show version",
        "cli_len"  => "set cli screen-length 100000",
        "int_ext"  => "show interfaces $data_results[3] extensive",
        "int_err"  => "show interfaces $data_results[3] extensive | match errors",
        "int_log"  => "show log messages | match SNMP_TRAP_LINK | match $data_results[3]",
        "sh_fpc"   => "show chassis fpc $sur_fpc",
);
my %hw_cmd_cisco = (
        "term_len" => "terminal length 0",
        "int_err"  => "show interface $data_results[3] | include errors",
        "int_log"  => "show log | include $data_results[3]",
);


##Determine what OS the router is running###
$session->cmd($hw_cmd_cisco{"term_len"});
my @show_version = $session->cmd($hw_cmd_jun{"show_ver"});
### If it is a Juniper ###
if (grep /^\s*JUNOS/, @show_version) {
        $device = "juniper";

        ### Grab the SUR HOSTNAME, VERSION OF CODE, AND MODEL ###
        foreach my $version_line (@show_version) {
                if ( $version_line =~ /\bHostname:\s(re[0-1]-sur[0-9]{2}.*)/) {
                         $hostname_sur = $1;
                }
                elsif ( $version_line =~ /\bHostname:\s(re[0-1]-ce-[a-z]*[0-9]{2}.*)/) {
                         $msc = "Yes";
                         $hostname_msc = $1;
                }
        }
        foreach my $version_line (@show_version) {
                if ($version_line =~ /Model[:]\s([a-z]*[0-9]*)/) {
                                $model = $1;
                }
                if ($version_line =~ /boot\s([[]\d.\dR\d.\d[\]])/) {
                                $version = $1;

                }
        }
        ### CHECK THE INTERFACE OF THE SUR FOR STATE ###
        $session->cmd($hw_cmd_jun{"cli_len"});
        my @interface_extensive_dump = $session->cmd($hw_cmd_jun{"int_ext"});
        foreach my $interface_state_line (@interface_extensive_dump) {
                if ( $interface_state_line =~ /\bPhysical\sinterface:\s[ge]{2}-[0-9]\/[0-9]\/[0-9][,]\s(\bEnabled|Administratively\sdown),\sPhysical\s[a-z]{4}\sis\s(Up|Down)/) {
                         $interface_admin_sur = $1;
                         $interface_phy_sur = $2;
                }
        }

                ### CHECK TO SEE IF ANYTHING THERE IS ANYTHING IN THE LOG PERTAINING TO THE INTERFACE ###
                my @interface_log_dump = $session->cmd($hw_cmd_jun{"int_log"});

                ### Check which FPC, PIC, and PORT ###
                foreach my $user_dump_line (@data_results) {
                        if ($user_dump_line =~ /[a-z]{2}-([0-9])\/([0-9])\/([0-9])/) {
                                $sur_fpc  = $1;
                                $sur_pic  = $2;
                                $sur_port = $3;
                        }
                }

                 ### CHECK THE STATE OF THE MODULE ###
                my @module_state_dump = $session->cmd($hw_cmd_jun{"sh_fpc"});
                foreach my $module_line (@module_state_dump) {
                        if ($module_line =~ /(Online|Offline)/) {
                                 $module_state_sur = $1;
                        }
                }
                if ($msc eq "Yes") {
                        return($hostname_msc,$model,$version);
                }
                else {
                        return($device,$hostname_sur,$model,$version,$interface_admin_sur,
                               \@interface_error_dump,\@interface_log_dump,$interface_phy_sur,
                                $sur_fpc,$sur_pic,$sur_port,$module_state_sur);
                }
}
else {
$device = "cisco";

## This is a Cisco device ##
### Gather the hostname, version, and model of the CRS ###
   foreach my $version_line_cisco (@show_version) {
        if ($version_line_cisco =~ /([a-z][a-z][a-z]?[0-9][0-9][.](.*)[.](.*)[.][a-z]*)/) {
                 $hostname_cisco = $1;
        }
        if ($version_line_cisco =~ /(Version\s[0-9].[0-9].[0-9]\[[0-9]{2}\])/) {
                 $cisco_version = $1;
        }
        #if ($version_line_cisco =~ /(cisco\sCRS-\d\d?/[A-Z]\*/) {
        #       $cisco_model = $1;
        #}
   }
   ### Gather any interface error statistics on the sur ###
   my @cisco_int_errors_dump = $session->cmd($hw_cmd_cisco{"int_err"});
   ### Gather any interface information in the log ###
   my @cisco_int_log_dump = $session->cmd($hw_cmd_cisco{"int_log"});
        return ($device,$hostname_cisco,$cisco_version,\@cisco_int_errors_dump,
                \@cisco_int_log_dump);
}## else
}## end &hardware()
##################################################################################################


## Perform Hardware Check ##
(my $type,my  $hostname,
  my $model,my  $version,
  my $interface_admin,my  @int_err_dump,
  my @int_log_dump,my  $int_phy,
  my $fpc, my $pic,my  $port,
  my $mod_state) = &hardware($sur_session, @data_results);

当我运行脚本时,我可以打印出@data_results;但是它在这里变得未定义:

### CHECK THE INTERFACE OF THE SUR FOR STATE ###
        $session->cmd($hw_cmd_jun{"cli_len"});
        my @interface_extensive_dump = $session->cmd($hw_cmd_jun{"int_ext"});

我得到的错误:

[jgearh200@srv01-netops CBH]$ perl cbh_script_final.pl 
Use of uninitialized value in concatenation (.) or string at cbh_script_final.pl line 47.
Use of uninitialized value in concatenation (.) or string at cbh_script_final.pl line 47.
Use of uninitialized value in concatenation (.) or string at cbh_script_final.pl line 47.
Use of uninitialized value in concatenation (.) or string at cbh_script_final.pl line 47.
Use of uninitialized value in concatenation (.) or string at cbh_script_final.pl line 55.
Use of uninitialized value in concatenation (.) or string at cbh_script_final.pl line 55.
Use of uninitialized value in string eq at cbh_script_final.pl line 117.

这是数组的内容:

7BAC012C <FQDN> <ip-address> ge-1/2/1 <ip-address> ACTIVATED <ip-address>
7BAC012C 4011 

所以数组中有正确的数据,但后来丢失了?我不知道数据发生了什么。

如果您需要更多详细信息,请告诉我,我将更新原始帖子。

#################### SUR JUNIPER FUNCTIONS ######################################################################
sub hardware {
my $session = shift(@_);
my @data_results = shift(@_);
my $sur_fpc;
my $sur_pic;
my $sur_port;
my $hostname_sur;
my $model;
my $version;
my $interface_admin_sur;
my $interface_phy_sur;
my $hostname_cisco;
my $cisco_version;
my $module_state_sur;
my @interface_error_dump;
my $msc;
my $hostname_msc;
my $device;
print @data_results;
my %hw_cmd_jun = (
        "show_ver" => "show version",
        "cli_len"  => "set cli screen-length 100000",
        "int_ext"  => "show interfaces $data_results[3] extensive",
        "int_err"  => "show interfaces $data_results[3] extensive | match errors",
        "int_log"  => "show log messages | match SNMP_TRAP_LINK | match $data_results[3]",
        "sh_fpc"   => "show chassis fpc $sur_fpc",
);
my %hw_cmd_cisco = (
        "term_len" => "terminal length 0",
        "int_err"  => "show interface $data_results[3] | include errors",
        "int_log"  => "show log | include $data_results[3]",
);


##Determine what OS the router is running###
$session->cmd($hw_cmd_cisco{"term_len"});
my @show_version = $session->cmd($hw_cmd_jun{"show_ver"});
### If it is a Juniper ###
if (grep /^\s*JUNOS/, @show_version) {
        $device = "juniper";

        ### Grab the SUR HOSTNAME, VERSION OF CODE, AND MODEL ###
        foreach my $version_line (@show_version) {
                if ( $version_line =~ /\bHostname:\s(re[0-1]-sur[0-9]{2}.*)/) {
                         $hostname_sur = $1;
                }
                elsif ( $version_line =~ /\bHostname:\s(re[0-1]-ce-[a-z]*[0-9]{2}.*)/) {
                         $msc = "Yes";
                         $hostname_msc = $1;
                }
        }
        foreach my $version_line (@show_version) {
                if ($version_line =~ /Model[:]\s([a-z]*[0-9]*)/) {
                                $model = $1;
                }
                if ($version_line =~ /boot\s([[]\d.\dR\d.\d[\]])/) {
                                $version = $1;

                }
        }
        ### CHECK THE INTERFACE OF THE SUR FOR STATE ###
        $session->cmd($hw_cmd_jun{"cli_len"});
        my @interface_extensive_dump = $session->cmd($hw_cmd_jun{"int_ext"});
        foreach my $interface_state_line (@interface_extensive_dump) {
                if ( $interface_state_line =~ /\bPhysical\sinterface:\s[ge]{2}-[0-9]\/[0-9]\/[0-9][,]\s(\bEnabled|Administratively\sdown),\sPhysical\s[a-z]{4}\sis\s(Up|Down)/) {
                         $interface_admin_sur = $1;
                         $interface_phy_sur = $2;
                }
        }

                ### CHECK TO SEE IF ANYTHING THERE IS ANYTHING IN THE LOG PERTAINING TO THE INTERFACE ###
                my @interface_log_dump = $session->cmd($hw_cmd_jun{"int_log"});

                ### Check which FPC, PIC, and PORT ###
                foreach my $user_dump_line (@data_results) {
                        if ($user_dump_line =~ /[a-z]{2}-([0-9])\/([0-9])\/([0-9])/) {
                                $sur_fpc  = $1;
                                $sur_pic  = $2;
                                $sur_port = $3;
                        }
                }

                 ### CHECK THE STATE OF THE MODULE ###
                my @module_state_dump = $session->cmd($hw_cmd_jun{"sh_fpc"});
                foreach my $module_line (@module_state_dump) {
                        if ($module_line =~ /(Online|Offline)/) {
                                 $module_state_sur = $1;
                        }
                }
                if ($msc eq "Yes") {
                        return($hostname_msc,$model,$version);
                }
                else {
                        return($device,$hostname_sur,$model,$version,$interface_admin_sur,
                               \@interface_error_dump,\@interface_log_dump,$interface_phy_sur,
                                $sur_fpc,$sur_pic,$sur_port,$module_state_sur);
                }
}
else {
$device = "cisco";

## This is a Cisco device ##
### Gather the hostname, version, and model of the CRS ###
   foreach my $version_line_cisco (@show_version) {
        if ($version_line_cisco =~ /([a-z][a-z][a-z]?[0-9][0-9][.](.*)[.](.*)[.][a-z]*)/) {
                 $hostname_cisco = $1;
        }
        if ($version_line_cisco =~ /(Version\s[0-9].[0-9].[0-9]\[[0-9]{2}\])/) {
                 $cisco_version = $1;
        }
        #if ($version_line_cisco =~ /(cisco\sCRS-\d\d?/[A-Z]\*/) {
        #       $cisco_model = $1;
        #}
   }
   ### Gather any interface error statistics on the sur ###
   my @cisco_int_errors_dump = $session->cmd($hw_cmd_cisco{"int_err"});
   ### Gather any interface information in the log ###
   my @cisco_int_log_dump = $session->cmd($hw_cmd_cisco{"int_log"});
        return ($device,$hostname_cisco,$cisco_version,\@cisco_int_errors_dump,
                \@cisco_int_log_dump);
}## else
}## end &hardware()
##################################################################################################


## Perform Hardware Check ##
(my $type,my  $hostname,
  my $model,my  $version,
  my $interface_admin,my  @int_err_dump,
  my @int_log_dump,my  $int_phy,
  my $fpc, my $pic,my  $port,
  my $mod_state) = &hardware($sur_session, @data_results);

When I run the script I can print out @data_results; however it becomes undefined here:

### CHECK THE INTERFACE OF THE SUR FOR STATE ###
        $session->cmd($hw_cmd_jun{"cli_len"});
        my @interface_extensive_dump = $session->cmd($hw_cmd_jun{"int_ext"});

Error I get:

[jgearh200@srv01-netops CBH]$ perl cbh_script_final.pl 
Use of uninitialized value in concatenation (.) or string at cbh_script_final.pl line 47.
Use of uninitialized value in concatenation (.) or string at cbh_script_final.pl line 47.
Use of uninitialized value in concatenation (.) or string at cbh_script_final.pl line 47.
Use of uninitialized value in concatenation (.) or string at cbh_script_final.pl line 47.
Use of uninitialized value in concatenation (.) or string at cbh_script_final.pl line 55.
Use of uninitialized value in concatenation (.) or string at cbh_script_final.pl line 55.
Use of uninitialized value in string eq at cbh_script_final.pl line 117.

This is the contents of the array:

7BAC012C <FQDN> <ip-address> ge-1/2/1 <ip-address> ACTIVATED <ip-address>
7BAC012C 4011 

So the array has the correct data in it, but later is lost? I have no idea what is going on with the data.

If you need any more details just let me know and i will update the original post.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眸中客 2024-12-16 11:37:07

您正在 hardware 函数内重新定义一个名为 @data_results 的新词法变量:

...
my @data_results = shift(@_)
...

并且可能在此处滥用 shift 函数。 shift 从数组前面删除单个元素并返回它,因此您的表达式相当于

my @data_results;
$data_results[0] = shift(@_);

$data_results[3],您稍后会在函数从未被定义。

您可能只是想说

my @data_results = @_

哪个会将 @_ 中的所有元素复制到新数组中。

You are redefining a new lexical variable called @data_results inside the hardware function:

...
my @data_results = shift(@_)
...

and probably abusing the shift function here. shift removes a single element from the front of an array and returns it, so your expression is equivalent to

my @data_results;
$data_results[0] = shift(@_);

In particular, $data_results[3], which you refer to later in the function, is never defined.

You probably just meant to say

my @data_results = @_

which will copy all the elements in @_ to the new array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文