#!/usr/bin/perl
#
-# Copyright (c) 2013 Michel Ketterle, Steven Schubiger
+# Copyright (c) 2013, 2015 Michel Ketterle, Steven Schubiger
#
# This file is part of distdns.
#
use warnings;
use constant false => 0;
-use Config::Tiny;
-use Digest::MD5 qw(md5_hex);
-use Fcntl ':flock';
-use File::Spec::Functions qw(catfile rel2abs);
-use FindBin qw($Bin);
-use Getopt::Long qw(:config no_auto_abbrev no_ignore_case);
-use JSON qw(decode_json);
-use LWP::UserAgent;
-use Sys::Hostname qw(hostname);
-use Tie::File;
+BEGIN
+{
+ my @modules = (
+ [ 'Config::Tiny', [ ] ],
+ [ 'Digest::MD5', [ qw(md5_hex) ] ],
+ [ 'Fcntl', [ qw(:flock) ] ],
+ [ 'File::Spec::Functions', [ qw(catfile rel2abs) ] ],
+ [ 'FindBin', [ qw($Bin) ] ],
+ [ 'Getopt::Long', [ qw(:config no_auto_abbrev no_ignore_case) ] ],
+ [ 'JSON', [ qw(decode_json) ] ],
+ [ 'LWP::UserAgent', [ ] ],
+ [ 'POSIX', [ qw(strftime) ] ],
+ [ 'Sys::Hostname', [ qw(hostname) ] ],
+ [ 'Tie::File', [ ] ],
+ );
+ my (@missing, @import);
+ foreach my $module (@modules) {
+ unless (eval "require $module->[0]; 1") {
+ push @missing, $module->[0];
+ next;
+ }
+ unless (eval { $module->[0]->import(@{$module->[1]}); 1 }) {
+ push @import, $module->[0];
+ }
+ }
+ if (@missing || @import) {
+ warn <<"EOT";
+Modules missing: @missing
+Import failures: @import
+EOT
+ exit 1;
+ }
+}
-my $VERSION = '0.05';
+my $VERSION = '0.07';
my $conf_file = catfile($Bin, 'client.conf');
sub usage
{
+ warn "$_[0]\n" if defined $_[0];
print <<"USAGE";
-Usage: $0
+Usage: $0 [options]
-d, --debug server debugging
-h, --help this help screen
-i, --init initialize session data
GetOptions(\%opts, qw(d|debug h|help i|init l|list)) or usage();
usage() if $opts{h};
+usage('Cannot combine --init and --list') if $opts{i} && $opts{l};
+
my $config = Config::Tiny->new;
$config = Config::Tiny->read($conf_file);
{
my ($section, $options) = @_;
- _die "Section $section missing in $conf_file\n" unless exists $config->{$section};
+ _die "Section '$section' missing in $conf_file\n" unless exists $config->{$section};
my %options;
@options{@$options} = @{$config->{$section}}{@$options};
foreach my $option (@$options) {
- _die "Option $option not set in $conf_file\n" unless defined $options{$option} && length $options{$option};
+ _die "Option '$option' not set in $conf_file\n" unless defined $options{$option} && length $options{$option};
}
return @options{@$options};
my $get_session = sub
{
open(my $fh, '<', $session_file) or _die "Cannot open $session_file for reading: $!\nPerhaps try running --init\n";
- my $session = do { local $/; <$fh> };
- chomp $session;
+ chomp(my $session = <$fh>);
close($fh);
return $session;
die "$0: [server] $data->{error}" if defined $data->{error};
- $save_session->($session) if $opts{i};
-
- if ($opts{l}) {
+ if ($opts{i}) {
+ $save_session->($session);
+ }
+ elsif ($opts{l}) {
format STDOUT_TOP =
-IP Name PC Netz
-=============================================================================
+IP Name PC Netz Aktualisiert
+====================================================================================================
.
foreach my $entry (sort { $a->{netz} cmp $b->{netz} } @{$data->{entries}}) {
+ my $updated = strftime '%Y-%m-%d %H:%M:%S', localtime $entry->{time};
format STDOUT =
-@<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<
-@$entry{qw(ip name pc netz)}
+@<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<
+@$entry{qw(ip name pc netz)}, $updated
.
write;
}
- exit;
- }
-
- my %list;
- foreach my $entry (@{$data->{entries}}) {
- my $host = "$entry->{ip}\t" . join '.', @$entry{qw(name pc netz)};
- push @{$list{$entry->{netz}}}, $host;
}
+ else {
+ my %list;
+ foreach my $entry (@{$data->{entries}}) {
+ my $host = "$entry->{ip}\t" . join '.', @$entry{qw(name pc netz)};
+ push @{$list{$entry->{netz}}}, $host;
+ }
- my $o = tie my @hosts, 'Tie::File', $hosts_file or _die "Cannot tie $hosts_file: $!\n";
- $o->flock(LOCK_EX);
-
- foreach my $network (keys %list) {
- my %indexes;
- for (my $i = 0; $i < @hosts; $i++) {
- if ($hosts[$i] =~ /^\#$network\#$/i) {
- $indexes{start} = $i;
- }
- elsif (exists $indexes{start} && $hosts[$i] =~ /^\#\/$network\#$/i) {
- $indexes{end} = $i;
- my $count = ($indexes{end} - $indexes{start} > 1)
- ? $indexes{end} - $indexes{start} - 1
- : 0;
- splice @hosts, $indexes{start} + 1, $count, @{$list{$network}};
- last;
+ my $o = tie my @hosts, 'Tie::File', $hosts_file or _die "Cannot tie $hosts_file: $!\n";
+ $o->flock(LOCK_EX);
+
+ foreach my $network (keys %list) {
+ my %indexes;
+ for (my $i = 0; $i < @hosts; $i++) {
+ if ($hosts[$i] =~ /^\#$network\#$/i) {
+ $indexes{start} = $i;
+ }
+ elsif (exists $indexes{start} && $hosts[$i] =~ /^\#\/$network\#$/i) {
+ $indexes{end} = $i;
+ my $count = ($indexes{end} - $indexes{start} > 1)
+ ? $indexes{end} - $indexes{start} - 1
+ : 0;
+ splice @hosts, $indexes{start} + 1, $count, @{$list{$network}};
+ last;
+ }
}
}
- }
- undef $o;
- untie @hosts;
+ undef $o;
+ untie @hosts;
+ }
}
else {
warn $response->status_line, "\n";