11 # Grab from .htaccess-authentication
12 my $user = $ENV{'REMOTE_USER'};
14 my $dbh = nms::db_connect();
15 $dbh->{AutoCommit} = 0;
17 # Ugly casting, found not other way
18 my $sinsert = $dbh->prepare( "INSERT INTO squeue
19 (gid, added, priority, addr, sysname, cmd, author)
20 VALUES(?::text::int, now(), ?::text::int, ?::text::inet, ?, ?, ?)")
21 or die "Could not prepare sinsert";
22 my $sgetip = $dbh->prepare("SELECT ip FROM switches WHERE sysname = ?")
23 or die "Could not prepare sgetip";
24 my $sgid = $dbh->prepare("SELECT nextval('squeue_group_sequence') as gid");
30 my @rangecomma = split(/\s*,\s*/, $switches);
31 foreach (@rangecomma) {
32 my ($first, $last) = $_ =~ /(e\d+\-(?:sw)?[123456])\s*\-\s*(e\d+\-(?:sw)?[123456])?/;
33 if (!defined($first) && $_ =~ /e\d+\-(sw)?[123456]/) {
36 if (!defined($first)) {
37 print "<font color=\"red\">Parse error in: $_</font><br>\n";
40 my ($rowstart, $placestart) = $first =~ /e(\d+)\-(?:sw)?([123456])/;
41 if (!defined($rowstart) || !defined($placestart)) {
42 print "<font color=\"red\">Parse error in: $_</font><br>\n";
45 my ($rowend, $placeend);
46 if (!defined($last)) {
48 $placeend = $placestart;
51 ($rowend, $placeend) = $last =~ /e(\d+)\-(?:sw)?([123456])/;
53 if (!defined($rowend) || !defined($placeend)) {
54 print "<font color=\"red\">Parse error in: $_</font><br>\n";
57 #print "e $rowstart - $placestart to e $rowend - $placeend <br>\n";
58 for (my $i = $rowstart; $i <= $rowend; $i++) {
60 if ($rowstart != $i) {
64 $dostart = $placestart;
66 for (my $j = $dostart; $j <= 6; $j++) {
67 last if ($i == $rowend && $j > $placeend);
68 push(@range, "e$i-sw$j");
73 # print ":: $_<br>\n";
78 sub get_addr_from_switchnum($) {
81 $sgetip->execute($sysname);
82 if ($sgetip->rows() < 1) {
83 print "Could not get the ip for: ".$sysname;
86 my $row = $sgetip->fetchrow_hashref();
92 print $cgi->header(-type=>'text/html');
97 <title>Switch managment</title>
100 <p>Du er logget inn som: $user</p>
101 <form method="POST" action="smanagement.pl">
104 <td>Alle switchene</td>
105 <td><input type="radio" name="rangetype" value="all" /></td>
111 <td><input type="radio" checked name="rangetype" value="switch" /></td>
112 <td><input type="text" name="range" /></td>
113 <td>e1-2, e3-3 - e10-2</td>
117 <td><input type="radio" name="rangetype" value="row" /></td>
118 <td><input type="text" name="range" /></td>
119 <td>1,3-5 (Disabled)</td>
131 <select name="priority">
132 <option value="1">1 (lavest)</option>
133 <option value="2">2</option>
134 <option selected value="3">3</option>
135 <option value="4">4</option>
136 <option value="5">5 (høyest)</option>
141 <td>Kommando(er):</td>
143 <td><textarea name="cmd"></textarea></td>
144 <td>En kommando per linje</td>
153 <input type="submit" value="Execute!" /><br />
160 switch ($cgi->param('rangetype')) {
162 # print "Sender `".$cgi->param('cmd')."` til alle switchene<br />";
164 print "<font color=\"red\">Slått av!</font>\n";
167 # print "Sender `".$cgi->param('cmd')."` til switchene `"
168 # .$cgi->param('range')."`.<br />";
169 $_ = $cgi->param('range');
170 @switches = parse_range($_);
173 # print "Sender `".$cgi->param('cmd')."` til radene `"
174 # .$cgi->param('range')."`.<br />";
175 # print "This function does not work yet.";
176 # $_ = $cgi->param('range');
177 # @switches = &parse_row_range($_);
179 print "<font color=\"red\">Slått av!</font>\n";
186 my $row = $sgid->fetchrow_hashref();
190 my $pri = $cgi->param('priority');
193 foreach my $switch (@switches) {
194 my $addr = get_addr_from_switchnum($switch);
195 if (!defined($addr)) {
198 print "$switch got addr $addr <br>\n";
199 my @cmds = split(/[\n\r]+/, $cgi->param('cmd'));
200 print "Queuing commands for $switch:\n";
201 foreach my $cmd (@cmds) {
202 my $result = $sinsert->execute($gid, $pri, $addr, $switch, $cmd, $user);
205 print "\t<font color=\"red\">"
206 ."Could not execute query."
208 print "\t".$dbh->errstr."\n";
211 print "\tQueued: $cmd\n";
218 print "<a href=\"sshow.pl?action=showgid&gid=".$gid."\">Vis resultat</a>\n";