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");
25 # Send a command to switch and return the data recvied from the switch
27 my ($cmd, $conn) = @_;
29 # Send the command and get data from switch
30 my @data = $conn->cmd($cmd);
32 foreach my $line (@data) {
33 # Remove escape-7 sequence
34 $line =~ s/\x1b\x37//g;
45 my @rangecomma = split(/\s*,\s*/, $switches);
46 foreach (@rangecomma) {
47 my ($first, $drop1, $last, $drop2) = $_ =~ /(e\d+\-[123456])(sw)?\s*\-\s*(e\d+\-[123456])?(sw)?/;
48 if (!defined($first) && $_ =~ /e\d+\-[123456]/) {
51 if (!defined($first)) {
52 print "<font color=\"red\">Parse error in: $_</font><br>\n";
55 my ($rowstart, $placestart) = $first =~ /e(\d+)\-([123456])/;
56 if (!defined($rowstart) || !defined($placestart)) {
57 print "<font color=\"red\">Parse error in: $_</font><br>\n";
60 my ($rowend, $placeend);
61 if (!defined($last)) {
63 $placeend = $placestart;
66 ($rowend, $placeend) = $last =~ /e(\d+)\-([123456])/;
68 if (!defined($rowend) || !defined($placeend)) {
69 print "<font color=\"red\">Parse error in: $_</font><br>\n";
72 #print "e $rowstart - $placestart to e $rowend - $placeend <br>\n";
73 for (my $i = $rowstart; $i <= $rowend; $i++) {
75 if ($rowstart != $i) {
79 $dostart = $placestart;
81 for (my $j = $dostart; $j <= 6; $j++) {
82 last if ($i == $rowend && $j > $placeend);
83 push(@range, "e$i-$j");
88 # print ":: $_<br>\n";
93 sub get_addr_from_switchnum($) {
96 $sgetip->execute($sysname."sw");
97 if ($sgetip->rows() < 1) {
98 print "Could not get the ip for: ".$sysname."sw";
101 my $row = $sgetip->fetchrow_hashref();
107 print $cgi->header(-type=>'text/html');
112 <title>Switch managment</title>
115 <p>Du er logget inn som: $user</p>
116 <form method="POST" action="smanagement.pl">
119 <td>Alle switchene</td>
120 <td><input type="radio" name="rangetype" value="all" /></td>
126 <td><input type="radio" checked name="rangetype" value="switch" /></td>
127 <td><input type="text" name="range" /></td>
128 <td>e1-2, e3-3 - e10-2</td>
132 <td><input type="radio" name="rangetype" value="row" /></td>
133 <td><input type="text" name="range" /></td>
134 <td>1,3-5 (Disabled)</td>
146 <select name="priority">
147 <option value="1">1 (lavest)</option>
148 <option value="2">2</option>
149 <option selected value="3">3</option>
150 <option value="4">4</option>
151 <option value="5">5 (høyest)</option>
156 <td>Kommando(er):</td>
158 <td><textarea name="cmd"></textarea></td>
159 <td>En kommando per linje</td>
168 <input type="submit" value="Execute!" /><br />
175 switch ($cgi->param('rangetype')) {
177 # print "Sender `".$cgi->param('cmd')."` til alle switchene<br />";
179 print "<font color=\"red\">Slått av!</font>\n";
182 # print "Sender `".$cgi->param('cmd')."` til switchene `"
183 # .$cgi->param('range')."`.<br />";
184 $_ = $cgi->param('range');
185 @switches = parse_range($_);
188 # print "Sender `".$cgi->param('cmd')."` til radene `"
189 # .$cgi->param('range')."`.<br />";
190 # print "This function does not work yet.";
191 # $_ = $cgi->param('range');
192 # @switches = &parse_row_range($_);
194 print "<font color=\"red\">Slått av!</font>\n";
201 my $row = $sgid->fetchrow_hashref();
205 my $pri = $cgi->param('priority');
208 foreach my $switch (@switches) {
209 my $addr = get_addr_from_switchnum($switch);
210 if (!defined($addr)) {
213 print "$switch got addr $addr <br>\n";
214 my @cmds = split(/[\n\r]+/, $cgi->param('cmd'));
215 print "Queuing commands for $switch:\n";
216 foreach my $cmd (@cmds) {
217 my $result = $sinsert->execute($gid, $pri, $addr, $switch, $cmd, $user);
220 print "\t<font color=\"red\">"
221 ."Could not execute query."
223 print "\t".$dbh->errstr."\n";
226 print "\tQueued: $cmd\n";
233 print "<a href=\"sshow.pl?action=showgid&gid=".$gid."\">Vis resultat</a>\n";