Untitled

From Fiery Duck, 1 Year ago, written in Plain Text, viewed 475 times.
URL http://stoicbaby.com/pastebin/view/e6061d2d Embed
Download Paste or View Raw
  1. #! /usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. my $conf = "/opt/mod_security/modsec_localips.txt";
  5. my $time = time;
  6. my $tmpname = "$conf.new.$time";
  7.  
  8. my @ips = qx(/sbin/ip addr) =~ /((?:\d+\.){3}\d+)/g;
  9. my $old = slurp($conf);
  10. my $new = join("\n", map { "/$_/" } sort @ips)."\n";
  11. exit if $old eq $new;
  12.  
  13. open my $tmp, '>', $tmpname or die "Unable to open $tmpname for writing : $!";
  14. chmod 0640, $tmpname;
  15. print {$tmp} $new;
  16. close $tmp && rename $tmpname => $conf;
  17. exec "/usr/sbin/httpd -k graceful";
  18.  
  19. sub slurp {
  20.         my ($file) = @_;
  21.         local $/ = undef;
  22.         open my $f, '<', $file or warn "Unable to open $file for reading : $!";
  23.         my $content = <$f>;
  24.         close $f;
  25.         return $content
  26. }

Reply to "Untitled"

Here you can reply to the paste above