2013-04-01 59 views
1

IP地址從下面的輸出,我只是想創建一個數組,其中所有的IP地址可以被存儲。 IP的Perl中獲取從string

示例:57.35.47.54

輸出:

Codes: '!' - success, 'Q' - request not sent, '.' - timeout, 
    'L' - labeled output interface, 'B' - unlabeled output interface, 
    'D' - DS Map mismatch, 'F' - no FEC mapping, 'f' - FEC mismatch, 
    'M' - malformed request, 'm' - unsupported tlvs, 'N' - no label entry, 
    'P' - no rx intf label prot, 'p' - premature termination of LSP, 
    'R' - transit router, 'I' - unknown upstream index, 
    'X' - unknown return code, 'x' - return code 0 

Type escape sequence to abort. 

    0 57.35.47.54 MRU 4470 [Labels: implicit-null/1852 Exp: 0/0] 
I 1 57.35.47.53 MRU 4470 [Labels: 16176 Exp: 0] 328 ms 
L 2 57.35.57.1 MRU 4470 [Labels: explicit-null/2795 Exp: 0/0] 140 ms 
D 3 57.35.7.105 MRU 4470 [Labels: 16228 Exp: 0] 364 ms 
I 4 57.35.4.78 MRU 4470 [Labels: implicit-null/16304 Exp: 0/0] 196 ms 
L 5 57.35.7.66 MRU 4470 [Labels: 16613 Exp: 0] 216 ms 
L 6 57.35.4.38 MRU 4470 [Labels: implicit-null/implicit-null Exp: 0/0] 216 ms 
! 7 57.35.4.122 288 ms 
+1

是對數據的第一行遺漏碼故意的嗎? –

回答

1

,如果你不能安裝缺少的正則表達式:常見的模塊,您可以解析它手動

use strict; 
use warnings; 

while (<DATA>) { 
    while(/([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/g) { 
     print "$1.$2.$3.$4\n" if ($1 < 256 && $2 < 256 && $3 < 256 && $4 <256); 
    } 
} 

__DATA__ 
Codes: '!' - success, 'Q' - request not sent, '.' - timeout, 
'L' - labeled output interface, 'B' - unlabeled output interface, 
'D' - DS Map mismatch, 'F' - no FEC mapping, 'f' - FEC mismatch, 
'M' - malformed request, 'm' - unsupported tlvs, 'N' - no label entry, 
'P' - no rx intf label prot, 'p' - premature termination of LSP, 
'R' - transit router, 'I' - unknown upstream index, 
'X' - unknown return code, 'x' - return code 0 
Type escape sequence to abort. 
0 57.35.47.54 MRU 4470 [Labels: implicit-null/1852 Exp: 0/0] 
I 1 57.35.47.53 MRU 4470 [Labels: 16176 Exp: 0] 328 ms 
L 2 57.35.57.1 MRU 4470 [Labels: explicit-null/2795 Exp: 0/0] 140 ms 
D 3 57.35.7.105 MRU 4470 [Labels: 16228 Exp: 0] 364 ms 
I 4 57.35.4.78 MRU 4470 [Labels: implicit-null/16304 Exp: 0/0] 196 ms 
L 5 57.35.7.66 MRU 4470 [Labels: 16613 Exp: 0] 216 ms 
L 6 57.35.4.38 MRU 4470 [Labels: implicit-null/implicit-null Exp: 0/0] 216 ms 
! 7 57.35.4.122 288 ms 
2

也許以下將是有用的:

use strict; 
use warnings; 
use Regexp::Common qw/net/; 

my @IPs; 

while (<DATA>) { 
    push @IPs, $1 while /($RE{net}{IPv4})/g; 
} 

print "$_\n" for @IPs; 

__DATA__ 
Codes: '!' - success, 'Q' - request not sent, '.' - timeout, 
'L' - labeled output interface, 'B' - unlabeled output interface, 
'D' - DS Map mismatch, 'F' - no FEC mapping, 'f' - FEC mismatch, 
'M' - malformed request, 'm' - unsupported tlvs, 'N' - no label entry, 
'P' - no rx intf label prot, 'p' - premature termination of LSP, 
'R' - transit router, 'I' - unknown upstream index, 
'X' - unknown return code, 'x' - return code 0 
Type escape sequence to abort. 
0 57.35.47.54 MRU 4470 [Labels: implicit-null/1852 Exp: 0/0] 
I 1 57.35.47.53 MRU 4470 [Labels: 16176 Exp: 0] 328 ms 
L 2 57.35.57.1 MRU 4470 [Labels: explicit-null/2795 Exp: 0/0] 140 ms 
D 3 57.35.7.105 MRU 4470 [Labels: 16228 Exp: 0] 364 ms 
I 4 57.35.4.78 MRU 4470 [Labels: implicit-null/16304 Exp: 0/0] 196 ms 
L 5 57.35.7.66 MRU 4470 [Labels: 16613 Exp: 0] 216 ms 
L 6 57.35.4.38 MRU 4470 [Labels: implicit-null/implicit-null Exp: 0/0] 216 ms 
! 7 57.35.4.122 288 ms 

輸出:

57.35.47.54 
57.35.47.53 
57.35.57.1 
57.35.7.105 
57.35.4.78 
57.35.7.66 
57.35.4.38 
57.35.4.122 

Regexp::CommonÇ包含一個匹配IPv4地址的正則表達式,並用於在每行上捕獲它們。儘管您的數據每行僅包含一個IP地址,但每行也會捕獲多個IP。捕獲的IP是push編輯到@IPs然後最後打印。

+0

是的可能是有用的,但我使用的服務器「Regexp :: Common」沒有安裝,它是要求他們安裝它的噩夢....任何其他方式? – Mahesh

+0

@Mahesh - 是的。以下將捕獲輸出中顯示的IP,但它不會執行任何數據完整性檢查,例如[Suic](http://stackoverflow.com/users/1927848/suic),但我認爲這不太可能(@:[0-9] {1,3} \。){3} [0-9] {1,3})/ g;'push @IPs,$ 1, – Kenosis

相關問題