匹配具有一系列时间值的行

时间:2014-03-07 02:33:24

标签: php regex

我有一个数组暴露了rooter cisco命令show ip bgp summary的输出;此命令显示used / active / idle / etc.连接bgp连接。

数组看起来像:

[router_name] =>
8.8.8.8    4 54332       0       0        0    0    0 never    Active
4.4.4.4     4 14432       0       0        0    0    0 never    Active
1.1.1.1  4 53222       0       0        0    0    0 never    Active
3.2.54.3   4 64323    9743    9236        0    0    0 3d14h    Idle (Admin)
60.34.22.56  4 99999    8434    9214        0    0    0 3d14h    Idle (Admin)
43.22.55.3   4 66789    5057    4980        0    0    0 03:30:00    Idle (Admin)

我想在数组ID(router_name)中显示包含03:30:00的行,如果它小于6小时,我想从数组中删除其余部分。

我的php中的代码是这样的:

$bgp_routers = "/path/to/a/file/that/contains/the/router/list";

$routers_list = array_filter(explode("\n",file_get_contents($bgp_routers)),'strlen');


foreach ($routers_list as $k=>$v) {
    $bgp_out[$v]=array_filter(explode('#',shell_exec("$bgp_expect $v")),'strlen');
    $bgp_out[$v] = $bgp_out[$v][2];
}

foreach ($bgp_out as $k=>$v){
    $bgp_out2[$k] = array_filter(explode("\n",preg_replace(array('/sh ip b s \| i Active\|Idle/'),'',$v)),'strlen');
    array_pop($bgp_out2[$k]);
    unset($bgp_out2[$k][0]);
}
foreach ($bgp_out2 as $k=>$v){
    $count = count($v);
    for ($i=1;$i<=$count;$i++) { $bgp1[$k][$i] = (preg_match('/(\s+|.*):(\s+|.*)/',$v[$i])) ? $v[$i] : '';  }
}

输出:

$ bgp_out:

Array
(
    [Router_name3] => sh ip b s | i Active|Idle
332.11.1.110    4 12341       0       0        0    0    0 never    Idle (Admin)
332.11.1.114    4 12341       0       0        0    0    0 never    Idle (Admin)
332.11.1.118    4 12341       0       0        0    0    0 never    Idle (Admin)
Router_name3
    [Router_name4] => sh ip b s | i Active|Idle
332.11.0.106    4 423432       0       0        0    0    0 03:01:21    Active
332.11.2.22     4 42322       0       0        0    0    0 never    Active
332.11.13.238   4 5243       0       0        0    0    0 never    Active
2.11.218.82   4 2222    9743    9236        0    0    0 3d14h    Idle (Admin)
2.11.218.117  4 23423    8434    9214        0    0    0 3d14h    Idle (Admin)
190.86.127.121   4 234    5057    4980        0    0    0 00:01:00    Idle (Admin)
94.177.37.165   4 423423    4521    5015        0    0    0 3d14h    Idle (Admin)
178.21.121.73   4 423432   27364   26346        0    0    0 3d14h    Idle (Admin)
Router_name4
    [router_name1] => sh ip b s | i Active|Idle
332.11.10.78    4 42343   13851   81070        0    0    0 1y8w     Active
332.11.11.18    4 75675       0       0        0    0    0 never    Active
332.11.12.178   4 76576       0       0        0    0    0 never    Idle (Admin)
332.11.13.150   4 65756       0  282570        0    0    0 never    08:00:00
router_name1
    [Router_name2] => sh ip b s | i Active|Idle
332.11.0.114    4  5675 1194812  416375        0    0    0 2w0d     Active
2.11.218.1    4  45666 3663159 5094548        0    0    0 1w0d     Active
Router_name2
)

$ bgp_out2:

Array
(
    [router_name3] => Array
        (
            [1] => 324.32.1.110    4 34522       0       0        0    0    0 never    Idle (Admin)
            [2] => 324.32.1.114    4 34522       0       0        0    0    0 never    Idle (Admin)
            [3] => 324.32.1.118    4 34522       0       0        0    0    0 never    Idle (Admin)
        )

    [router_name4] => Array
        (
            [1] => 324.32.0.106    4 42343       0       0        0    0    0 03:01:21    Active
            [2] => 324.32.2.22     4 24234       0       0        0    0    0 never    Active
            [3] => 324.32.13.238   4 32432       0       0        0    0    0 never    Active
            [4] => 12.324.218.82   4 42332    9743    9236        0    0    0 3d15h    Idle (Admin)
            [5] => 12.324.218.117  4 12312    8434    9214        0    0    0 3d15h    Idle (Admin)
            [6] => 3.86.127.121   4 12444    5057    4980        0    0    0 00:01:00    Idle (Admin)
            [7] => 94.17.37.165   4 39737    4521    5015        0    0    0 3d15h    Idle (Admin)
            [8] => 18.21.121.73   4 14124   27364   26346        0    0    0 3d15h    Idle (Admin)
        )

    [router_name1] => Array
        (
            [1] => 324.32.10.78    4 39335   13851   81070        0    0    0 1y8w     Active
            [2] => 324.32.11.18    4 34732       0       0        0    0    0 never    Active
            [3] => 324.32.12.178   4 43890       0       0        0    0    0 8:00:00    Idle (Admin)
            [4] => 324.32.13.150   4 34279       0  283414        0    0    0 never    Idle
        )

    [router_name2] => Array
        (
            [1] => 324.32.0.114    4  5483 1194812  416375        0    0    0 2w0d     Active
            [2] => 12.324.218.1    4  3234 3663159 5094548        0    0    0 1w0d     Active
        )

)

预期产出:

array (
[router_name4] => 
array( 
[0] => 324.32.0.106    4 42343       0       0        0    0    0 03:01:21    Active 
[1] => 3.86.127.121   4 12444    5057    4980        0    0    0 00:01:00
)
)

任何解决方案?

1 个答案:

答案 0 :(得分:1)

这就是如何匹配一条时间少于6小时的行:

preg_match_all('/^.*0[0-5]:\d{2}:\d{2}.*$/m', $str, $matches);

对于每一行,它匹配hh:mm:ss模式,其中小时组件介于05之间。

$matches数组包含匹配的行,如果没有匹配的行,则为空。