diff options
Diffstat (limited to 'scripts/system/vyatta_update_ntp.pl')
-rwxr-xr-x | scripts/system/vyatta_update_ntp.pl | 75 |
1 files changed, 63 insertions, 12 deletions
diff --git a/scripts/system/vyatta_update_ntp.pl b/scripts/system/vyatta_update_ntp.pl index a162f858..118cfc18 100755 --- a/scripts/system/vyatta_update_ntp.pl +++ b/scripts/system/vyatta_update_ntp.pl @@ -21,27 +21,78 @@ use strict; use lib "/opt/vyatta/share/perl5"; use Vyatta::Config; +use NetAddr::IP; die "$0 expects no arguments\n" if (@ARGV); -# Weed existing servers from config -print grep {! /^server/ } <STDIN>; +sub ntp_format { + my ($cidr) = @_; + my $ip = NetAddr::IP->new($cidr); + die "$cidr: not a valid IP address" unless $ip; -my $cfg = new Vyatta::Config; -$cfg->setLevel("system ntp"); - -foreach my $server ($cfg->listNodes("server")) { - print "server $server iburst"; - for my $property (qw(dynamic noselect preempt prefer)) { - print " $property" if ($cfg->exists("server $server $property")); + my $address = $ip->addr(); + my $mask = $ip->mask(); + + if ($mask eq '255.255.255.255') { + if ($ip->version() == 6) { + return "-6 $address"; + } else { + return "$address"; + } + } else { + if ($ip->version() == 6) { + return "-6 $address mask $mask"; + } else { + return "$address mask $mask"; + } } - print "\n"; } -exit 0; +my @ntp; +if (-e '/etc/ntp.conf') { + open (my $file, '<', '/etc/ntp.conf') + or die("$0: Error! Unable to open '/etc/ntp.conf' for input: $!\n"); + @ntp = <$file>; + close ($file); +} +open (my $output, '>', '/etc/ntp.conf') + or die("$0: Error! Unable to open '/etc/ntp.conf' for output: $!\n"); + +my $cfg = new Vyatta::Config; +$cfg->setLevel("system ntp"); +foreach my $line (@ntp) { + if ($line =~ /^# VyOS CLI configuration options/) { + print $output $line; + print $output "\n"; + last; + } else { + print $output $line; + } +} +if ($cfg->exists("server")) { + print $output "# Servers\n\n"; + foreach my $server ($cfg->listNodes("server")) { + my $server_addr = ntp_format($server); + print $output "server $server_addr iburst"; + for my $property (qw(dynamic noselect preempt prefer)) { + print $output " $property" if ($cfg->exists("server $server $property")); + } + print $output "\nrestrict $server_addr nomodify notrap nopeer noquery\n"; + } + print $output "\n"; +} +if ($cfg->exists("client")) { + print $output "# Clients\n\n"; + my @clients = $cfg->returnValues("client address"); + foreach my $client (@clients) { + my $address = ntp_format($client); + print $output "restrict $address nomodify notrap nopeer\n"; + } + print $output "\n"; +} - +exit 0; |