lib/App/ipchgmon.pm

filter: Statementstmt 92.6% 190 / 205 Branchbran 78.5% 77 / 98 Conditioncond 50.6% 40 / 79 MC/DCmcdc 31.5% 12 / 38 Subroutinesub 100% 36 / 36 Podpod 0.0% 0 / 17 totaltotal 75.0% 355 / 473 CC 69 SCAR 22.1
CC
69
Cov
80%
main
25.1
send_email
10.7
valid6
10.0
CRAP 9.1
↑ Summary /usr/local/bin/perldoc »
1 #!/usr/bin/perl
2 package App::ipchgmon;
3
4 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 45 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 45 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 45 ▶use Data::Validate::Email qw(is_email);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 45 ▶use Data::Validate::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 45 ▶use Email::Sender::Transport::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 45 ▶use Email::Stuffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 45 ▶use LWP::Online 'online';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 45 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 45 ▶use Socket qw(:addrinfo SOCK_RAW);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 45 ▶use Text::CSV qw(csv);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 45 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 25 our $VERSION = '1.0.7';
21
22 27 my $TIMEFORMAT = '%FT%T%z';
23 27 my $strp = DateTime::Format::Strptime->new(on_error => 'croak',
24 pattern => $TIMEFORMAT,
25 );
26
27 27 our ($opt_help, $opt_man, $opt_versions,
28 $opt_debug, $opt_singleemail, $opt_4, $opt_6,
29 $opt_email, $opt_file, $opt_mailserver, $opt_mailport, $opt_leeway,
30 $opt_mailfrom, $opt_mailsubject, $opt_server, $opt_dnsname,
31 );
32
33 27 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("help!", \$App::ipchgmon::opt_help, "man!", \$App::ipchgmon::opt_man, "versions!", \$App::ipchgmon::opt_versions, "debug!", \$App::ipchgmon::opt_debug, "singleemail!", \$App::ipchgmon::opt_singleemail, "4!", \$App::ipchgmon::opt_4, "6!", \$App::ipchgmon::opt_6, "email|mailto=s\@", \$App::ipchgmon::opt_email, "file=s", \$App::ipchgmon::opt_file, "server=s", \$App::ipchgmon::opt_server, "mailserver=s", \$App::ipchgmon::opt_mailserver, "mailport=i", \$App::ipchgmon::opt_mailport, "leeway=i", \$App::ipchgmon::opt_leeway, "mailfrom=s", \$App::ipchgmon::opt_mailfrom, "mailsubject=s", \$App::ipchgmon::opt_mailsubject, "dnsname=s", \$App::ipchgmon::opt_dnsname) or pod2usage("-verbose", 1) and exit 12 0 12
Condition50%2/4
&GetOptions('help!', \$App::ipchgmon::opt_help, 'man!', \$App::ipchgmon::opt_man, 'versions!', \$App::ipchgmon::opt_versions, 'debug!', \$App::ipchgmon::opt_debug, 'singleemail!', \$App::ipchgmon::opt_singleemail, '4!', \$App::ipchgmon::opt_4, '6!', \$App::ipchgmon::opt_6, 'email|mailto=s@', \$App::ipchgmon::opt_email, 'file=s', \$App::ipchgmon::opt_file, 'server=s', \$App::ipchgmon::opt_server, 'mailserver=s', \$App::ipchgmon::opt_mailserver, 'mailport=i', \$App::ipchgmon::opt_mailport, 'leeway=i', \$App::ipchgmon::opt_leeway, 'mailfrom=s', \$App::ipchgmon::opt_mailfrom, 'mailsubject=s', \$App::ipchgmon::opt_mailsubject, 'dnsname=s', \$App::ipchgmon::opt_dnsname) || pod2usage('-verbose', 1) && exit
l!l
120
pod2usage("-verbose", 1) and exit
!ll
20
MC/DC0%0/2
&GetOptions('help!', \$App::ipchgmon::opt_help, 'man!', \$App::ipchgmon::opt_man, 'versions!', \$App::ipchgmon::opt_versions, 'debug!', \$App::ipchgmon::opt_debug, 'singleemail!', \$App::ipchgmon::opt_singleemail, '4!', \$App::ipchgmon::opt_4, '6!', \$App::ipchgmon::opt_6, 'email|mailto=s@', \$App::ipchgmon::opt_email, 'file=s', \$App::ipchgmon::opt_file, 'server=s', \$App::ipchgmon::opt_server, 'mailserver=s', \$App::ipchgmon::opt_mailserver, 'mailport=i', \$App::ipchgmon::opt_mailport, 'leeway=i', \$App::ipchgmon::opt_leeway, 'mailfrom=s', \$App::ipchgmon::opt_mailfrom, 'mailsubject=s', \$App::ipchgmon::opt_mailsubject, 'dnsname=s', \$App::ipchgmon::opt_dnsname) || pod2usage('-verbose', 1) && exit
&GetOptions('help!', \$App::ipchgmon::opt_help, 'man!', \$App::ipchgmon::opt_man, 'versions!', \$App::ipchgmon::opt_versions, 'debug!', \$App::ipchgmon::opt_debug, 'singleemail!', \$App::ipchgmon::opt_singleemail, '4!', \$App::ipchgmon::opt_4, '6!', \$App::ipchgmon::opt_6, 'email|mailto=s@', \$App::ipchgmon::opt_email, 'file=s', \$App::ipchgmon::opt_file, 'server=s', \$App::ipchgmon::opt_server, 'mailserver=s', \$App::ipchgmon::opt_mailserver, 'mailport=i', \$App::ipchgmon::opt_mailport, 'leeway=i', \$App::ipchgmon::opt_leeway, 'mailfrom=s', \$App::ipchgmon::opt_mailfrom, 'mailsubject=s', \$App::ipchgmon::opt_mailsubject, 'dnsname=s', \$App::ipchgmon::opt_dnsname) pod2usage("-verbose", 1)
Truth table33%1/3
A || B
A= &GetOptions('help!', \$App::ipchgmon::opt_help, 'man!', \$App::ipchgmon::opt_man, 'versions!', \$App::ipchgmon::opt_versions, 'debug!', \$App::ipchgmon::opt_debug, 'singleemail!', \$App::ipchgmon::opt_singleemail, '4!', \$App::ipchgmon::opt_4, '6!', \$App::ipchgmon::opt_6, 'email|mailto=s@', \$App::ipchgmon::opt_email, 'file=s', \$App::ipchgmon::opt_file, 'server=s', \$App::ipchgmon::opt_server, 'mailserver=s', \$App::ipchgmon::opt_mailserver, 'mailport=i', \$App::ipchgmon::opt_mailport, 'leeway=i', \$App::ipchgmon::opt_leeway, 'mailfrom=s', \$App::ipchgmon::opt_mailfrom, 'mailsubject=s', \$App::ipchgmon::opt_mailsubject, 'dnsname=s', \$App::ipchgmon::opt_dnsname)B= pod2usage("-verbose", 1)
ABresult
1X1
011
000
34 'help!' => \$opt_help,
35 'man!' => \$opt_man,
36 'versions!' => \$opt_versions,
37
38 'debug!' => \$opt_debug,
39 'singleemail!' => \$opt_singleemail,
40 '4!' => \$opt_4,
41 '6!' => \$opt_6,
42
43 'email|mailto=s@' => \$opt_email,
44 'file=s' => \$opt_file,
45 'server=s' => \$opt_server,
46 'mailserver=s' => \$opt_mailserver,
47 'mailport=i' => \$opt_mailport,
48 'leeway=i' => \$opt_leeway,
49 'mailfrom=s' => \$opt_mailfrom,
50 'mailsubject=s' => \$opt_mailsubject,
51 'dnsname=s' => \$opt_dnsname,
52 ) or pod2usage(-verbose => 1) && exit;
53
54 27 ▶pod2usage(-verbose => 1) && exit if defined $opt_help;
Branch: 1 branch
BranchTrueFalseTotal
if defined $App::ipchgmon::opt_help 0 12 12
Condition60%3/5
defined $App::ipchgmon::opt_help && (pod2usage('-verbose', 1) && exit)
!ll
1311
pod2usage("-verbose", 1) and exit
!ll
00
MC/DC100%2/2
defined $App::ipchgmon::opt_help && (pod2usage('-verbose', 1) && exit)
defined $App::ipchgmon::opt_help pod2usage("-verbose", 1)
Truth table100%3/3
A && B
A= defined $App::ipchgmon::opt_helpB= pod2usage("-verbose", 1)
ABresult
0X0
100
111
55 27 ▶pod2usage(-verbose => 2) && exit if defined $opt_man;
Branch: 1 branch
BranchTrueFalseTotal
if defined $App::ipchgmon::opt_man 0 12 12
Condition25%1/4
defined $App::ipchgmon::opt_man && (pod2usage('-verbose', 2) && exit)
!ll
120
pod2usage("-verbose", 2) and exit
!ll
00
MC/DC0%0/2
defined $App::ipchgmon::opt_man && (pod2usage('-verbose', 2) && exit)
defined $App::ipchgmon::opt_man pod2usage("-verbose", 2)
Truth table33%1/3
A && B
A= defined $App::ipchgmon::opt_manB= pod2usage("-verbose", 2)
ABresult
0X0
100
111
56
57 27 ▶unless (caller()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller()) 0 12 12
58 0 ▶ if (!defined $opt_file and !$opt_debug) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $App::ipchgmon::opt_file or $App::ipchgmon::opt_debug) 0 0 0
Condition0%0/3
defined $App::ipchgmon::opt_file or $App::ipchgmon::opt_debug
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $App::ipchgmon::opt_file or $App::ipchgmon::opt_debug
defined $App::ipchgmon::opt_file $App::ipchgmon::opt_debug
Truth table0%0/3
A or B
A= defined $App::ipchgmon::opt_fileB= $App::ipchgmon::opt_debug
ABresult
1X1
011
000
59 0 pod2usage(-verbose => 1);
60 0 exit;
61 }
62 0 ▶ if (!defined $opt_server and !$opt_debug) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $App::ipchgmon::opt_server or $App::ipchgmon::opt_debug) 0 0 0
Condition0%0/3
defined $App::ipchgmon::opt_server or $App::ipchgmon::opt_debug
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $App::ipchgmon::opt_server or $App::ipchgmon::opt_debug
defined $App::ipchgmon::opt_server $App::ipchgmon::opt_debug
Truth table0%0/3
A or B
A= defined $App::ipchgmon::opt_serverB= $App::ipchgmon::opt_debug
ABresult
1X1
011
000
63 0 pod2usage(-verbose => 1);
64 0 exit;
65 }
66 0 main();
67 }
68
69 17 ▶sub main {
Subroutine main: called CC=17 SCAR=32.2
Pod undocumented
70 33 ▶ dump_options() if $opt_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::ipchgmon::opt_debug 0 21 21
71 33 ▶ validate_email() if defined $opt_email and scalar @$opt_email;
Branch: 1 branch
BranchTrueFalseTotal
if defined $App::ipchgmon::opt_email and scalar @$App::ipchgmon::opt_email 0 21 21
Condition100%2/2
defined $App::ipchgmon::opt_email and scalar @$App::ipchgmon::opt_email
!ll
2112
MC/DC100%1/1
defined $App::ipchgmon::opt_email and scalar @$App::ipchgmon::opt_email
defined $App::ipchgmon::opt_email
Truth table100%2/2
A
A= defined $App::ipchgmon::opt_email
Aresult
00
11
72 33 ▶ validate_transport() if defined $opt_mailport or defined $opt_mailserver;
Branch: 1 branch
BranchTrueFalseTotal
if defined $App::ipchgmon::opt_mailport or defined $App::ipchgmon::opt_mailserver 21 0 21
Condition0%0/3
defined $App::ipchgmon::opt_mailport or defined $App::ipchgmon::opt_mailserver
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $App::ipchgmon::opt_mailport or defined $App::ipchgmon::opt_mailserver
defined $App::ipchgmon::opt_mailport defined $App::ipchgmon::opt_mailserver
Truth table0%0/3
A or B
A= defined $App::ipchgmon::opt_mailportB= defined $App::ipchgmon::opt_mailserver
ABresult
1X1
011
000
73 33 ▶ $opt_4 ||= 0;
Condition33%1/3
$App::ipchgmon::opt_4 ||= 0
l!l
2100
MC/DC0%0/1
$App::ipchgmon::opt_4 ||= 0
$App::ipchgmon::opt_4
Truth table50%1/2
A
A= $App::ipchgmon::opt_4
Aresult
11
00
74 33 ▶ $opt_6 ||= 0;
Condition0%0/2
$App::ipchgmon::opt_6 ||= 0
l!l
00
MC/DC0%0/1
$App::ipchgmon::opt_6 ||= 0
$App::ipchgmon::opt_6
Truth table0%0/2
A
A= $App::ipchgmon::opt_6
Aresult
11
00
75 33 ▶ unless (online) {
Branch: 1 branch
BranchTrueFalseTotal
unless (online()) 4 17 21
76 33 $opt_mailsubject = "Internet connection lost for $opt_server";
77 33 send_email($opt_server, 'No internet connection');
78 33 exit;
79 }
80 33 my $aoaref;
81 33 ▶ $aoaref = read_file() if -e $opt_file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $App::ipchgmon::opt_file 0 17 17
82 33 my $ip4 = get_ip4();
83 33 ▶ check_changes($ip4, $aoaref) if $ip4 and ($opt_4 or !$opt_6);
Branch: 1 branch
BranchTrueFalseTotal
if $ip4 and $App::ipchgmon::opt_4 || !$App::ipchgmon::opt_6 1 32 33
Condition16%1/6
$ip4 and $App::ipchgmon::opt_4 || !$App::ipchgmon::opt_6
!ll&&!rl&&r
2100
$App::ipchgmon::opt_4 or not $App::ipchgmon::opt_6
l!l&&r!l&&!r
000
MC/DC0%0/3
$ip4 and $App::ipchgmon::opt_4 || !$App::ipchgmon::opt_6
$ip4 $App::ipchgmon::opt_4 not $App::ipchgmon::opt_6
Truth table0%0/4
A and B or not(C)
A= $ip4B= $App::ipchgmon::opt_4C= not $App::ipchgmon::opt_6
ABCresult
0XX0
1000
11X1
1011
84 33 my $ip6 = get_ip6();
85 1 ▶ check_changes($ip6, $aoaref) if $ip6 and ($opt_6 or !$opt_4);
Branch: 1 branch
BranchTrueFalseTotal
if $ip6 and $App::ipchgmon::opt_6 || !$App::ipchgmon::opt_4 16 1 17
Condition83%5/6
$ip6 and $App::ipchgmon::opt_6 || !$App::ipchgmon::opt_4
!ll&&!rl&&r
2154
$App::ipchgmon::opt_6 or not $App::ipchgmon::opt_4
l!l&&r!l&&!r
2150
MC/DC0%0/3
$ip6 and $App::ipchgmon::opt_6 || !$App::ipchgmon::opt_4
$ip6 $App::ipchgmon::opt_6 not $App::ipchgmon::opt_4
Truth table0%0/4
A and B or not(C)
A= $ip6B= $App::ipchgmon::opt_6C= not $App::ipchgmon::opt_4
ABCresult
0XX0
1000
11X1
1011
86 17 $aoaref = read_file();
87 17 ▶ check_dns($opt_dnsname, $aoaref) if $opt_dnsname;
Branch: 1 branch
BranchTrueFalseTotal
if $App::ipchgmon::opt_dnsname 6 11 17
88 14 exit;
89 }
90
91 6 ▶sub check_dns {
Subroutine check_dns: called CC=8 SCAR=20.8
Pod undocumented
92 19 my ($dnsname, $aoaref) = @_;
93 19 my ($ip4, $ip6) = nslookup($dnsname);
94 19 my @list;
95 6 ▶ if ($opt_4 == $opt_6) {
Branch: 2 branches
BranchTrueFalseTotal
if ($App::ipchgmon::opt_4 == $App::ipchgmon::opt_6) { } 7 13 20
elsif ($App::ipchgmon::opt_4) { } 1 14 15
96 4 push @list, $ip4, $ip6;
97 } elsif ($opt_4) {
98 1 push @list, $ip4;
99 } else {
100 14 push @list, $ip6;
101 }
102 19 for my $ip (@list) {
103 23 my ($latest, $overdue) = last_ip($ip, $aoaref);
104 23 ▶ if (!$latest or $overdue) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $latest or $overdue) 9 14 23
Condition100%3/3
not $latest or $overdue
l!l&&r!l&&!r
1971
MC/DC100%2/2
not $latest or $overdue
not $latest $overdue
Truth table100%3/3
not(A) or B
A= not $latestB= $overdue
ABresult
1X1
011
000
105 11 ▶ send_email($ip, "$dnsname has moved to $ip")
Branch: 1 branch
BranchTrueFalseTotal
if defined $App::ipchgmon::opt_email and scalar @$App::ipchgmon::opt_email 22 0 22
Condition100%3/3
defined $App::ipchgmon::opt_email and scalar @$App::ipchgmon::opt_email
!ll
21011
MC/DC100%1/1
defined $App::ipchgmon::opt_email and scalar @$App::ipchgmon::opt_email
defined $App::ipchgmon::opt_email
Truth table100%2/2
A
A= defined $App::ipchgmon::opt_email
Aresult
00
11
106 if defined $opt_email and scalar @$opt_email;
107 }
108 }
109 }
110
111 19 ▶sub check_changes {
Subroutine check_changes: called CC=2 SCAR=6.9
Pod undocumented
112 8 my ($ip, $aoaref) = @_;
113 8 my ($latest, $overdue) = last_ip($ip, $aoaref);
114 8 ▶ new_ip($ip) if !$latest;
Branch: 1 branch
BranchTrueFalseTotal
if not $latest 4 4 8
115 }
116
117 # Returns two booleans. The first indicates whether the IP address passed in
118 # is the latest of its type in the AoA. The second indicates whether the leeway
119 # has passed.
120 33 ▶sub last_ip {
Subroutine last_ip: called CC=9 SCAR=22.0
Pod undocumented
121 22 my ($ip, $aoaref) = @_;
122 20 ▶ return 0, 0 unless defined $aoaref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $aoaref 2 20 22
123 18 my $v4 = valid4($ip);
124 18 my ($lastip, $lasttime);
125 18 for my $line (reverse @$aoaref) {
126 32 ▶ if ((valid4($$line[0]) and $v4)
Branch: 1 branch
BranchTrueFalseTotal
if (valid4($line->[0]) and $v4 or valid6($line->[0]) and not $v4) 18 14 32
Condition88%8/9
valid4($line->[0]) and $v4 or valid6($line->[0]) and not $v4
l!l&&r!l&&!r
141714
valid4($line->[0]) and $v4
!ll&&!rl&&r
181314
valid6($line->[0]) and not $v4
!ll&&!rl&&r
01417
MC/DC50%2/4
valid4($line->[0]) and $v4 or valid6($line->[0]) and not $v4
valid4($line->[0]) $v4 valid6($line->[0]) not $v4
Truth table42%3/7
A and B or C and not(D)
A= valid4($line->[0])B= $v4C= valid6($line->[0])D= not $v4
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
127 or (valid6($$line[0]) and !$v4)) {
128 18 $lastip = $$line[0];
129 18 $lasttime = $strp->parse_datetime($$line[1]);
130 18 last;
131 }
132 }
133 18 ▶ if ($lastip eq $ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastip eq $ip) { } 6 12 18
134 # This is the latest IP address of its type
135 6 ▶ $opt_leeway //= 0;
Condition66%2/3
$App::ipchgmon::opt_leeway //= 0
l!l
4150
MC/DC100%1/1
$App::ipchgmon::opt_leeway //= 0
$App::ipchgmon::opt_leeway
Truth table100%2/2
A
A= $App::ipchgmon::opt_leeway
Aresult
11
00
136 6 my $dt = DateTime->now;
137 19 my $overdue = $dt->epoch > ($lasttime->epoch + $opt_leeway);
138 19 return 1, $overdue;
139 } else {
140 25 return 0, 0;
141 }
142 }
143
144 20 ▶sub new_ip {
Subroutine new_ip: called CC=5 SCAR=16.3
Pod undocumented
145 20 my ($ip) = @_;
146 20 ▶ open my $fh, '>>:encoding(utf8)', $opt_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>:encoding(utf8)", $App::ipchgmon::opt_file or die "Unable to append to $App::ipchgmon::opt_file: $!" 6 1 7
147 or die "Unable to append to $opt_file: $!";
148 6 my $dt = DateTime->now;
149 6 my $timestamp = $dt->rfc3339;
150 6 my $csv = Text::CSV->new();
151 6 my @fields = ($ip, $timestamp);
152 6 $csv->say($fh, \@fields);
153 6 ▶ close $fh or die "Unable to close $opt_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Unable to close $App::ipchgmon::opt_file: $!" 6 0 6
154 6 ▶ send_email($ip) if defined $opt_email and scalar @$opt_email;
Branch: 1 branch
BranchTrueFalseTotal
if defined $App::ipchgmon::opt_email and scalar @$App::ipchgmon::opt_email 13 6 19
Condition33%1/3
defined $App::ipchgmon::opt_email and scalar @$App::ipchgmon::opt_email
!ll
800
MC/DC0%0/1
defined $App::ipchgmon::opt_email and scalar @$App::ipchgmon::opt_email
defined $App::ipchgmon::opt_email
Truth table50%1/2
A
A= defined $App::ipchgmon::opt_email
Aresult
00
11
155 }
156
157 6 ▶sub read_file {
Subroutine read_file: called CC=1 SCAR=0.0
Pod undocumented
158 4 return csv (in => $opt_file);
159 }
160
161 16 ▶sub dump_options {
Subroutine dump_options: called CC=1 SCAR=0.2
Pod undocumented
162 45 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
163 0 say "Help: >$opt_help<";
164 0 say "Man: >$opt_man<";
165 0 say "Versions: >$opt_versions<";
166 0 say "Single email: >$opt_singleemail<";
167 0 say "4: >$opt_4<";
168 13 say "6: >$opt_6<";
169 13 say "File: >$opt_file<";
170 2 say "Server: >$opt_server<";
171 2 say "DNS name: >$opt_dnsname<";
172 2 say "Mail Server: >$opt_mailserver<";
173 2 say "Mail Port: >$opt_mailport<";
174 2 say "Mail From: >$opt_mailfrom<";
175 2 say "Mail Subject: >$opt_mailsubject<";
176 2 say "Leeway: >$opt_leeway<";
177 2 print "Email addresses: ";
178 16 print Dumper $opt_email;
179 45 ▶ use warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
180 }
181
182 6 ▶sub validate_email {
Subroutine validate_email: called CC=3 SCAR=11.4
Pod undocumented
183 16 for my $address (@$opt_email) {
184 16 ▶ die "Invalid email address: $address" unless is_email($address);
Branch: 1 branch
BranchTrueFalseTotal
unless is_email($address) 13 0 13
185 }
186 }
187
188 3 ▶sub validate_transport {
Subroutine validate_transport: called CC=3 SCAR=21.1
Pod undocumented
189 16 ▶ die "Invalid option combination - mailport is $opt_mailport but mailserver is unspecified"
Branch: 1 branch
BranchTrueFalseTotal
if defined $App::ipchgmon::opt_mailport and not defined $App::ipchgmon::opt_mailserver 0 0 0
Condition0%0/3
defined $App::ipchgmon::opt_mailport and not defined $App::ipchgmon::opt_mailserver
!ll&&!rl&&r
000
MC/DC0%0/2
defined $App::ipchgmon::opt_mailport and not defined $App::ipchgmon::opt_mailserver
defined $App::ipchgmon::opt_mailport not defined $App::ipchgmon::opt_mailserver
Truth table0%0/3
A and not(B)
A= defined $App::ipchgmon::opt_mailportB= not defined $App::ipchgmon::opt_mailserver
ABresult
0X0
100
111
190 if defined $opt_mailport and !defined $opt_mailserver;
191 }
192
193 2 ▶sub build_transport {
Subroutine build_transport: called CC=2 SCAR=7.0
Pod undocumented
194 18 ▶ $opt_mailport ||= 25;
Condition66%2/3
$App::ipchgmon::opt_mailport ||= 25
l!l
110
MC/DC100%1/1
$App::ipchgmon::opt_mailport ||= 25
$App::ipchgmon::opt_mailport
Truth table100%2/2
A
A= $App::ipchgmon::opt_mailport
Aresult
11
00
195 18 my $transport = Email::Sender::Transport::SMTP->new({
196 host => $opt_mailserver,
197 port => $opt_mailport,
198 });
199 18 return $transport;
200 }
201
202 10 ▶sub send_email {
Subroutine send_email: called CC=9 SCAR=23.7
Pod undocumented
203 26 my ($ip, $body) = @_;
204 26 my $transport;
205 26 ▶ $transport = build_transport if defined $opt_mailserver;
Branch: 1 branch
BranchTrueFalseTotal
if defined $App::ipchgmon::opt_mailserver 0 10 10
206 26 ▶ $opt_server ||= '';
Condition100%2/2
$App::ipchgmon::opt_server ||= ""
l!l
19
MC/DC100%1/1
$App::ipchgmon::opt_server ||= ""
$App::ipchgmon::opt_server
Truth table100%2/2
A
A= $App::ipchgmon::opt_server
Aresult
11
00
207 26 ▶ $opt_mailsubject ||= $opt_server . ' has a new address';
Condition33%1/3
$App::ipchgmon::opt_mailsubject ||= $App::ipchgmon::opt_server . ' has a new address'
l!l
1000
MC/DC0%0/1
$App::ipchgmon::opt_mailsubject ||= $App::ipchgmon::opt_server . ' has a new address'
$App::ipchgmon::opt_mailsubject
Truth table50%1/2
A
A= $App::ipchgmon::opt_mailsubject
Aresult
11
00
208 26 ▶ $body ||= "$opt_server is now at $ip";
Condition66%2/3
$body ||= "$App::ipchgmon::opt_server is now at $ip"
l!l
910
MC/DC100%1/1
$body ||= "$App::ipchgmon::opt_server is now at $ip"
$body
Truth table100%2/2
A
A= $body
Aresult
11
00
209 26 my %params;
210 26 $params{'from'} = $opt_mailfrom;
211 26 $params{'subject'} = $opt_mailsubject;
212 16 $params{'text_body'} = $body;
213 17 ▶ $params{'transport'} = $transport if defined $transport;
Branch: 1 branch
BranchTrueFalseTotal
if defined $transport 2 21 23
214 13 ▶ if ($opt_singleemail or 1 == scalar(@$opt_email)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::ipchgmon::opt_singleemail or 1 == scalar @$App::ipchgmon::opt_email) { } 12 0 12
Condition33%1/3
$App::ipchgmon::opt_singleemail or 1 == scalar @$App::ipchgmon::opt_email
l!l&&r!l&&!r
0100
MC/DC0%0/2
$App::ipchgmon::opt_singleemail or 1 == scalar @$App::ipchgmon::opt_email
$App::ipchgmon::opt_singleemail 1 == scalar @$App::ipchgmon::opt_email
Truth table33%1/3
A or B
A= $App::ipchgmon::opt_singleemailB= 1 == scalar @$App::ipchgmon::opt_email
ABresult
1X1
011
000
215 10 for my $address (@$opt_email) {
216 10 $params{'to'} = $address;
217 10 my $stuffer = Email::Stuffer->new(\%params);
218 10 $stuffer->send;
219 }
220 } else {
221 0 $params{'to'} = $opt_email;
222 0 my $stuffer = Email::Stuffer->new(\%params);
223 0 $stuffer->send;
224 }
225 }
226
227 66 ▶sub valid4 {
Subroutine valid4: called CC=9 SCAR=22.0
Pod undocumented
228 66 my $ip = shift;
229 66 ▶ return 0 if is_unroutable_ipv4($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_unroutable_ipv4($ip) 1 67 68
230 65 ▶ return 0 if is_private_ipv4($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_private_ipv4($ip) 4 68 72
231 64 ▶ return 0 if is_loopback_ipv4($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_loopback_ipv4($ip) 2 65 67
232 63 ▶ return 0 if is_linklocal_ipv4($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_linklocal_ipv4($ip) 1 62 63
233 62 ▶ return 0 if is_testnet_ipv4($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_testnet_ipv4($ip) 1 61 62
234 61 ▶ return 0 if is_anycast_ipv4($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_anycast_ipv4($ip) 1 60 61
235 60 ▶ return 0 if is_multicast_ipv4($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_multicast_ipv4($ip) 1 59 60
236 59 ▶ return 1 if is_ipv4($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_ipv4($ip) 37 22 59
237 }
238
239 27 ▶sub valid6 {
Subroutine valid6: called CC=10 SCAR=23.0
Pod undocumented
240 27 my $ip = shift;
241 27 ▶ return 0 if is_private_ipv6($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_private_ipv6($ip) 1 26 27
242 26 ▶ return 0 if is_loopback_ipv6($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_loopback_ipv6($ip) 1 25 26
243 25 ▶ return 0 if is_linklocal_ipv6($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_linklocal_ipv6($ip) 1 24 25
244 24 ▶ return 0 if is_multicast_ipv6($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_multicast_ipv6($ip) 1 23 24
245 23 ▶ return 0 if is_ipv4_mapped_ipv6($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_ipv4_mapped_ipv6($ip) 1 22 23
246 22 ▶ return 0 if is_discard_ipv6($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_discard_ipv6($ip) 1 21 22
247 21 ▶ return 0 if is_special_ipv6($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_special_ipv6($ip) 1 20 21
248 20 ▶ return 0 if is_documentation_ipv6($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_documentation_ipv6($ip) 1 19 20
249 19 ▶ return 1 if is_ipv6($ip);
Branch: 1 branch
BranchTrueFalseTotal
if is_ipv6($ip) 19 0 19
250 }
251
252 3 ▶sub get_ip6 {
Subroutine get_ip6: called CC=1 SCAR=0.0
Pod undocumented
253 1 return get_ip('http://ip6only.me/api/');
254 }
255
256 14 ▶sub get_ip4 {
Subroutine get_ip4: called CC=1 SCAR=0.0
Pod undocumented
257 1 return get_ip('http://ip4only.me/api/');
258 }
259
260 17 ▶sub get_ip {
Subroutine get_ip: called CC=1 SCAR=0.0
Pod undocumented
261 2 my $url = shift;
262 2 my $ua = LWP::UserAgent->new;
263 2 my $req = HTTP::Request->new(GET => $url);
264 2 my $res = $ua->request($req);
265 2 my $csv = $res->content;
266 2 my $aoa = csv(in => \$csv);
267 2 return $$aoa[0][1];
268 }
269
270 8 ▶sub nslookup {
Subroutine nslookup: called CC=2 SCAR=7.0
Pod undocumented
271 8 my $hostname = shift;
272 8 my ($err, $v1, $v2) = getaddrinfo($hostname, "", { socktype => SOCK_RAW });
273 8 my (undef, $ip1) = getnameinfo($v1->{addr}, NI_NUMERICHOST, NIx_NOSERV);
274 8 my (undef, $ip2) = getnameinfo($v2->{addr}, NI_NUMERICHOST, NIx_NOSERV);
275 8 ▶ if (valid4($ip1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (valid4($ip1)) { } 8 0 8
276 8 return $ip1, $ip2;
277 } else {
278 2 return $ip2, $ip1;
279 }
280 }
281
282 45 ▶END {
Subroutine END: called CC=2 SCAR=6.9
283 45 ▶ if(defined $opt_versions){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $App::ipchgmon::opt_versions) 21 24 45
284 33 print
285 "\nModules, Perl, OS, Program info:\n",
286 " Getopt::Long $Getopt::Long::VERSION\n",
287 " Pod::Usage $Pod::Usage::VERSION\n",
288 " Data::Dumper $Data::Dumper::VERSION\n",
289 " Data::Validate::Email $Data::Validate::Email::VERSION\n",
290 " Data::Validate::IP $Data::Validate::IP::VERSION\n",
291 " DateTime $DateTime::VERSION\n",
292 " DateTime::Format::Strptime $DateTime::Format::Strptime::VERSION\n",
293 " Email::Sender::Transport::SMTP $Email::Sender::Transport::SMTP::VERSION\n",
294 " Email::Stuffer $Email::Stuffer::VERSION\n",
295 " LWP::Online $LWP::Online::VERSION\n",
296 " LWP::UserAgent $LWP::UserAgent::VERSION\n",
297 " Socket $Socket::VERSION\n",
298 " Text::CSV $Text::CSV::VERSION\n",
299 " strict $strict::VERSION\n",
300 " Perl $]\n",
301 " OS $^O\n",
302 " $0 $VERSION\n",
303 "\n\n";
304 }
305 }
306
307 12 1;
308
309 =pod
310
311 =head1 NAME
312
313 ipchgmon.pm - Watches for changes to public facing IP addresses
314
315 =head1 SYNOPSIS
316
317 perl ipchgmon.pm --file c:\data\log.txt --server example.com
318
319 Who knows? It might even work. More usually, in a cron job:
320
321 perl ipchgmon --file ~/log.txt \
322 --server back_office_top_shelf \
323 --dnsname example.com \
324 --leeway 86400 \
325 --email serverchange@example.com \
326 --mailserver 192.168.0.2 \
327 --mailfrom ipchgmon@example.com \
328 --mailsubject 'Change of IP address'
329
330 =head1 BACKGROUND
331
332 I and friends run email and other servers at home. I pay for a static IP
333 address with the clause in my contract that force majeur may require a
334 change. Others are on dynamic addresses. Should the public facing address
335 change, we want to know. This modulino is intended to monitor the public
336 IP address and shout for help should the address change. One friend is
337 looking at code to change public DNS records automatically.
338
339 =head1 DESCRIPTION
340
341 This modulino is intended to be run automatically as a cron job. It should
342 check whether the server running it has changed its IP address. If so,
343 messages should be sent to those specified.
344
345 Either IPv4 or IPv6 or both formats can be tested. It might well be that
346 different servers handle the different types.
347
348 There are three issues that may be checked. The first is connectivity. If
349 there is no connectivity, messages should be sent. No further issues will be
350 tested.
351
352 The second issue that will be tested is whether the IP address has changed.
353 The current public-facing IP address is established via internet use and
354 compared to a log file, specified by the "--file" option. If it is not the
355 last entry in the log, messages will be sent.
356
357 Finally and optionally, the DNS name of the server will be used to get
358 another IP address from global DNS. If this is not the last address in the
359 log file, messages will be send unless a leeway has been specified and this
360 has not expired.
361
362 If connectivity is lost, the number of retries and the wait will both be
363 options in a later version. At present, or if the retries all fail, someone
364 should be sent a message.
365
366 The design includes three forms of message, SMS, HTTP and email. It is
367 reasonable to send an email if internet connectivity is lost; the server
368 may be internal. SMS may be harder to justify. HTTP must depend on the
369 location of the server.
370
371 There is currently no facility to send different classes of message to
372 different addresses. There is nothing inherently impossible about writing
373 code to do this if it proves desirable.
374
375 If the IP address has changed, messages should be sent without delay or retries.
376
377 =head1 ON ARGUMENTS AND OPTIONS
378
379 These are processed by Getopt::Long. This means that shorter versions may
380 be used. If the first letter of an argument or option is unique, the call
381 may be reduced to a single minus sign and the first letter. So the "email"
382 argument has an alias, "mailto". But the alias must be specified as
383 "--mailt" at least, while "email" can be reduced to "-e".
384
385 =head1 ARGUMENTS
386
387 --file filename THIS OPTION IS COMPULSORY unless --debug is used.
388 The file "filename" will be created if it does not exist.
389 It may be preferable to use a fully qualified filename.
390 Attempting to write to a non-existent directory or without
391 the necessary permissions will cause an error. The file is
392 plain text containing the IPv4 and IPv6 addresses of the server.
393 --server name THIS OPTION IS COMPULSORY unless --debug is used.
394 It is the name that will be used to identify the machine
395 should internet connectivity be lost or the IP address
396 change. It is not used internally and should not be
397 confused with the dnsname.
398 --dnsname name The name of the server in global DNS.
399 --leeway time This option specifies how many seconds should elapse from
400 an IP address changing on DNS to a second email being sent.
401 It is used only in conjunction with dnsname. If dnsname
402 is unspecified, the value of this option is meaningless.
403 It isn't compulsory, but without it, messages will be sent
404 every time the modulino finds the IP address is not the
405 same as returned by DNS. This is fine if you like getting
406 up every hour during the night. Otherwise, use something
407 like 86400 (one day).
408 --email address Multiple instances of this option are acceptable. An email
409 will be sent to each, if possible.
410 --mailto Synonym for --email.
411 --mailserver Must be followed by the name or ip address of the outbound
412 server. Some systems may have a default for this.
413 --mailport Must be numeric. Will default to 25 if omitted.
414 --mailfrom Most servers will insist on this, but some systems may
415 have a default.
416 --mailsubject Can be omitted if desired. A default would be created
417 which would be different for each message type. Subjects
418 that include spaces would need quoting. The quote character
419 can be OS dependent.
420
421 =head1 OPTIONS
422
423 --help Brief manual
424 --man Full manual
425 --versions Code info
426 --debug Debugging information
427 --singleemail Sends one email at a time. Prevents multiple email
428 addresses appearing in each email and prevents server
429 confusion if different mechanisms are used for different
430 destinations. Yes, it can happen.
431 --4 Check IPv4 addresses. Both will be checked if neither
432 or both options are used.
433 --6 Check IPv6 addresses. Both will be checked if neither
434 or both options are used.
435
436 =head1 TO DO
437
438 =over
439
440 * Implement SMS messages
441
442 * Implement HTTP messages
443
444 * implement config file
445
446 =back
447
448 =head1 COPYRIGHT AND LICENSE
449
450 Copyright (C) 2022 by John Davies
451
452 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
453
454 =head1 CREDITS
455
456 https://www.perlmonks.org/?node_id=155288 for the getopt/usage model.
457
458 Fergus McMenemie for the talk on modulinos (https://www.youtube.com/watch?v=wCW4tpMgdHs).
459
460 Corion (https://www.perlmonks.org/?node=Corion) for solving a naming blunder of mine.
461
462 Slaven Rezic of the CPAN testers for taking the trouble to raise an issue that was causing lots of tester reports and suggesting a solution I would never have found alone.
463
464 Pryrt (https://www.perlmonks.org/?node=pryrt) for spotting that I was ignoring the real cause of the problem Slaven had reported.
465
466 Hv (https://www.perlmonks.org/?node=hv) for patiently, despite my stupidity, showing me how to emulate the testers' issue on my local machine.
467
↑ Summary /usr/local/bin/perldoc »