6 my $password = 'removed';
8 # Seconds to wait for connection
20 # Grab from .htaccess-authentication
21 my $user = $ENV{'REMOTE_USER'};
23 my $dbh = DBI->connect("dbi:Pg:dbname=snmpfetch;host=violet.tg05.gathering.org",
24 "snmpfetch", "removed")
25 or die "Couldn't connect to database";
26 $dbh->{AutoCommit} = 0;
28 # Ugly casting, found not other way
29 my $sinsert = $dbh->prepare( "INSERT INTO squeue
30 (gid, added, priority, addr, sysname, cmd, author)
31 VALUES(?::text::int, now(), ?::text::int, ?::text::inet, ?, ?, ?)")
32 or die "Could not prepare sinsert";
33 my $sgetip = $dbh->prepare("SELECT ip FROM switches WHERE sysname = ?")
34 or die "Could not prepare sgetip";
35 my $sgid = $dbh->prepare("SELECT nextval('squeue_group_sequence') as gid");
37 # Send a command to switch and return the data recvied from the switch
39 my ($cmd, $conn) = @_;
41 # Send the command and get data from switch
42 my @data = $conn->cmd($cmd);
44 foreach my $line (@data) {
45 # Remove escape-7 sequence
46 $line =~ s/\x1b\x37//g;
53 sub switch_connect($) {
56 my $conn = new Net::Telnet( Timeout => $timeout,
58 Prompt => '/(es3024|e\d+\-\dsw)>/i');
59 my $ret = $conn->open( Host => $ip);
60 if (!$ret || $ret != 1) {
63 # XXX: Just send the password as text, I did not figure out how to
64 # handle authentication with only password through $conn->login().
65 #$conn->login( Prompt => '/password[: ]*$/i',
67 # Password => $password);
68 $conn->cmd($password);
78 my @rangecomma = split(/\s*,\s*/, $switches);
79 foreach (@rangecomma) {
80 my ($first, $drop1, $last, $drop2) = $_ =~ /(e\d+\-[123456])(sw)?\s*\-\s*(e\d+\-[123456])?(sw)?/;
81 if (!defined($first) && $_ =~ /e\d+\-[123456]/) {
84 if (!defined($first)) {
85 print "<font color=\"red\">Parse error in: $_</font><br>\n";
88 my ($rowstart, $placestart) = $first =~ /e(\d+)\-([123456])/;
89 if (!defined($rowstart) || !defined($placestart)) {
90 print "<font color=\"red\">Parse error in: $_</font><br>\n";
93 my ($rowend, $placeend);
94 if (!defined($last)) {
96 $placeend = $placestart;
99 ($rowend, $placeend) = $last =~ /e(\d+)\-([123456])/;
101 if (!defined($rowend) || !defined($placeend)) {
102 print "<font color=\"red\">Parse error in: $_</font><br>\n";
105 #print "e $rowstart - $placestart to e $rowend - $placeend <br>\n";
106 for (my $i = $rowstart; $i <= $rowend; $i++) {
108 if ($rowstart != $i) {
112 $dostart = $placestart;
114 for (my $j = $dostart; $j <= 6; $j++) {
115 last if ($i == $rowend && $j > $placeend);
116 push(@range, "e$i-$j");
121 # print ":: $_<br>\n";
126 sub get_addr_from_switchnum($) {
129 $sgetip->execute($sysname."sw");
130 if ($sgetip->rows() < 1) {
131 print "Could not get the ip for: ".$sysname."sw";
134 my $row = $sgetip->fetchrow_hashref();
140 print $cgi->header(-type=>'text/html');
145 <title>Switch managment</title>
148 <p>Du er logget inn som: $user</p>
149 <form method="POST" action="smanagement.pl">
152 <td>Alle switchene</td>
153 <td><input type="radio" name="rangetype" value="all" /></td>
159 <td><input type="radio" checked name="rangetype" value="switch" /></td>
160 <td><input type="text" name="range" /></td>
161 <td>e1-2, e3-3 - e10-2</td>
165 <td><input type="radio" name="rangetype" value="row" /></td>
166 <td><input type="text" name="range" /></td>
167 <td>1,3-5 (Disabled)</td>
179 <select name="priority">
180 <option value="1">1 (lavest)</option>
181 <option value="2">2</option>
182 <option selected value="3">3</option>
183 <option value="4">4</option>
184 <option value="5">5 (høyest)</option>
189 <td>Kommando(er):</td>
191 <td><textarea name="cmd"></textarea></td>
192 <td>En kommando per linje</td>
201 <input type="submit" value="Execute!" /><br />
208 switch ($cgi->param('rangetype')) {
210 # print "Sender `".$cgi->param('cmd')."` til alle switchene<br />";
212 print "<font color=\"red\">Slått av!</font>\n";
215 # print "Sender `".$cgi->param('cmd')."` til switchene `"
216 # .$cgi->param('range')."`.<br />";
217 $_ = $cgi->param('range');
218 @switches = parse_range($_);
221 # print "Sender `".$cgi->param('cmd')."` til radene `"
222 # .$cgi->param('range')."`.<br />";
223 # print "This function does not work yet.";
224 # $_ = $cgi->param('range');
225 # @switches = &parse_row_range($_);
227 print "<font color=\"red\">Slått av!</font>\n";
234 my $row = $sgid->fetchrow_hashref();
238 my $pri = $cgi->param('priority');
241 foreach my $switch (@switches) {
242 my $addr = get_addr_from_switchnum($switch);
243 if (!defined($addr)) {
246 print "$switch got addr $addr <br>\n";
247 my @cmds = split(/[\n\r]+/, $cgi->param('cmd'));
248 print "Queuing commands for $switch:\n";
249 foreach my $cmd (@cmds) {
250 my $result = $sinsert->execute($gid, $pri, $addr, $switch, $cmd, $user);
253 print "\t<font color=\"red\">"
254 ."Could not execute query."
256 print "\t".$dbh->errstr."\n";
259 print "\tQueued: $cmd\n";
266 print "<a href=\"sshow.pl?action=showgid&gid=".$gid."\">Vis resultat</a>\n";