10 # Grab from .htaccess-authentication
11 my $user = $ENV{'REMOTE_USER'};
13 my $dbh = nms::db_connect();
14 $dbh->{AutoCommit} = 0;
16 # Ugly casting, found not other way
17 my $sinsert = $dbh->prepare( "INSERT INTO squeue
18 (gid, added, priority, addr, sysname, cmd, author)
19 VALUES(?::text::int, now(), ?::text::int, ?::text::inet, ?, ?, ?)")
20 or die "Could not prepare sinsert";
21 my $sgetip = $dbh->prepare("SELECT ip FROM switches WHERE sysname = ?")
22 or die "Could not prepare sgetip";
23 my $sgid = $dbh->prepare("SELECT nextval('squeue_group_sequence') as gid");
29 my @rangecomma = split(/\s*,\s*/, $switches);
30 foreach (@rangecomma) {
31 my ($first, $drop1, $last, $drop2) = $_ =~ /(e\d+\-[123456])(sw)?\s*\-\s*(e\d+\-[123456])?(sw)?/;
32 if (!defined($first) && $_ =~ /e\d+\-[123456]/) {
35 if (!defined($first)) {
36 print "<font color=\"red\">Parse error in: $_</font><br>\n";
39 my ($rowstart, $placestart) = $first =~ /e(\d+)\-([123456])/;
40 if (!defined($rowstart) || !defined($placestart)) {
41 print "<font color=\"red\">Parse error in: $_</font><br>\n";
44 my ($rowend, $placeend);
45 if (!defined($last)) {
47 $placeend = $placestart;
50 ($rowend, $placeend) = $last =~ /e(\d+)\-([123456])/;
52 if (!defined($rowend) || !defined($placeend)) {
53 print "<font color=\"red\">Parse error in: $_</font><br>\n";
56 #print "e $rowstart - $placestart to e $rowend - $placeend <br>\n";
57 for (my $i = $rowstart; $i <= $rowend; $i++) {
59 if ($rowstart != $i) {
63 $dostart = $placestart;
65 for (my $j = $dostart; $j <= 6; $j++) {
66 last if ($i == $rowend && $j > $placeend);
67 push(@range, "e$i-$j");
72 # print ":: $_<br>\n";
77 sub get_addr_from_switchnum($) {
80 $sgetip->execute($sysname."sw");
81 if ($sgetip->rows() < 1) {
82 print "Could not get the ip for: ".$sysname."sw";
85 my $row = $sgetip->fetchrow_hashref();
91 print $cgi->header(-type=>'text/html');
96 <title>Switch managment</title>
99 <p>Du er logget inn som: $user</p>
100 <form method="POST" action="smanagement.pl">
103 <td>Alle switchene</td>
104 <td><input type="radio" name="rangetype" value="all" /></td>
110 <td><input type="radio" checked name="rangetype" value="switch" /></td>
111 <td><input type="text" name="range" /></td>
112 <td>e1-2, e3-3 - e10-2</td>
116 <td><input type="radio" name="rangetype" value="row" /></td>
117 <td><input type="text" name="range" /></td>
118 <td>1,3-5 (Disabled)</td>
130 <select name="priority">
131 <option value="1">1 (lavest)</option>
132 <option value="2">2</option>
133 <option selected value="3">3</option>
134 <option value="4">4</option>
135 <option value="5">5 (høyest)</option>
140 <td>Kommando(er):</td>
142 <td><textarea name="cmd"></textarea></td>
143 <td>En kommando per linje</td>
152 <input type="submit" value="Execute!" /><br />
159 switch ($cgi->param('rangetype')) {
161 # print "Sender `".$cgi->param('cmd')."` til alle switchene<br />";
163 print "<font color=\"red\">Slått av!</font>\n";
166 # print "Sender `".$cgi->param('cmd')."` til switchene `"
167 # .$cgi->param('range')."`.<br />";
168 $_ = $cgi->param('range');
169 @switches = parse_range($_);
172 # print "Sender `".$cgi->param('cmd')."` til radene `"
173 # .$cgi->param('range')."`.<br />";
174 # print "This function does not work yet.";
175 # $_ = $cgi->param('range');
176 # @switches = &parse_row_range($_);
178 print "<font color=\"red\">Slått av!</font>\n";
185 my $row = $sgid->fetchrow_hashref();
189 my $pri = $cgi->param('priority');
192 foreach my $switch (@switches) {
193 my $addr = get_addr_from_switchnum($switch);
194 if (!defined($addr)) {
197 print "$switch got addr $addr <br>\n";
198 my @cmds = split(/[\n\r]+/, $cgi->param('cmd'));
199 print "Queuing commands for $switch:\n";
200 foreach my $cmd (@cmds) {
201 my $result = $sinsert->execute($gid, $pri, $addr, $switch, $cmd, $user);
204 print "\t<font color=\"red\">"
205 ."Could not execute query."
207 print "\t".$dbh->errstr."\n";
210 print "\tQueued: $cmd\n";
217 print "<a href=\"sshow.pl?action=showgid&gid=".$gid."\">Vis resultat</a>\n";