You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Perl循环提取SSH捕获输出中的Cage及电源状态

优化Perl代码处理存储笼双电源状态提取

场景说明

通过SSH执行showcage -d命令获取存储笼(Cage)的详细数据,需编写Perl代码提取每个Cage的OverallState,以及每个电源的PSState、ACState、DCState、Fan State状态。现有代码仅支持单电源处理,需优化为兼容双电源,并生成指定格式的整合状态信息。


原始SSH输出示例

Cage ID: 1
OverallState: Normal
Power Supply 1:
    PSState: Online
    ACState: Good
    DCState: Good
    Fan State: Normal
Power Supply 2:
    PSState: Online
    ACState: Good
    DCState: Good
    Fan State: Normal

Cage ID: 2
OverallState: Warning
Power Supply 1:
    PSState: Degraded
    ACState: Good
    DCState: Warning
    Fan State: Normal
Power Supply 2:
    PSState: Online
    ACState: Good
    DCState: Good
    Fan State: Normal

现有Perl代码(仅支持单电源)

#!/usr/bin/perl
use strict;
use warnings;

my ($cage_id, $overall_state, $ps_state, $ac_state, $dc_state, $fan_state);

while (<>) {
    chomp;
    if (/Cage ID: (\d+)/) {
        $cage_id = $1;
    } elsif (/OverallState: (\w+)/) {
        $overall_state = $1;
    } elsif (/PSState: (\w+)/) {
        $ps_state = $1;
    } elsif (/ACState: (\w+)/) {
        $ac_state = $1;
    } elsif (/DCState: (\w+)/) {
        $dc_state = $1;
    } elsif (/Fan State: (\w+)/) {
        $fan_state = $1;
    }
}

# 仅输出单电源信息
print "Cage $cage_id - Overall: $overall_state\n";
print "  Power Supply 1: PS=$ps_state, AC=$ac_state, DC=$dc_state, Fan=$fan_state\n";

期望输出格式

Cage 1 - OverallState: Normal
  Power Supply 1: PSState=Online, ACState=Good, DCState=Good, Fan State=Normal
  Power Supply 2: PSState=Online, ACState=Good, DCState=Good, Fan State=Normal
Cage 2 - OverallState: Warning
  Power Supply 1: PSState=Degraded, ACState=Good, DCState=Warning, Fan State=Normal
  Power Supply 2: PSState=Online, ACState=Good, DCState=Good, Fan State=Normal

优化后的Perl代码

#!/usr/bin/perl
use strict;
use warnings;

my %cages;
my ($current_cage, $current_ps);

while (<>) {
    chomp;
    next if /^\s*$/; # 跳过空行,避免无效匹配

    if (/Cage ID: (\d+)/) {
        $current_cage = $1;
        # 初始化当前Cage的存储结构
        $cages{$current_cage}{'OverallState'} = '';
        $cages{$current_cage}{'PowerSupplies'} = {};
    } elsif (/OverallState: (\w+)/) {
        $cages{$current_cage}{'OverallState'} = $1;
    } elsif (/Power Supply (\d+):/) {
        $current_ps = $1;
        # 初始化当前电源的状态存储结构
        $cages{$current_cage}{'PowerSupplies'}{$current_ps} = {
            'PSState' => '',
            'ACState' => '',
            'DCState' => '',
            'Fan State' => ''
        };
    } elsif (/PSState: (\w+)/) {
        $cages{$current_cage}{'PowerSupplies'}{$current_ps}{'PSState'} = $1;
    } elsif (/ACState: (\w+)/) {
        $cages{$current_cage}{'PowerSupplies'}{$current_ps}{'ACState'} = $1;
    } elsif (/DCState: (\w+)/) {
        $cages{$current_cage}{'PowerSupplies'}{$current_ps}{'DCState'} = $1;
    } elsif (/Fan State: (\w+)/) {
        $cages{$current_cage}{'PowerSupplies'}{$current_ps}{'Fan State'} = $1;
    }
}

# 遍历输出所有Cage和电源的完整状态
foreach my $cage (sort keys %cages) {
    print "Cage $cage - OverallState: $cages{$cage}{'OverallState'}\n";
    foreach my $ps (sort keys %{$cages{$cage}{'PowerSupplies'}}) {
        my $ps_data = $cages{$cage}{'PowerSupplies'}{$ps};
        print "  Power Supply $ps: PSState=$ps_data->{'PSState'}, ACState=$ps_data->{'ACState'}, DCState=$ps_data->{'DCState'}, Fan State=$ps_data->{'Fan State'}\n";
    }
}

优化点说明

  • 采用哈希嵌套结构%cages,统一存储每个Cage的OverallState及多电源状态集合,避免单变量覆盖问题
  • 新增$current_ps变量跟踪当前处理的电源编号,确保每个电源的状态被正确映射到对应条目
  • 跳过空行提升处理效率,避免无意义的匹配操作
  • 最终通过嵌套循环按顺序输出所有Cage和电源的完整状态,兼容任意数量的电源(不仅限于双电源)

内容的提问来源于stack exchange,提问作者R. Gaines

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 14:15:47