]> git.sesse.net Git - nms/blob - web/smanagement.pl
Put Zyxel password in config.pm.
[nms] / web / smanagement.pl
1 #!/usr/bin/perl
2 use lib '../include';
3 use nms;
4
5 # Seconds to wait for connection
6 my $timeout = 15;
7
8
9 use warnings;
10 use strict;
11 use Switch;
12 use CGI;
13 use Net::Telnet;
14 use DBI;
15 use Data::Dumper;
16
17 # Grab from .htaccess-authentication
18 my $user = $ENV{'REMOTE_USER'};
19
20 my $dbh = nms::db_connect();
21 $dbh->{AutoCommit} = 0;
22
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");
31
32 # Send a command to switch and return the data recvied from the switch
33 sub switch_exec($$) {
34         my ($cmd, $conn) = @_;
35
36         # Send the command and get data from switch
37         my @data = $conn->cmd($cmd);
38         my @lines = ();
39         foreach my $line (@data) {
40                 # Remove escape-7 sequence
41                 $line =~ s/\x1b\x37//g;
42                 push (@lines, $line);
43         }
44
45         return @lines;
46 }
47
48 sub switch_connect($) {
49         my ($ip) = @_;
50
51         my $conn = new Net::Telnet(     Timeout => $timeout,
52                                         Errmode => 'return',
53                                         Prompt => '/(es3024|e\d+\-\dsw)>/i');
54         my $ret = $conn->open(  Host => $ip);
55         if (!$ret || $ret != 1) {
56                 return (0);
57         }
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',
61         #               Name => $password,
62         #               Password => $password);
63         $conn->cmd($nms::config::zyxel_password);
64         # Get rid of banner
65         $conn->get;
66         return ($conn);
67 }
68
69 sub parse_range($) {
70         my $switches = $_;
71         my @range;
72
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]/) {
77                         $first = $_;
78                 }
79                 if (!defined($first)) {
80                         print "<font color=\"red\">Parse error in: $_</font><br>\n";
81                         next;
82                 }
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";
86                         next;
87                 }
88                 my ($rowend, $placeend);
89                 if (!defined($last)) {
90                         $rowend = $rowstart;
91                         $placeend = $placestart;
92                 }
93                 else {
94                         ($rowend, $placeend) = $last =~ /e(\d+)\-([123456])/;
95                 }
96                 if (!defined($rowend) || !defined($placeend)) {
97                         print "<font color=\"red\">Parse error in: $_</font><br>\n";
98                         next;
99                 }
100                 #print "e $rowstart - $placestart to e $rowend - $placeend <br>\n";
101                 for (my $i = $rowstart; $i <= $rowend; $i++) {
102                         my $dostart;
103                         if ($rowstart != $i) {
104                                 $dostart = 1;
105                         }
106                         else {
107                                 $dostart = $placestart;
108                         }
109                         for (my $j = $dostart; $j <= 6; $j++) {
110                                 last if ($i == $rowend && $j > $placeend);
111                                 push(@range, "e$i-$j");
112                         }
113                 }
114         }
115 #       foreach (@range) {
116 #               print ":: $_<br>\n";
117 #       }
118         return @range;
119 }
120
121 sub get_addr_from_switchnum($) {
122         my ($sysname) = @_;
123
124         $sgetip->execute($sysname."sw");
125         if ($sgetip->rows() < 1) {
126                 print "Could not get the ip for: ".$sysname."sw";
127                 return undef;
128         }
129         my $row = $sgetip->fetchrow_hashref();
130         return $row->{'ip'};
131 }
132
133 my $cgi = new CGI;
134
135 print $cgi->header(-type=>'text/html');
136
137 print << "EOF";
138 <html>
139   <head>
140     <title>Switch managment</title>
141   </head>
142   <body>
143   <p>Du er logget inn som: $user</p>
144     <form method="POST" action="smanagement.pl">
145     <table>
146       <tr>
147         <td>Alle switchene</td>
148         <td><input type="radio" name="rangetype" value="all" /></td>
149         <td></td>
150         <td>Disabled</td>
151       </tr>
152       <tr>
153         <td>Switch</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>
157       </tr>
158       <tr>
159         <td>Rad</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>
163       </tr>
164        <tr>
165         <td><hr /></td>
166         <td><hr /></td>
167         <td><hr /></td>
168         <td><hr /></td>
169       </tr>
170       <tr>
171         <td>Prioritet</td>
172         <td></td>
173         <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>
180           </select>
181         </td>
182       </tr>
183       <tr>
184         <td>Kommando(er):</td>
185         <td></td>
186         <td><textarea name="cmd"></textarea></td>
187         <td>En kommando per linje</td>
188       </td>
189       <tr>
190         <td><hr /></td>
191         <td><hr /></td>
192         <td><hr /></td>
193         <td><hr /></td>
194       </tr>
195     </table>
196     <input type="submit" value="Execute!" /><br />
197     </form>
198 EOF
199
200 print "<br />\n";
201
202 my @switches = ();
203 switch ($cgi->param('rangetype')) {
204         case 'all' {
205 #               print "Sender `".$cgi->param('cmd')."` til alle switchene<br />";
206                 @switches = ();
207                 print "<font color=\"red\">Slått av!</font>\n";
208         }
209         case 'switch' {
210 #               print "Sender `".$cgi->param('cmd')."` til switchene `"
211 #                     .$cgi->param('range')."`.<br />";
212                 $_ = $cgi->param('range');
213                 @switches = parse_range($_);
214         }
215         case 'row' {
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($_);
221                 @switches = ();
222                 print "<font color=\"red\">Slått av!</font>\n";
223         }
224 }
225
226 my $gid;
227 if (@switches > 0) {
228         $sgid->execute();
229         my $row = $sgid->fetchrow_hashref();
230         $gid = $row->{gid};
231 }
232
233 my $pri = $cgi->param('priority');
234
235 print "<pre>\n";
236 foreach my $switch (@switches) {
237         my $addr = get_addr_from_switchnum($switch);
238         if (!defined($addr)) {
239                 next;
240         }
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);
246         #       my $result = 1;
247                 if (!$result) {
248                         print "\t<font color=\"red\">"
249                                ."Could not execute query."
250                                ."</font>\n";
251                         print "\t".$dbh->errstr."\n";
252                 }
253                 else {
254                         print "\tQueued: $cmd\n";
255                 }
256         }
257         print "\n";
258 }
259 $dbh->commit;
260 if (defined($gid)) {
261         print "<a href=\"sshow.pl?action=showgid&gid=".$gid."\">Vis resultat</a>\n";
262 }
263 print "</pre>\n";
264
265 print << "EOF";
266   </body>
267 </html>
268 EOF