Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, Im playing with DIG command in PERL and wanted to send email to someone when the A-record result is not as expected. My script for DIG is below:
#!/usr/bin/perl -w use strict; open(INPUT_FILE, "<HOSTLIST>") or die "Cannot open: $!"; my @array = <INPUT_FILE>; close(INPUT_FILE) or die "Cannot close: $!"; for my $line (@array) { my @output = `dig A $line`; print("="x50,"\n"); print ("@output\n\n"); }
Then i run this script on a cron and piped the result to a file which i tailed to be shown in my terminal. Now it's difficult to notice if the resulting A record changed. I have to be glued to the terminal all the time. I was thinking maybe, if the result is not as expected, the script can email and notify me. Is that possible? Thanks..! n0jp7

Replies are listed 'Best First'.
Re: Send Email If Result of Dig command
by Lotus1 (Vicar) on Sep 10, 2018 at 12:49 UTC

    It is definitely possible to send email from Perl. How you send it depends on if you have a mailserver available on your network.

    If a mailserver is available some people use the system's sendmail or use Net::SMTP. There are other modules available that handle the low level details for this. MIME::Lite is a common one but it is not suggested for new programs. The documentation for MIME::Lite recommends using Email::MIME or MIME::Entity and Email::Sender. I still use MIME::Lite for my simple needs.

    If no mailserver is available then you'll need a module to connect you to a webemail account on something like GMail. Try Sending mails via gmail, Mail::Webmail::Gmail, or Email::Send::Gmail.

Re: Send Email If Result of Dig command
by PeterKaagman (Beadle) on Sep 10, 2018 at 11:23 UTC

    Nothing as boring as waiting for a anomaly :D

    Not that Im really sure what output you get from <HOSTLIST>, but I would itterate the lines in that array. On every itteration check if a regex (matching the expected/correct format) would not match (negate the regex) and act on that.... send an email... print a report... whatever...

    Peter