Perl or Python script to remove a user from a group

I am building a Samba-based server as the primary domain controller and have come across a cute little problem that had to be resolved many times. But a series of searches did not give a result. I need to remove an existing user from an existing group using the command line script. It seems that usermod easily allows you to add a user to an additional group with this command:

usermod -a -G supgroup1,supgroup2 username

Without the “-a” option, if the user is currently a member of a group that is not listed, the user will be removed from the group. Does anyone have a perl (or Python) script that allows you to specify the user and group to delete? Am I missing an obvious existing team or a known solution? Thanks in advance!

Thanks to JJ for a pointer to the Unix :: Group module, which is part of the Unix-ConfigFile. It appears that the deluser command will do what I want, but was not in any of the existing repositories. I went ahead and wrote a perl script using Unix: Group Module. Here is a script for your sysadmining fun.

#!/usr/bin/perl
#
# Usage:   removegroup.pl login group
# Purpose: Removes a user from a group while retaining current primary and
#          supplementary groups.
# Notes:   There is a Debian specific utility that can do this called deluser,
#          but I did not want any cross-distribution dependencies
#
# Date:   25 September 2008

# Validate Arguments (correct number, format etc.)
if ( ($#ARGV < 1) || (2 < $#ARGV) ) {
  print "\nUsage: removegroup.pl login group\n\n";
  print "EXIT VALUES\n";
  print "     The removeuser.pl script exits with the following values:\n\n";
  print "     0 success\n\n";
  print "     1 Invalid number of arguments\n\n";
  print "     2 Login or Group name supplied greater than 16 characters\n\n";
  print "     3 Login and/or Group name contains invalid characters\n\n";
  exit 1;
}

# Check for well formed group and login names
if ((16 < length($ARGV[0])) ||(16 < length($ARGV[1])))
{
  print "Usage: removegroup.pl login group\n";
  print "ERROR: Login and Group names must be less than 16 Characters\n";
  exit 2;
}

if ( ( $ARGV[0] !~ m{^[a-z_]+[a-z0-9_-]*$}) || ( $ARGV[0] !~ m{^[a-z_]+[a-z0-9_-]*$} ) )
{
  print "Usage: removegroup.pl login group\n";
  print "ERROR: Login and/or Group name contains invalid characters\n";
  exit 3;
}

# Set some variables for readability
$login=$ARGV[0];
$group=$ARGV[1];

# Requires the GroupFile interface from perl-Unix-Configfile
use Unix::GroupFile;

$grp = new Unix::GroupFile "/etc/group";
$grp->remove_user("$group", "$login");
$grp->commit();
undef $grp;
exit 0;
+3
source share
4 answers

Web link: http://www.ibm.com/developerworks/linux/library/l-roadmap4/

To add members to a group, use the gpasswd command with the -a switch and the identifier of the user you want to add:

gpasswd -a userid mygroup

Remove users from the group with the same command, but instead of the -d switch instead of -a:

gpasswd -d userid mygroup

"man gpasswd" ...

, . , ...

+2

. , . , Perl . .

+2

, deluser - group [groupname] .

, , . Perl, ( ), () usermod.

+1

Perl script, , :

my $user = 'user';
my $groupNoMore = 'somegroup';
my $groups = join ',', grep { $_ ne $groupNoMore } split /\s/, `groups $user`;

Receiving and deactivating the necessary arguments remains as execrcise to the reader.

+1
source

Source: https://habr.com/ru/post/1697479/


All Articles