#!/usr/bin/perl ### ## The aim of this script is to export a list of the Networker clients. ## This export will be stored in csv format and is aimed to be imported ## in an Excel spreadsheet ## BEGIN { eval { require bytes; }; } use strict; no strict "vars"; ## Switch will be used to test the field value and build the csv file use Switch; use Data::Dumper; $self = $0; $self =~ s!^.*/!!; ## Define the command to pilot the library $nsradmin_cmd = "/nsr/sbin/nsradmin"; $tmp_cmd_file = "/tmp/clientlist.cmd"; $tmp_csv_file = "/tmp/clientlist.csv"; ## Create the nsradmin command file sub create_nsradmin_cmd { open (NSRADMFILE, ">$tmp_cmd_file"); print NSRADMFILE ". type: nsr client;scheduled backup: Enabled\n"; print NSRADMFILE "show name;group;comment;\"save set\";\"browse policy\";\"retention policy\"\n"; print NSRADMFILE "print"; close NSRADMFILE; } ## Get the raw list of clients create_nsradmin_cmd(); @rawlist = `$nsradmin_cmd -i $tmp_cmd_file`; ## Hash of hashes to host the csv data %result = ( "_name" => { comment => "Description", groups => { group => "Backup Group", saveset => "Save set", brpol => "Browse Policy", retpol => "Retention Policy" }, }, ); ## Building the hash of hashes, using the client name as a key $svrname=""; $savegrp=""; $grpidx=0; for $rawlist_el(@rawlist) { $field = substr($rawlist_el,0,28); $field =~ s/^\s+|\s+$//g; chomp $field; $data = substr($rawlist_el,30); chomp $data; chop $data; switch ($field) { case "name" { $svrname = $data; $grpidx++; } case "comment" { $result{"$svrname"}{"comment"} = "$data"; } case "group" { #$savegrp = $data; } $result{"$svrname"}{"groups$grpidx"}{"group"} = "$data "; } case "save set" { $result{"$svrname"}{"groups$grpidx"}{"saveset"} .= "$data "; } case "browse policy" { $result{"$svrname"}{"groups$grpidx"}{"brpol"} .= "$data "; } case "retention policy" { $result{"$svrname"}{"groups$grpidx"}{"retpol"} .= "$data "; } case "" { $result{"$svrname"}{"groups$grpidx"}{"saveset"} .= "$data "; } #else { print $field; } } } ## Formating the output in a csv file open ( CSVFILE, ">$tmp_csv_file" ); foreach $svr ( sort keys %result ) { foreach $gpdef ( sort keys %{$result{$svr}} ) { if ( $gpdef =~ /^group/ ) { print CSVFILE $svr.";".$result{$svr}{"comment"}.";"; print CSVFILE $result{$svr}{$gpdef}{"group"}.";".$result{$svr}{$gpdef}{"saveset"}.";".$result{$svr}{$gpdef}{"brpol"}.";".$result{$svr}{$gpdef}{"retpol"}.";\n"; } } } close CSVFILE;