5 # Seconds to wait for connection
17 # Grab from .htaccess-authentication
18 my $user = $ENV{'REMOTE_USER'};
20 my $dbh = nms::db_connect();
21 $dbh->{AutoCommit} = 0;
23 # Ugly casting, found not other way
24 my $sinsert = $dbh->prepare( "INSERT INTO squeue
25 (gid, added, priority, addr, sysname, cmd, author)
26 VALUES(?::text::int, now(), ?::text::int, ?::text::inet, ?, ?, ?)")
27 or die "Could not prepare sinsert";
28 my $sgetip = $dbh->prepare("SELECT ip FROM switches WHERE sysname = ?")
29 or die "Could not prepare sgetip";
30 my $sgid = $dbh->prepare("SELECT nextval('squeue_group_sequence') as gid");
32 # Send a command to switch and return the data recvied from the switch
34 my ($cmd, $conn) = @_;
36 # Send the command and get data from switch
37 my @data = $conn->cmd($cmd);
39 foreach my $line (@data) {
40 # Remove escape-7 sequence
41 $line =~ s/\x1b\x37//g;
48 sub switch_connect($) {
51 my $conn = new Net::Telnet( Timeout => $timeout,
53 Prompt => '/(es3024|e\d+\-\dsw)>/i');
54 my $ret = $conn->open( Host => $ip);
55 if (!$ret || $ret != 1) {
58 # XXX: Just send the password as text, I did not figure out how to
59 # handle authentication with only password through $conn->login().
60 #$conn->login( Prompt => '/password[: ]*$/i',
62 # Password => $password);
63 $conn->cmd($nms::config::zyxel_password);
73 my @rangecomma = split(/\s*,\s*/, $switches);
74 foreach (@rangecomma) {
75 my ($first, $drop1, $last, $drop2) = $_ =~ /(e\d+\-[123456])(sw)?\s*\-\s*(e\d+\-[123456])?(sw)?/;
76 if (!defined($first) && $_ =~ /e\d+\-[123456]/) {
79 if (!defined($first)) {
80 print "<font color=\"red\">Parse error in: $_</font><br>\n";
83 my ($rowstart, $placestart) = $first =~ /e(\d+)\-([123456])/;
84 if (!defined($rowstart) || !defined($placestart)) {
85 print "<font color=\"red\">Parse error in: $_</font><br>\n";
88 my ($rowend, $placeend);
89 if (!defined($last)) {
91 $placeend = $placestart;
94 ($rowend, $placeend) = $last =~ /e(\d+)\-([123456])/;
96 if (!defined($rowend) || !defined($placeend)) {
97 print "<font color=\"red\">Parse error in: $_</font><br>\n";
100 #print "e $rowstart - $placestart to e $rowend - $placeend <br>\n";
101 for (my $i = $rowstart; $i <= $rowend; $i++) {
103 if ($rowstart != $i) {
107 $dostart = $placestart;
109 for (my $j = $dostart; $j <= 6; $j++) {
110 last if ($i == $rowend && $j > $placeend);
111 push(@range, "e$i-$j");
116 # print ":: $_<br>\n";
121 sub get_addr_from_switchnum($) {
124 $sgetip->execute($sysname."sw");
125 if ($sgetip->rows() < 1) {
126 print "Could not get the ip for: ".$sysname."sw";
129 my $row = $sgetip->fetchrow_hashref();
135 print $cgi->header(-type=>'text/html');
140 <title>Switch managment</title>
143 <p>Du er logget inn som: $user</p>
144 <form method="POST" action="smanagement.pl">
147 <td>Alle switchene</td>
148 <td><input type="radio" name="rangetype" value="all" /></td>
154 <td><input type="radio" checked name="rangetype" value="switch" /></td>
155 <td><input type="text" name="range" /></td>
156 <td>e1-2, e3-3 - e10-2</td>
160 <td><input type="radio" name="rangetype" value="row" /></td>
161 <td><input type="text" name="range" /></td>
162 <td>1,3-5 (Disabled)</td>
174 <select name="priority">
175 <option value="1">1 (lavest)</option>
176 <option value="2">2</option>
177 <option selected value="3">3</option>
178 <option value="4">4</option>
179 <option value="5">5 (høyest)</option>
184 <td>Kommando(er):</td>
186 <td><textarea name="cmd"></textarea></td>
187 <td>En kommando per linje</td>
196 <input type="submit" value="Execute!" /><br />
203 switch ($cgi->param('rangetype')) {
205 # print "Sender `".$cgi->param('cmd')."` til alle switchene<br />";
207 print "<font color=\"red\">Slått av!</font>\n";
210 # print "Sender `".$cgi->param('cmd')."` til switchene `"
211 # .$cgi->param('range')."`.<br />";
212 $_ = $cgi->param('range');
213 @switches = parse_range($_);
216 # print "Sender `".$cgi->param('cmd')."` til radene `"
217 # .$cgi->param('range')."`.<br />";
218 # print "This function does not work yet.";
219 # $_ = $cgi->param('range');
220 # @switches = &parse_row_range($_);
222 print "<font color=\"red\">Slått av!</font>\n";
229 my $row = $sgid->fetchrow_hashref();
233 my $pri = $cgi->param('priority');
236 foreach my $switch (@switches) {
237 my $addr = get_addr_from_switchnum($switch);
238 if (!defined($addr)) {
241 print "$switch got addr $addr <br>\n";
242 my @cmds = split(/[\n\r]+/, $cgi->param('cmd'));
243 print "Queuing commands for $switch:\n";
244 foreach my $cmd (@cmds) {
245 my $result = $sinsert->execute($gid, $pri, $addr, $switch, $cmd, $user);
248 print "\t<font color=\"red\">"
249 ."Could not execute query."
251 print "\t".$dbh->errstr."\n";
254 print "\tQueued: $cmd\n";
261 print "<a href=\"sshow.pl?action=showgid&gid=".$gid."\">Vis resultat</a>\n";