]> git.refcnt.org Git - distdns.git/commitdiff
List remote entries
authorSteven Schubiger <stsc@refcnt.org>
Sun, 15 Sep 2013 14:16:39 +0000 (16:16 +0200)
committerSteven Schubiger <stsc@refcnt.org>
Sun, 15 Sep 2013 14:16:39 +0000 (16:16 +0200)
client.pl
distdns.pod
server.cgi

index 9992c9de5d97b96d10048631deb0c3b240eff632..e573f9dadfe271826fb5d91844cd090ac4034cce 100755 (executable)
--- a/client.pl
+++ b/client.pl
@@ -45,12 +45,13 @@ Usage: $0
     -d, --debug    server debugging
     -h, --help     this help screen
     -i, --init     initialize session data
+    -l, --list     list remote entries
 USAGE
     exit;
 }
 
 my %opts;
-GetOptions(\%opts, qw(d|debug h|help i|init)) or usage();
+GetOptions(\%opts, qw(d|debug h|help i|init l|list)) or usage();
 usage() if $opts{h};
 
 my $config = Config::Tiny->new;
@@ -104,6 +105,7 @@ my %params = (
     name    => $name,
     debug   => $opts{d} || false,
     init    => $opts{i} || false,
+    list    => $opts{l} || false,
     session => $session,
 );
 
@@ -122,6 +124,21 @@ if ($response->is_success) {
 
     $save_session->($session) if $opts{i};
 
+    if ($opts{l}) {
+        format STDOUT_TOP =
+IP                 Name               PC                      Netz
+=============================================================================
+.
+        foreach my $entry (sort { $a->{netz} cmp $b->{netz} } @{$data->{entries}}) {
+            format STDOUT =
+@<<<<<<<<<<<<<<    @<<<<<<<<<<<<<<    @<<<<<<<<<<<<<<<<<<<    @<<<<<<<<<<<<<<
+@$entry{qw(ip name pc netz)}
+.
+            write;
+        }
+        exit;
+    }
+
     my %list;
     foreach my $entry (@{$data->{entries}}) {
         my $host = "$entry->{ip}\t" . join '.', @$entry{qw(name pc netz)};
index 920deb4a91d92f52324f2182adaaca7d2cdf0882..eaec8fe03b8547ac09d6dae5e096d4330e5b3f7c 100644 (file)
@@ -24,6 +24,7 @@ machines.
  -d, --debug    server debugging
  -h, --help     this help screen
  -i, --init     initialize session data
+ -l, --list     list remote entries
 
 =over 4
 
@@ -39,6 +40,10 @@ Show usage screen.
 
 Initialize session data prior to using client/server script regularly.
 
+=item * C<-l, --list>
+
+List remote JSON entries.
+
 =back
 
 =head1 AUTHORS
index 67b522efb7ebd8c8ae7aeed18ce433409ab04a53..77e979b66a6253c3a0fb6297155ab2703059a03b 100755 (executable)
@@ -34,7 +34,7 @@ my $conf_file = catfile($Bin, 'server.conf');
 
 my $query = CGI->new;
 
-my @params = qw(netz pc name debug init session);
+my @params = qw(netz pc name debug init list session);
 my %params;
 
 foreach my $param (@params) {
@@ -115,26 +115,34 @@ if (exists $access{$params{name}} && grep /^$params{pc}$/i, @{$access{$params{na
 
     my $data = defined $json && length $json ? decode_json($json) : [];
 
-    for (my $i = 0; $i < @$data; $i++) {
-        if ($params{netz} eq $data->[$i]->{netz}
-         && $params{pc}   eq $data->[$i]->{pc}
-         && $params{name} eq $data->[$i]->{name}) {
-            splice @$data, $i--, 1;
-        }
+    if ($params{list}) {
+        close($fh);
+
+        print $query->header('application/json');
+        print encode_json({ entries => $data, error => undef });
     }
-    push @$data, { map { $_ => $params{$_} } qw(netz pc name ip) };
+    else {
+        for (my $i = 0; $i < @$data; $i++) {
+            if ($params{netz} eq $data->[$i]->{netz}
+             && $params{pc}   eq $data->[$i]->{pc}
+             && $params{name} eq $data->[$i]->{name}) {
+                splice @$data, $i--, 1;
+            }
+        }
+        push @$data, { map { $_ => $params{$_} } qw(netz pc name ip) };
 
-    seek($fh, 0, 0)  or die "Cannot seek to start of $json_file: $!\n";
-    truncate($fh, 0) or die "Cannot truncate $json_file: $!\n";
+        seek($fh, 0, 0)  or die "Cannot seek to start of $json_file: $!\n";
+        truncate($fh, 0) or die "Cannot truncate $json_file: $!\n";
 
-    print {$fh} encode_json($data);
+        print {$fh} encode_json($data);
 
-    close($fh);
+        close($fh);
 
-    my @data = grep $_->{netz} eq $params{netz}, @$data;
+        my @data = grep $_->{netz} eq $params{netz}, @$data;
 
-    print $query->header('application/json');
-    print encode_json({ entries => \@data, error => undef });
+        print $query->header('application/json');
+        print encode_json({ entries => \@data, error => undef });
+    }
 }
 else {
     die "Access not permitted\n";