bajangerry has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks!
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.
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.
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.
My code (ugly as it is) is below. Any help appreciated!
#!/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 }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Timed event within a script
by stevieb (Canon) on Mar 07, 2017 at 21:51 UTC | |
by bajangerry (Sexton) on Mar 08, 2017 at 13:23 UTC | |
Re: Timed event within a script
by johngg (Canon) on Mar 07, 2017 at 23:04 UTC | |
Re: Timed event within a script
by hippo (Bishop) on Mar 08, 2017 at 09:36 UTC | |
by bajangerry (Sexton) on Mar 08, 2017 at 13:22 UTC | |
by Marshall (Canon) on Mar 09, 2017 at 01:13 UTC | |
by bajangerry (Sexton) on Mar 09, 2017 at 20:18 UTC | |
by Marshall (Canon) on Mar 10, 2017 at 21:29 UTC | |
Re: Timed event within a script
by huck (Prior) on Mar 07, 2017 at 23:56 UTC | |
Re: Timed event within a script
by madtoperl (Hermit) on Mar 08, 2017 at 10:03 UTC |