perlquestion
bajangerry
<p> Hello Monks!</p>
<p> I need some help with trying to find a way to have a single script complete two functions at the same time and then compile this into an executable... if that is at all possible. </p>
<p> The first point of the script is to constantly monitor a IP address and port for data and when any data is presented on that port number the script parses it and sends an alert if it finds the word "Emergency". This portion of the project is working fine at the moment.</p>
<p> The second function is for the script to send an "I'm alive" email every 30 mins or so but I can't seem to figure out how I can do this within the same script as the above even with subroutines in place. Currently I just have a simple sleep subroutine within the main script but it is not getting to that because I believe the main script is not allowing it to get that far.</p>
<p>My code (ugly as it is) is below. Any help appreciated!</p>
<code>
#!/usr/bin/perl
use strict;
use IO::Socket;
use POSIX;
use warnings;
use MIME::Lite;
use Config::Simple;
use Win32();
sub logMonitor{
# Reading configuration parameters from the config.ini file
my $cfg = new Config::Simple();
$cfg->read('config.ini');
my $HOST = $cfg->param("pbx");
my $COMPANY = $cfg->param("company");
my $COMMUNITY = $cfg->param("community");
my $PORT = $cfg->param("port");
for(;; ){
my $sock = new IO::Socket::INET(PeerAddr => $HOST, PeerPort => $PORT,Proto => "tcp",)
or die "Cannot connect to PBX at address: $HOST port: $PORT: $!";
while (<$sock>) {
s/^\0+//; # Remove leading null characters
print $_;
chomp ($_);
if ($_ =~m"Emergency Call From Extension") {
filePrint();
}
}
}
} #End of monitor subroutine
sub filePrint{ #write data to file with date and time stamp
my $data = $_;
print $data;
my $file = strftime '%Y-%m-%d_%H-%M-%S.txt', localtime;
if (-f $file){
open (my $fh,'>>', $file);
print $fh "$_";
close $file;
}
else {
open (my $fh,'>', $file);
print $fh "$_";
close $file;
}
}# End of filePrint routine
sub keepAlive{
while () {
sleep 20;
print " It's alive!!!";
}
}
#print disclaimer and wait for "y" to continue or exit
my $disclaimer = "disclaimer.txt";
open(my $fh, '<:encoding(UTF-8)', $disclaimer)
or die "Could not open file '$disclaimer' $!";
while (my $row = <$fh>) {
chomp $row;
Win32::MsgBox($row);
}
print "\nDo you accept this disclaimer and continue at your own risk?(Y/N)?"; # first question yes/no
my $input = <STDIN>;
chomp $input;
if ($input =~ m/^[Y]$/i){ # Start of processing
logMonitor(); #Open the log monitoring subroutine
keepAlive(); #start the alive email message
}
</code>