root/NetAddr-IP-Find/trunk/lib/NetAddr/IP/Find.pm

Revision 270 (checked in by miyagawa, 19 years ago)

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 package NetAddr::IP::Find;
2
3 use strict;
4 use vars qw($VERSION @EXPORT);
5 $VERSION = '0.02';
6
7 use base qw(Exporter);
8 @EXPORT = qw(find_ipaddrs);
9
10 use NetAddr::IP;
11
12 my $regex = qr<(\d+)\.(\d+)\.(\d+)\.(\d+)>;
13
14 sub find_ipaddrs (\$&) {
15     my($r_text, $callback) = @_;
16     my $addrs_found = 0;
17
18     $$r_text =~ s{$regex}{
19         my $orig_match = join '.', $1, $2, $3, $4;
20         if (grep { _in_range($_) } $1, $2, $3, $4 == 4) {
21             $addrs_found++;
22             my $ipaddr = NetAddr::IP->new($orig_match);
23             $callback->($ipaddr, $orig_match);
24         } else {
25             $orig_match;
26         }
27     }eg;
28
29     return $addrs_found;
30 }
31
32 sub _in_range {
33     return 0 <= $_[0] && $_[0] <= 255;
34 }
35
36
37 1;
38 __END__
39
40 =head1 NAME
41
42 NetAddr::IP::Find - Find IP addresses in plain text
43
44 =head1 SYNOPSIS
45
46   use NetAddr::IP::Find;
47   $num_found = find_ipaddrs($text, \&callback);
48
49 =head1 DESCRIPTION
50
51 This is a module for finding IP addresses in plain text.
52
53 =head2 Functions
54
55 NetAddr::IP::Find exports one function, find_ipaddrs(). It works very
56 similar to URI::Find's find_uris() or Email::Find's find_emails().
57
58   $num_ipaddrs_found = find_ipaddrs($text, \&callback);
59
60 The first argument is a text to search through and manipulate. Second
61 is a callback routine which defines what to do with each IP address as
62 they're found. It returns the total number of IP addresses found.
63
64 The callback is given two arguments. The first is a NetAddr::IP
65 instance representing the IP address found. The second is the actual
66 IP address as found in the text. Whatever the callback returns will
67 replace the original text.
68
69 =head1 EXAMPLES
70
71   # For each IP address found, ping its host to see if its alive.
72   use Net::Ping;
73   my $pinger = Net::Ping->new;
74   my %pinged;
75   find_ipaddrs($text, sub {
76                    my($ipaddr, $orig) = @_;
77                    my $host = $ipaddr->to_string;
78                    next if exists $pinged{$host};
79                    $pinged{$host} = $pinger->ping($host);
80                });
81
82   while (my($host, $up) == each %pinged) {
83       print "$host is " . $up ? 'up' : 'down' . "\n";
84   }
85
86
87   # Resolve IP address to FQDN
88   find_ipaddrs($text, sub {
89                    my($ipaddr, $orig) = @_;
90                    resolve_ip($ipaddr->to_string);
91                });
92
93   sub resolve_ip {
94       use Net::DNS;
95       # see perldoc Net::DNS for details
96   }
97
98 =head1 TODO
99
100 =over 4
101
102 =item *
103
104 Subnet support.
105
106 =item *
107
108 IPv6 support.
109
110 =back
111
112 =head1 AUTHOR
113
114 Tatsuhiko Miyagawa <miyagawa@bulknews.net>
115
116 This library is free software; you can redistribute it and/or modify
117 it under the same terms as Perl itself.
118
119 =head1 SEE ALSO
120
121 L<NetAddr::IP>, L<URI::Find>, L<Email::Find>, jdresove
122
123 =cut
Note: See TracBrowser for help on using the browser.