#!/usr/bin/perl use warnings; use strict; use 5.010; use CGI; use DBI; use Data::Dumper; use lib '../../include'; use nms; # Grab from .htaccess-authentication my $user = $ENV{'REMOTE_USER'}; my $dbh = nms::db_connect(); $dbh->{AutoCommit} = 0; # Ugly casting, found no other way my $sinsert = $dbh->prepare( "INSERT INTO squeue (gid, added, priority, addr, sysname, cmd, author) VALUES(?::text::int, timeofday()::timestamptz, ?::text::int, ?::text::inet, ?, ?, ?)") or die "Could not prepare sinsert"; my $sgetip = $dbh->prepare("SELECT ip FROM switches WHERE sysname = ?") or die "Could not prepare sgetip"; my $sgid = $dbh->prepare("SELECT nextval('squeue_group_sequence') as gid"); my $all_switches = $dbh->prepare("SELECT sysname FROM switches ORDER BY sysname"); sub parse_range($) { my $switches = $_; my @range; my @rangecomma = split(/\s*,\s*/, $switches); foreach (@rangecomma) { my ($first, $last) = $_ =~ /(e\d+\-(?:sw)?[123456])\s*\-\s*(e\d+\-(?:sw)?[123456])?/; if (!defined($first) && $_ =~ /e\d+\-(sw)?[123456]/) { $first = $_; } if (!defined($first)) { print "Parse error in: $_
\n"; next; } my ($rowstart, $placestart) = $first =~ /e(\d+)\-(?:sw)?([123456])/; if (!defined($rowstart) || !defined($placestart)) { print "Parse error in: $_
\n"; next; } my ($rowend, $placeend); if (!defined($last)) { $rowend = $rowstart; $placeend = $placestart; } else { ($rowend, $placeend) = $last =~ /e(\d+)\-(?:sw)?([123456])/; } if (!defined($rowend) || !defined($placeend)) { print "Parse error in: $_
\n"; next; } #print "e $rowstart - $placestart to e $rowend - $placeend
\n"; for (my $i = $rowstart; $i <= $rowend; $i++) { my $dostart; if ($rowstart != $i) { $dostart = 1; } else { $dostart = $placestart; } for (my $j = $dostart; $j <= 6; $j++) { last if ($i == $rowend && $j > $placeend); push(@range, "e$i-$j"); } } } # foreach (@range) { # print ":: $_
\n"; # } return @range; } sub get_addr_from_switchnum($) { my ($sysname) = @_; $sgetip->execute($sysname); if ($sgetip->rows() < 1) { print "Could not get the ip for: ".$sysname; return undef; } my $row = $sgetip->fetchrow_hashref(); return $row->{'ip'}; } my $cgi = new CGI; print $cgi->header(-type=>'text/html; charset=utf-8'); print << "EOF"; Switch managment

Du er logget inn som: $user

Alle switchene
Switch e1-2, e3-3 - e10-2
Regexp Regulært uttrykk
Rad 1,3-5 (Disabled)




Prioritet
Kommando(er):
  • En kommando per linje.
  • Linjer som begynner med ! sørger for at nms ikke venter på normal prompt, men fyrer av gårde neste linje umiddelbart. Kjekt for kommandoer av typen "!save\\nY"
  • %SYSNAME% erstattes med hostnavnet til switchen
  • "#require-version 14.1X53-D15.2" avbryter scriptet om versjonen av JunOS ikke er lik 14.1X53-D15.2





EOF print "
\n"; my @switches = (); given ($cgi->param('rangetype')) { when ('all') { print "Sender `".$cgi->param('cmd')."` til alle switchene
"; @switches = (); $all_switches->execute(); while (my $ref = $all_switches->fetchrow_hashref) { push @switches, $ref->{'sysname'}; } } when ('switch') { # print "Sender `".$cgi->param('cmd')."` til switchene `" # .$cgi->param('range')."`.
"; $_ = $cgi->param('range'); @switches = parse_range($_); } when ('regexp') { @switches = (); $all_switches->execute(); while (my $ref = $all_switches->fetchrow_hashref) { push @switches, $ref->{'sysname'} if $ref->{'sysname'} =~ $cgi->param('regexp'); } } when ('row') { # print "Sender `".$cgi->param('cmd')."` til radene `" # .$cgi->param('range')."`.
"; # print "This function does not work yet."; # $_ = $cgi->param('range'); # @switches = &parse_row_range($_); # @switches = (); print "Slått av!\n"; } }; my $gid; if (@switches > 0) { $sgid->execute(); my $row = $sgid->fetchrow_hashref(); $gid = $row->{gid}; } my $pri = $cgi->param('priority'); print "
\n";
foreach my $switch (@switches) {
	my $addr = get_addr_from_switchnum($switch);
	if (!defined($addr)) {
		next;
	}
	my $cmd = $cgi->param('cmd');
	print "$switch got addr $addr 
\n"; print "Queuing commands for $switch:\n"; my $result = $sinsert->execute($gid, $pri, $addr, $switch, $cmd, $user); if (!$result) { print "\t" ."Could not execute query." ."\n"; print "\t".$dbh->errstr."\n"; } else { print "\tQueued: $cmd\n"; } print "\n"; } $dbh->commit; if (defined($gid)) { print "Vis resultat\n"; } print "
\n"; print << "EOF"; EOF