File: setupuser

package info (click to toggle)
net-snmp 5.9.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 38,720 kB
  • sloc: ansic: 282,878; perl: 17,704; sh: 12,151; makefile: 2,711; python: 734; xml: 663; pascal: 62; sql: 47
file content (45 lines) | stat: -rwxr-xr-x 1,271 bytes parent folder | download | duplicates (13)
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
#!/usr/bin/perl

use DBI;
$hostname = 'localhost';          # Host that serves the mSQL Database
$dbname = 'snmp';                 # mySQL Database name
$doit = 1;

sub usage {
    print "$0 [-H host] [-u user] [-p password] [-v] [-h] [-n] [-d] [-a] GROUP USER EMAILADDRESS\n";
    exit 0;
}

while ($#ARGV > -1 && $ARGV[0] =~ /^-/) {
    $_ = shift @ARGV;
    usage if (/-h/);
    $hostname = shift if (/-H/);
    $sqluser = shift if (/-u/);
    $pass = shift if (/-p/);
    $admin = 1 if (/-a/);
    $verbose = 1 if (/-v/);
    $delete = 1 if (/-d/);
    $doit = 0 if (/-n/);
}

($group, $user, $email) = @ARGV;

die "group $group is a reserved group name, you can't use it.  Sorry." if ($group eq "default");

die "no group specified" if (!defined($group));

( $dbh = DBI->connect("DBI:mysql:database=$dbname;host=$hostname", $sqluser, $pass))
    or die "\tConnect not ok: $DBI::errstr\n";

DO("insert into usergroups(user, groupname, isadmin) values('$user', '$group', " . (($admin) ? "'Y'" : "'N'") . ")");
if (defined($email)) {
    DO("insert into oncall(user, groupname, email, days, hours) values('$user', '$group', '$email', '*', '*')");
}

$dbh->disconnect();

sub DO {
    my $cmd = shift;
    print $cmd,"\n" if ($verbose);
    $dbh->do($cmd) if ($doit);
}