-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodgroupname.pl
More file actions
executable file
·71 lines (63 loc) · 1.82 KB
/
modgroupname.pl
File metadata and controls
executable file
·71 lines (63 loc) · 1.82 KB
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
#!/usr/bin/perl
# script to modify user groups
# designed to be called from fish.pl, but could also be used directly (sudo)
# arguments -u user -a group[,group,...] -d group[,group,...]
use strict;
# options
my %options = (
'verbose' => 1, # weather verbose output is on or off
);
# main code ... no need to modify below here
$|=1;
if ($< != 0) { die "Sorry. You must be root to run $0\n"; }
# get arguments
my $in = &get_args(\@ARGV);
# delete user from groups
if (scalar @{$in->{del}} > 0) {
if ($options{verbose}) { print "Removing group(s) for user $in->{user}\n";}
&modgroup('-d',$in->{user},$in->{del});
}
# add user to groups
if (scalar @{$in->{add}} > 0) {
if ($options{verbose}) { print "Adding group(s) for user $in->{user}\n";}
&modgroup('-a',$in->{user},$in->{add});
}
# subroutines
sub modgroup {
my ($action,$user,$groups) = @_;
my @command = ("/usr/bin/gpasswd",$action,$user);
foreach my $group (@$groups) {
system @command, $group;
}
}
sub get_args {
my $args = shift;
my ($user,@add,@del) = undef;
while (my $arg = shift @$args) {
if ($arg eq '-u') {
$user = shift @$args;
} elsif ($arg eq '-a') {
my $addlist = shift @$args;
@add = split /,/,$addlist;
} elsif ($arg eq '-d') {
my $dellist = shift @$args;
@del = split /,/,$dellist;
} else {
print STDERR "Invalid argument $arg\n\n";
&help;
}
}
unless ($user && (scalar @add > 0 || scalar @del > 0)) {
print STDERR "Missing action.\n\n";
&help;
}
return {'user'=>$user,'add'=>\@add,'del'=>\@del};
}
sub help {
die <<MSG;
$0 : script to modify user groups
Description: designed to be called from fish.pl, but could also
be used directly (via sudo)
Usage: modgroup -u user {-a group[,group,...] | -d group[,group,...]}
MSG
}