Skip to content

Instantly share code, notes, and snippets.

@bldewolf
Last active August 29, 2015 13:58
Show Gist options
  • Save bldewolf/10011011 to your computer and use it in GitHub Desktop.
Save bldewolf/10011011 to your computer and use it in GitHub Desktop.
dibbler-client/radvd integrator
#!/usr/bin/perl
use warnings;
use strict;
use File::Compare;
open(my $dib, "/etc/dibbler/radvd.conf") or die "Failed to open dibbler config: $!";
my $int; # current interface
my $start; # used to skip non-prefix lines from dibbler
my %ints;
while(<$dib>) {
chomp;
if(/^{/ or /^\s*($|#)/) {
next;
} elsif(/^};/) {
$int = undef;
$start = undef;
} elsif(/^interface\s*(\S*)\s*/) {
$int = $1;
} else {
$start = 1 if(/prefix/);
next if !$start;
s/ /\t/; # 5 space tabs? wat
s/ /\t/g;
$ints{$int} .= "$_\n";
# adding options dibbler doesn't add
$ints{$int} .= "\t\tDeprecatePrefix on;\n\t\tAdvRouterAddr on;\n"
if(/prefix/);
}
}
close $dib;
open(my $newrad, ">", "/etc/radvd.conf.tmp") or die "Failed to open new radvd config: $!";
open(my $rad, "/etc/radvd.conf") or die "Failed to open radvd config: $!";
my $skip; # used to skip previous dibbler conf
while(<$rad>) {
print $newrad $_ if !$skip;
chomp;
if(/^interface\s*(\S*)\s*/) {
$int = $1;
} elsif(/#dibbler/) {
$skip = !$skip;
print $newrad $ints{$int}
if $skip && exists $ints{$int};
print $newrad "$_\n"
if !$skip;
}
}
close $rad;
close $newrad or die "Failed to close new radvd config: $!";
if(!compare("/etc/radvd.conf.tmp", "/etc/radvd.conf")) {
unlink("/etc/radvd.conf.tmp");
exit;
}
rename("/etc/radvd.conf.tmp", "/etc/radvd.conf")
or die "Failed to rename new to old: $!";
exec("pkill -HUP -o radvd")
or die "Failed to exec pkill: $!";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment