Debian Codegraph @

perl/manager/setupauth

raw
1 #!/usr/bin/perl
2
3 use DBI;
4 $hostname = 'localhost'; # Host that serves the mSQL Database
5 $dbname = 'snmp'; # mySQL Database name
6 $doit = 1;
7
8 sub usage {
9 print "$0 [-H host] [-u user] [-p password] [-v] [-h] [-n] [-g groupname] [-m machinename] TOKEN VALUE\n";
10 exit 0;
11 }
12
13 while ($#ARGV > -1 && $ARGV[0] =~ /^-/) {
14 $_ = shift @ARGV;
15 usage if (/-h/);
16 $hostname = shift if (/-H/);
17 $user = shift if (/-u/);
18 $pass = shift if (/-p/);
19 $group = shift if (/-g/);
20 $machine = shift if (/-m/);
21 $verbose = 1 if (/-v/);
22 $doit = 0 if (/-n/);
23 }
24
25 ( $dbh = DBI->connect("DBI:mysql:database=$dbname;host=$hostname", $user, $pass))
26 or die "\tConnect not ok: $DBI::errstr\n";
27
28 if (defined($machine)) {
29 $table = "authhost";
30 $group = $machine;
31 } else {
32 $table = "authgroup";
33 $group = "default" if (!defined($group));
34 }
35
36 $token = shift;
37 $value = shift;
38 while(defined($value)) {
39 if (DO("select * from $table where lookup = '$group' and varcol = '$token'") eq "0E0") {
40 DO("insert into $table(lookup, varcol, valcol) values('$group', '$token', '$value')");
41 } else {
42 DO("update $table set valcol = '$value' where lookup = '$group' and varcol = '$token'");
43 }
44 $token = shift;
45 $value = shift;
46 }
47
48 $dbh->disconnect();
49
50 sub DO {
51 my $cmd = shift;
52 print $cmd,"\n" if ($verbose);
53 my $ret = $dbh->do($cmd) if ($doit);
54 print " returned: $ret\n" if ($verbose);
55 if ($DBI::errstr) {
56 print "db error ($ret): $DBI::errstr\n";
57 }
58 return $ret;
59 }