From: Nicolas Boisselier Date: Thu, 14 Mar 2019 05:51:55 +0000 (+0000) Subject: bin/cron_d2crontab X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=b5070c993b61eacc6bcf74e672d02a350d7e710f;p=nb.git bin/cron_d2crontab --- diff --git a/bin/cron_d2crontab b/bin/cron_d2crontab new file mode 100755 index 00000000..be81e864 --- /dev/null +++ b/bin/cron_d2crontab @@ -0,0 +1,244 @@ +#!/usr/bin/env perl +use strict; +use warnings; +################################################################################# +# +# VERSION +# +################################################################################# +my $VERSION = '0.0.1'; +# NB 14.03.19 +# - create script: ? + +################################################################################# +# +# GLOBALS +# +################################################################################# +my ($NAME) = $0 =~ m,([^/]+)$,; + +################################################################################# +# +# ARGS +# +################################################################################# +my $VERBOSE = $main::VERBOSE = 1; +my $DEBUG = $main::DEBUG = 0; + +my %Opt = ( +); +get_options(\%Opt); +help() unless @ARGV; +$main::_DATA_ = undef; + +################################################################################# +# +# BEGIN +# +################################################################################# +use Data::Dumper; +my $diff = 0; +my $exp = shift @ARGV; + +# +# Parse src file +# +my @src = map {chomp; $_} grep {/^(\w+=|\*|\d+)/} <>; + +for (my $i=0;$i<@src;$i++) { + + $_ = $src[$i]; + + if (@_=/^ + ( + (?:[\*\d\/-]+\s+){5} + |(?:@\w+\s+) + ) + (\S+)\s+(.*)$/x) { + my $when = $1; chop $when; + my $user = $2; + my $cmd = $3; + print Dumper(\@_); + exit; + } + my @F = split(" ",$src[$i]); + + next if $src[$i] !~ /^[\*\d]/ or @F<6; + if ($F[5] ne "root"){ + $_ = join(" ",@F[6..$#F]); + s/"/\\"/g; + @_=qq|su -l "$F[5]" -c "$_"|; + @F = (@F[0..4],"",@_); + }; + + $src[$i] = join(" ",@F[0..4,6..$#F]); +} +0 and warn join("\n",@src); +my %src = map {$_=>1} @src; + +# +# Fiter existing crontab +# +@ARGV = ("crontab -l|"); +my @cron = map {chomp; $_} <>; +my %cron = map {$_=>1} @cron; + +for (@cron) { + 0 and print STDERR "-$_\n" if $exp and !$src{$_} and /$exp/; + if ($exp and !$src{$_} and /$exp/) { + $diff=1 ; + next; + } + print "$_\n"; +} + +# +# Add src +# +for (@src) { + $diff=2 if !$cron{$_} and print "$_\n"; +} + +exit $diff ? 0 : 1; + +################################################################################# +# +# END +# +################################################################################# +exit 0; + +################################################################################# +# +# Functions +# +################################################################################# +sub help { +#------------------------------------------------------------------------------ +# Print help and exit +#------------------------------------------------------------------------------ + + require 'Pod/Usage.pm' unless $INC{'Pod/Usage.pm'}; + + # Substitutions + sub pod_env { + my $v = ''; + eval '$v = ref(\\'.$_[0].') eq "ARRAY" ? join(" ",'.$_[0].') : '.$_[0].'; return defined $v ? $v : qq|UNDEF|;'; + return $v; + } + + $main::_DATA_ =~ s/([@\$][A-Z_a-z\{\}]+)/pod_env($1)/eg; + + my $in; + open($in,'<',\$main::_DATA_); + + open(STDOUT,"|perl -pe 's/\.$$//g'".(($ENV{PAGER}||'') eq 'less' ? "|less -FRi" : "")); + my $opts = { + -input => $in, + -ouput => \*STDOUT, + -exitval => 'noexit', + -sections => [qw(SYNOPSIS DESCRIPTION OPTIONS)], + -verbose => ($Opt{'help'} ? 99 : 3), + }; + + Pod::Usage::pod2usage($opts); + close $in; + close STDOUT; + + exit 0; +} + +#------------------------------------------------------------------------------ +# Print version and exit +#------------------------------------------------------------------------------ +sub version { print "$NAME: version [$VERSION]\n"; exit 0; } + +#------------------------------------------------------------------------------ +# Get options from pod +#------------------------------------------------------------------------------ +sub get_options { + + use Getopt::Long qw(:config no_ignore_case no_auto_abbrev); + + my @Opt; + + sub pod_opt { + local $_; + my $o = shift; + $o =~ s/(=.|[\+\-\!]$)//; + $o = join(", ",map{"-$_"} split(/[\|,:;]/,$o)); + return "$o"; + } + + while () { + s/option\[([^\]]+)\]/push(@Opt,$1) and pod_opt($1)/eg; + $main::_DATA_ .= $_; + } + + GetOptions($_[0],@Opt) || exit -1; + + help() if $_[0]{'help'} or $_[0]{'man'}; + version() if $_[0]{'version'}; + + $main::VERBOSE = $VERBOSE = $_[0]{'verbose'} if defined $_[0]{'verbose'}; + $main::DEBUG = $DEBUG = $_[0]{'debug'} if defined $_[0]{'debug'}; + +} + +__DATA__ + +=head1 NAME + +$NAME - Script to do ... + +=head1 SYNOPSIS + +Quick usage! + +=over + +=item $NAME --verbose + +=item $NAME --help + +=back + +=head1 DESCRIPTION + +Description! + +=head1 OPTIONS + + -option[verbose|v+] Verbose mode: increase the verbosity level. + -option[debug+] Debug mode: increase the verbosity level. + -option[version|V] Print version (default: $VERSION) + -option[help|h|?] Print a brief help message and exits. + -option[man] Print the manual page and exits. + +=cut + +=head1 EXAMPLES + +... + +=head1 REQUIRES + +Getopt::Std, Pod::Usage + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2019 Nicolas Boisselier + +This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + +See . + +=head1 SEE ALSO + +perl(1), http://perldoc.perl.org/perlpodstyle.html + +=head1 AUTHOR + +Nicolas Boisselier + +=cut diff --git a/etc/profile.d/functions b/etc/profile.d/functions index b372c21e..da7126d3 100755 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -873,13 +873,15 @@ cron_d_install() { # Case 2 - crontab # +#@f = map {chomp; $_} grep {!/^(@|#|\s*$)/} <>; cron=$(perl -e ' $diff = 0; $e = shift @ARGV; -@f = map {chomp; $_} grep {!/^(#|\s*$)/} <>; +@f = map {chomp; $_} grep {/^(\w+=|\*|\d+)/} <>; for ($i=0;$i<@f;$i++) { @F = split(" ",$f[$i]); + next if $f[$i] !~ /^[\*\d]/ or @F<6; if ($F[5] ne "root"){ $_ = join(" ",@F[6..$#F]); @@ -889,6 +891,7 @@ for ($i=0;$i<@f;$i++) { }; $f[$i] = join(" ",@F[0..4,6..$#F]); } +0 and warn join("\n",@f); %f = map {$_=>1} @f; @ARGV = ("crontab -l|");