SHARE
TWEET

GXMLG

sweenig Apr 14th, 2015 350 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2. #use strict;
  3. #use warnings;
  4. use Getopt::Long; #used for grabbing the command line arguments by name
  5. use Text::CSV; #used for parsing the input CSV file
  6. if ( @ARGV == 0 || @ARGV eq '/?' || @ARGV eq '?') {goto SYNTAX;}
  7. my $debug=0; #set to 1 for debug output, 0 to disable debug output
  8. my ($outnpcxml,$outsacsv,$outnvcsv,$outucmcsv,$npcinspath,$inputfile) = (0,0,0,0,"","sites.csv"); #set default command line options
  9. my ($npcxmlname,$sacsvname,$nvcsvname,$ucmcsvname) = ('NPCGroups.xml','SANetworks.csv','NVScopes.csv','UCMLocations.csv'); #change these values to change the default output file names
  10. GetOptions ( # get command line options (if provided)
  11.         'outnpcxml' => \$outnpcxml,
  12.         'outsacsv' => \$outsacsv,
  13.         'outnvcsv' => \$outnvcsv,
  14.         'outucmcsv' => \$outucmcsv,
  15.         'infile:s' => \$inputfile,
  16.         'npcinspath:s' => \$npcinspath,
  17.         'npcxmlname:s' => \$npcxmlname,
  18.         'sacsvname:s' => \$sacsvname,
  19.         'nvcsvname:s' => \$nvcsvname,
  20.         'ucmcsvname:s' => \$ucmcsvname,
  21.         'verbose' => \$debug,
  22.         'v' => \$debug);
  23. if ($debug){print "Input CSV: $inputfile\nNPC Insertion Path: $npcinspath\nOutput Types\nNPC: $outnpcxml (to file $npcxmlname)\nSA:  $outsacsv (to file $outsacsvname)\nNV:  $outnvcsv (to file $outnvcsvname)\nUCM: $outucmcsv (to file $outucmcsvname)\n";} #output command line parameters
  24. if ($outnpcxml+$outsacsv+$outnvcsv+$outucmcsv == 0) {print "ERROR: You have not specified any output files.\n";goto SYNTAX;}
  25. if ($outnpcxml) { #check for required argument (npcinspath) and insert header text into output xml
  26.         if ($npcinspath eq "") { #can't generage npc xml without the insertion point
  27.                 print "ERROR: The insertion point argument is missing.\nAdd it with '--npcinspath \"\\All Groups\\Sites\"\n\n";
  28.                 goto SYNTAX; #exit the script without doing anything else
  29.         }
  30.         open OUTNPCFILE, ">", $npcxmlname or die $!; #open output file
  31.         print OUTNPCFILE "<GroupTree path=\"" . $npcinspath . "\" inheritDefault=\"true\" xmlns=\"\">\n"; #insert xml header data
  32. }
  33. if ($outsacsv) {open OUTSAFILE, ">", $sacsvname or die $!;} #open output file
  34. if ($outnvcsv) {open OUTNVFILE, ">", $nvcsvname or die $!;} #open output file
  35. if ($outucmcsv) {open OUTUCMFILE, ">", $ucmcsvname or die$!;} #open output file
  36. my $csv = Text::CSV->new(); #create csv instance
  37. open (CSV, "<", $inputfile) or die $!; #open the intput file
  38. my @subnets; #create array to hold input from CSV.  This array will be an array of arrays.
  39. print "Reading input file contents...";
  40. while (<CSV>) { #Import the CSV file into @subnets
  41.         if ($csv->parse($_)) { #parse the current line into the $csv variable
  42.                 my @columns = $csv->fields(); #split the csv contents into an array and store in @columns
  43.                 if ($columns[0] ne 'NetworkNumber') { #skip if the first line contains column headers
  44.                         push @subnets, [ @columns ]; #add the line contents to the @subnets array
  45.                 }
  46.         }
  47. }
  48. close CSV; #get rid of the input file
  49. my @sites; #array to contain the individual sites
  50. my ($i, $n, $aref, $j); #initialize counters and reference variables for loops
  51. if ($debug) {
  52.         print "Network Number\tNetwork Mask\tSite\n"; #print column headers
  53.         for $i ( 0 .. $#subnets ) { #print contents of @subnets
  54.                 $aref = $subnets[$i]; #get the reference of the array of the current row
  55.                 $n = @$aref - 1; #find the length of the current row
  56.                 for $j ( 0 .. $n ) { #cycle through each column of the current row
  57.                         if ($j==2) {print "\t";} #extra tab to line things up
  58.                         print "$subnets[$i][$j]\t"; #output the contents
  59.                 }
  60.                 print "\n";
  61.         }
  62. }
  63. for $i ( 0 .. $#subnets ) { push @sites, $subnets[$i][2]; } #populate the @sites array with site names
  64. my %temphash = map { $_, 1 } @sites; @sites = keys %temphash; #remove duplicate sites
  65. print "Done\nProcessing content and preparing to generate output files...\n";
  66. my $currentsite; #variable to hold the current site name from @sites for comparison when looping through @subnets
  67. if ($outnpcxml) { # print OUTNPCFILE
  68.         print "Generating NPC XML...";
  69.         foreach (@sites){ #loop through sites list
  70.                 $currentsite = $_; #grab the current site name
  71.                 #count subnets per site
  72.                 $j=0; #reset counter
  73.                 for $i ( 0 .. $#subnets) {if ($currentsite eq $subnets[$i][2]){$j++;}} #$j now has the number of subnets for this site
  74.                 #output xml
  75.                 print OUTNPCFILE "<Group name=\"$currentsite\" inherit=\"true\" type=\"site\" desc=\"\" >\n";
  76.                 print OUTNPCFILE " <Rules allowDeletes=\"true\" saveRules=\"true\">\n";
  77.                 #make rule to add devices by IP
  78.                         print OUTNPCFILE "  <Rule add=\"Devices\" name=\"Add Devices by IP Address\">\n";
  79.                         print OUTNPCFILE "   <Match>\n";
  80.                         print OUTNPCFILE "    <Compare using=\"equals\">\n";
  81.                         print OUTNPCFILE "     <Property type=\"Device\" name=\"Address\" />\n";
  82.                         if ($j > 1) {print OUTNPCFILE "     <ValueList>\n";} #if multiple subnets, open ValueList tag
  83.                         for $i ( 0 .. $#subnets) {if ($currentsite eq $subnets[$i][2]){print OUTNPCFILE "      <Value>$subnets[$i][0]/$subnets[$i][1]</Value>\n";}}
  84.                         if ($j > 1) {print OUTNPCFILE "     </ValueList>\n";} #if multiple subnets, close ValueList tag
  85.                         print OUTNPCFILE "    </Compare>\n   </Match>\n  </Rule>\n";
  86.                 if ($outsacsv) { #make rule to add subnets by IP
  87.                         print OUTNPCFILE "  <Rule add=\"Networks\" name=\"Add Networks\">\n";
  88.                         print OUTNPCFILE "   <Match>\n";
  89.                         print OUTNPCFILE "    <Compare using=\"equals\">\n";
  90.                         print OUTNPCFILE "     <Property type=\"Network\" name=\"Subnet\" />\n";
  91.                         if ($j > 1) {print OUTNPCFILE "     <ValueList>\n";} #if multiple subnets, open ValueList tag
  92.                         for $i ( 0 .. $#subnets) {if ($currentsite eq $subnets[$i][2]){print OUTNPCFILE "      <Value>$subnets[$i][0]/$subnets[$i][1]</Value>\n";}}
  93.                         if ($j > 1) {print OUTNPCFILE "     </ValueList>\n";} #if multiple subnets, close ValueList tag
  94.                         print OUTNPCFILE "    </Compare>\n   </Match>\n  </Rule>\n";
  95.                 }
  96.                 if ($outucmcsv) { #make rule to add VoIP locations by name
  97.                         print OUTNPCFILE "  <Rule add=\"VoIP Locations\" name=\"Add UCMonitor Locations\">\n";
  98.                         print OUTNPCFILE "   <Match>\n";
  99.                         print OUTNPCFILE "    <Compare using=\"equals\">\n";
  100.                         print OUTNPCFILE "     <Property type=\"VoIP Location\" name=\"Name\" />\n";
  101.                         print OUTNPCFILE "      <Value>$currentsite</Value>\n";
  102.                         print OUTNPCFILE "    </Compare>\n   </Match>\n  </Rule>\n";
  103.                 }
  104.                 print OUTNPCFILE " </Rules>\n</Group>\n";
  105.         }
  106.         print OUTNPCFILE "</GroupTree>";
  107.         close OUTNPCFILE;
  108.         print "Done\n";
  109. }
  110. if ($outsacsv) { # output SA CSV
  111.         print "Generating SA CSV...";
  112.         for $i ( 0 .. $#subnets) { #loop through subnets list
  113.                 my $regions = 2**(24-$subnets[$i][1]); #calculate the best number of regions
  114.                 if ($regions < 1) { $regions=1; } #use 1 if mask is > 24
  115.                 if ($regions > 256) { $regions=256; } #use the lower value (validate the regions)
  116.                 print OUTSAFILE "$subnets[$i][2],$subnets[$i][0],$subnets[$i][1],$regions\n"; #output to csv
  117.         }
  118.         close OUTSAFILE;
  119.         print "Done\n";
  120. }
  121. if ($outnvcsv) { # output NV CSV
  122.         print "Generating NV CSV...";
  123.         for $i ( 0 .. $#subnets) { print OUTNVFILE "$subnets[$i][0]/$subnets[$i][1]\n"; } #loop through the sites list and export it
  124.         close OUTNVFILE;
  125.         print "Done\n";
  126. }
  127. if ($outucmcsv) { # output UCM CSV
  128.         print "Generating UCM CSV...";
  129.         foreach (@sites){
  130.                 $currentsite = $_; #grab current site name
  131.                 #count subnets per site
  132.                 $j=0; #reset counter
  133.                 for $i ( 0 .. $#subnets) {if ($currentsite eq $subnets[$i][2]){$j++;}} #$j now has the number of subnets for this site
  134.                 #make rule to add devices by IP
  135.                         print OUTUCMFILE "LOCATION,$currentsite\n"; #for each site insert a Location line
  136.                         for $i ( 0 .. $#subnets) { if ($currentsite eq $subnets[$i][2]){ print OUTUCMFILE "SUBNET,$currentsite,$subnets[$i][0]/$subnets[$i][1],$subnets[$i][0],$subnets[$i][1]\n";}} #if the current subnet belongs to the current site, output the subnet line
  137.         }
  138.         close OUTUCMFILE;
  139.         print "Done\n";
  140. }
  141. goto THEEND;
  142. SYNTAX:
  143. print <<FOO;
  144. \nThis script outputs any combination of configuration files for the NetQoS
  145. suite of products. You must install strawberry perl and Text::CSV,
  146. Text::CSV_XS, and Getopt::Long. To install CPAN modules, run cpan [module name]
  147. from the command prompt.
  148. \nExample: cpan Text::CSV\n
  149. This script was created by Stuart Weenig (C)2012.  For more information visit
  150. http://stuart.weenig.com. This script may be redistributed as long as all the
  151. files are kept in their original state.\n
  152.  
  153. Current Version: 2.0
  154.  
  155. Usage: PERL gxmlg.pl [-outnpcxml] [-outsacsv] [-outnvcsvnv] [-outucmcsv]
  156.                      [-infile NAMEOFINFILE] [-npcinspath INSERTPATH]
  157.                      [-npcxmlname NPCXMLFILE] [-sacsvname SACSVFILE]
  158.                      [-nvcsvname NVCSVFILE] [-ucmcsvname UCMCSVFILE]
  159.  
  160.     -infile NAMEOFINFILE        Specifies the name of the Sites file to be
  161.                                 imported. (If omitted: sites.csv)
  162.     -outnpcxml                  Output an NPC XML groups definition file.
  163.     -npcxmlname NPCXMLFILE      Name of the NPC file to be output. (If
  164.                                 omitted: NPCGroups.xml)
  165.     -npcinspath INSERTPATH      The path to the group that will serve as
  166.                                 the insertion point.  Required if using
  167.                                 -outnpcxml option.
  168.     -outsacsv                   Output a SA networks CSV file.
  169.     -sacsvname SACSVFILE        Name of the SA file to be output. (If omitted:
  170.                                 SANetworks.csv)
  171.     -outnvcsv                   Output a NV discovery scopes file.
  172.     -nvcsvname NVCSVFILE        Name of the NV file to be output. (If omitted:
  173.                                 NVScopes.csv)
  174.     -outucmcsv                  Output a UCMonitor locations file.
  175.     -ucmcsvname UCMCSVFILE      Name of the UCMonitor locations file to be output.
  176.                                 (If omitted: UCMLocations.csv)
  177.     -v|-verbose                 Runs with verbose output, including an echo of the
  178.                                 content after it is imported, allowing verification
  179.                                 of the imported data.
  180. You must install strawberry perl and Text::CSV, Text::CSV_XS, and Getopt::Long.
  181. To install CPAN modules, run cpan [module name] from the command prompt
  182. Example: cpan Text::CSV
  183.  
  184. The infile must be a CSV with the following columns:
  185. NetworkNumber,NetworkMask,Site
  186.  
  187. Here is an example:
  188. NetworkNumber,NetworkMask,Site
  189. 10.1.0.0,16,New York
  190. 10.2.0.0,15,Los Angeles (Bldg 1)
  191. 10.4.0.0,16,Los Angeles (Bldg 2 - Corp)
  192. 10.5.0.0,16,Los Angeles (Bldg 2 - Sales)
  193. 172.16.1.0,24,Los Angeles (Bldg 2 - DC)
  194. 10.100.0.0,16,London
  195. 10.150.0.0,16,Singapore
  196. 192.168.150.0,24,Singapore
  197.  
  198.  
  199. If the server you will be running this on doesn't have access to the internet, you
  200. won't be able to install the modules automatically (since they have to be downloaded
  201. from the internet).  The solution is to download the tarballs from www.cpan.org and
  202. extract them using winzip or winrar or 7z.  You might need to extract several times
  203. until you get just the folder with the files in them.  Then copy them to your server.
  204. Open a command prompt and cd to the directory containing Makefile.pl (you'll have to
  205. do this for each module).  Then execute the following:
  206.  
  207.      perl Makefile.pl && dmake && dmake test && dmake install
  208.  
  209. For the text modules, do Text::CSV first, then Text::CSV_XS.
  210. FOO
  211. THEEND:
RAW Paste Data
Top