Mahesh
Mahesh

Reputation: 233

Perl Fetch IP address from string

From the output below, I just want to create one array where all IP addresses can be stored.

Example of IP: 57.35.47.54

Output:

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

Upvotes: 1

Views: 1384

Answers (2)

Suic
Suic

Reputation: 2501

if you can't install missing regexp:common module you can parse it manually

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

Upvotes: 1

Kenosis
Kenosis

Reputation: 6204

Perhaps the following will be helpful:

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

Output:

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 contains a regex that will match IPv4 addresses, and is used above to capture them on each line. Although your data only contains one IP address per line, multiple IPs per line will also be captured. The captured IPs are pushed onto @IPs and then finally printed.

Upvotes: 2

Related Questions