1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
#!/usr/bin/perl
use warnings;
use strict;
use 5.010;
use CGI;
use DBI;
use Data::Dumper;
use lib '../../include';
use nms;
# Grab from .htaccess-authentication
my $user = $ENV{'REMOTE_USER'};
my $dbh = nms::db_connect();
$dbh->{AutoCommit} = 0;
# Ugly casting, found no other way
my $sinsert = $dbh->prepare( "INSERT INTO squeue
(gid, added, priority, addr, sysname, cmd, author)
VALUES(?::text::int, timeofday()::timestamptz, ?::text::int, ?::text::inet, ?, ?, ?)")
or die "Could not prepare sinsert";
my $sgetip = $dbh->prepare("SELECT ip FROM switches WHERE sysname = ?")
or die "Could not prepare sgetip";
my $sgid = $dbh->prepare("SELECT nextval('squeue_group_sequence') as gid");
my $all_switches = $dbh->prepare("SELECT sysname FROM switches ORDER BY sysname");
sub parse_range($) {
my $switches = $_;
my @range;
my @rangecomma = split(/\s*,\s*/, $switches);
foreach (@rangecomma) {
my ($first, $last) = $_ =~ /(e\d+\-(?:sw)?[123456])\s*\-\s*(e\d+\-(?:sw)?[123456])?/;
if (!defined($first) && $_ =~ /e\d+\-(sw)?[123456]/) {
$first = $_;
}
if (!defined($first)) {
print "<font color=\"red\">Parse error in: $_</font><br>\n";
next;
}
my ($rowstart, $placestart) = $first =~ /e(\d+)\-(?:sw)?([123456])/;
if (!defined($rowstart) || !defined($placestart)) {
print "<font color=\"red\">Parse error in: $_</font><br>\n";
next;
}
my ($rowend, $placeend);
if (!defined($last)) {
$rowend = $rowstart;
$placeend = $placestart;
}
else {
($rowend, $placeend) = $last =~ /e(\d+)\-(?:sw)?([123456])/;
}
if (!defined($rowend) || !defined($placeend)) {
print "<font color=\"red\">Parse error in: $_</font><br>\n";
next;
}
#print "e $rowstart - $placestart to e $rowend - $placeend <br>\n";
for (my $i = $rowstart; $i <= $rowend; $i++) {
my $dostart;
if ($rowstart != $i) {
$dostart = 1;
}
else {
$dostart = $placestart;
}
for (my $j = $dostart; $j <= 6; $j++) {
last if ($i == $rowend && $j > $placeend);
push(@range, "e$i-$j");
}
}
}
# foreach (@range) {
# print ":: $_<br>\n";
# }
return @range;
}
sub get_addr_from_switchnum($) {
my ($sysname) = @_;
$sgetip->execute($sysname);
if ($sgetip->rows() < 1) {
print "Could not get the ip for: ".$sysname;
return undef;
}
my $row = $sgetip->fetchrow_hashref();
return $row->{'ip'};
}
my $cgi = new CGI;
print $cgi->header(-type=>'text/html; charset=utf-8');
print << "EOF";
<html>
<head>
<title>Switch managment</title>
</head>
<body>
<p>Du er logget inn som: $user</p>
<form method="POST" action="smanagement.pl">
<table>
<tr>
<td>Alle switchene</td>
<td><input type="radio" name="rangetype" value="all" /></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Switch</td>
<td><input type="radio" checked name="rangetype" value="switch" /></td>
<td><input type="text" name="range" /></td>
<td>e1-2, e3-3 - e10-2</td>
</tr>
<tr>
<td>Regexp</td>
<td><input type="radio" name="rangetype" value="regexp" /></td>
<td><input type="text" name="regexp" /></td>
<td>Regulært uttrykk</td>
</tr>
<tr>
<td>Rad</td>
<td><input type="radio" name="rangetype" value="row" /></td>
<td><input type="text" name="range" /></td>
<td>1,3-5 (Disabled)</td>
</tr>
<tr>
<td><hr /></td>
<td><hr /></td>
<td><hr /></td>
<td><hr /></td>
</tr>
<tr>
<td>Prioritet</td>
<td></td>
<td>
<select name="priority">
<option value="1">1 (lavest)</option>
<option value="2">2</option>
<option selected value="3">3</option>
<option value="4">4</option>
<option value="5">5 (høyest)</option>
</select>
</td>
</tr>
<tr>
<td>Kommando(er):</td>
<td></td>
<td><textarea name="cmd" cols="80" rows="24"></textarea></td>
<td>
<ul>
<li>En kommando per linje.</li>
<li>Linjer som begynner med ! sørger for at nms ikke venter på normal prompt, men fyrer av gårde neste linje umiddelbart. Kjekt for kommandoer av typen "!save\\nY"</li>
<li>%SYSNAME% erstattes med hostnavnet til switchen</li>
<li>"#require-version 14.1X53-D15.2" avbryter scriptet om versjonen av JunOS ikke er lik 14.1X53-D15.2</li>
</ul>
</td>
</td>
<tr>
<td><hr /></td>
<td><hr /></td>
<td><hr /></td>
<td><hr /></td>
</tr>
</table>
<input type="submit" value="Execute!" /><br />
</form>
EOF
print "<br />\n";
my @switches = ();
given ($cgi->param('rangetype')) {
when ('all') {
print "Sender `".$cgi->param('cmd')."` til alle switchene<br />";
@switches = ();
$all_switches->execute();
while (my $ref = $all_switches->fetchrow_hashref) {
push @switches, $ref->{'sysname'};
}
}
when ('switch') {
# print "Sender `".$cgi->param('cmd')."` til switchene `"
# .$cgi->param('range')."`.<br />";
$_ = $cgi->param('range');
@switches = parse_range($_);
}
when ('regexp') {
@switches = ();
$all_switches->execute();
while (my $ref = $all_switches->fetchrow_hashref) {
push @switches, $ref->{'sysname'} if $ref->{'sysname'} =~ $cgi->param('regexp');
}
}
when ('row') {
# print "Sender `".$cgi->param('cmd')."` til radene `"
# .$cgi->param('range')."`.<br />";
# print "This function does not work yet.";
# $_ = $cgi->param('range');
# @switches = &parse_row_range($_);
# @switches = ();
print "<font color=\"red\">Slått av!</font>\n";
}
};
my $gid;
if (@switches > 0) {
$sgid->execute();
my $row = $sgid->fetchrow_hashref();
$gid = $row->{gid};
}
my $pri = $cgi->param('priority');
print "<pre>\n";
foreach my $switch (@switches) {
my $addr = get_addr_from_switchnum($switch);
if (!defined($addr)) {
next;
}
my $cmd = $cgi->param('cmd');
print "$switch got addr $addr <br>\n";
print "Queuing commands for $switch:\n";
my $result = $sinsert->execute($gid, $pri, $addr, $switch, $cmd, $user);
if (!$result) {
print "\t<font color=\"red\">"
."Could not execute query."
."</font>\n";
print "\t".$dbh->errstr."\n";
}
else {
print "\tQueued: $cmd\n";
}
print "\n";
}
$dbh->commit;
if (defined($gid)) {
print "<a href=\"sshow.pl?action=showgid&gid=".$gid."\">Vis resultat</a>\n";
}
print "</pre>\n";
print << "EOF";
</body>
</html>
EOF
|