Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Command at a specific Date

by giany (Acolyte)
on Dec 12, 2005 at 10:17 UTC ( [id://515988]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I`m trying to write a script that will execute a command at a specific date.
#!/usr/bin/perl # # # use Time::Local; use Term::ANSIColor; use POSIX qw(strftime); open(IN,"test"); my $actdate = strftime "%H:%d:%m:%M", localtime; my $acthour = strftime "%H",localtime; my $actmonth = strftime "%d",localtime; my $actday = strftime "%m",localtime; my $actmin = strftime "%M",localtime; while( $line = <IN> ){ if ( $line =~ /^#./ ){ } elsif ( $line =~ /^[1-9]/ ) { ($IP,$MAC,$MINI,$MAXI,$VIRUS,$FULL,$BUN,$NAME,$DATA) = spl +it(/\ /,$line); ($HH,$ZZ,$MM,$Min) = split(/:/, $DATA); chomp($DATA); my $comm = "/sbin/iptables-save | /bin/grep $IP | /bin +/grep DNAT >/dev/null 2>&1"; my $comI = "/sbin/iptables -t nat -I PREROUTING -i eth +1 -m tcp -p tcp --dport 8080 -j DNAT --to-destination 10.0.0.251:80 - +s "; my $comD = "/sbin/iptables -t nat -D PREROUTING -i eth +1 -m tcp -p tcp --dport 8080 -j DNAT --to-destination 10.0.0.251:80 - +s "; if ( $actdate eq $DATE) { $check = system($comm); chomp($check); if ( $check == 0 ) { system("$comD $IP"); system("$comI $IP"); } else { system("$comI $IP"); } } else { $check = system($comm); chomp($check); if ( $check == 0 ) { system("$comD $IP"); } else { print "$NAME ok\n"; } } } }
The test file contains some data and the last field $DATA contains a date ( 01:12:12:40) which is hour,day,month,minute. The problem is that I dont know what to do if $actdate != $DATE . Normally if its a smaller date then it should go with the last part of the code:
else { $check = system($comm); chomp($check); if ( $check == 0 ) { system("$comD $IP"); } else { print "$NAME ok\n"; }
But what if its a higher date? Maybe with the $actday,$actmonth,$acthour $ZZ,$MM,$HH variables? Any hints? Thx

Replies are listed 'Best First'.
Re: Command at a specific Date
by Hena (Friar) on Dec 12, 2005 at 10:33 UTC
    Since you are using '/sbin/iptables', I'm assuming that you are on *nix. Wouldn't it be easier to use cron? See 'man crontab' and 'man 5 crontab'.
      or at if you only want to execute the command once.
Re: Command at a specific Date
by tirwhan (Abbot) on Dec 12, 2005 at 10:28 UTC
    hour,day,month,minute

    That seems a little counterintuitive, don't you think? Wouldn't it be better to specify the date in the form minute,hour,day,month (or reverse)?

    If your script is designed to be run periodically you should check for datetimes smaller or equal than the current datetime and execute the action for all instances you find. You'll need a way to mark that an action has already occurred though, because otherwise you'll just repeat them every time your script runs.

    You may also want to look at the "at" utility(man 1 at), that should do most of what you're trying to do here.


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: Command at a specific Date
by gjb (Vicar) on Dec 12, 2005 at 13:34 UTC

    For the sake of completeness, on Windows you could use the TaskSchedular, even accessible from perl with Win32::TaskScheduler.

    Hope this helps some, although probably not the OP, -gjb-

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://515988]
Approved by muntfish
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found