我正在将一个基于 Samba 的服务器组合为主域控制器,但遇到了一个本应多次解决的可爱小问题。但多次搜索并没有产生结果。我需要能够使用命令行脚本从现有组中删除现有用户。看来 usermod 很容易让我使用以下命令将用户添加到补充组:
usermod -a -G supgroup1,supgroup2 username
如果没有“-a”选项,如果用户当前是未列出的组的成员,则该用户将从该组中删除。有没有人有允许删除用户和组的规范的 perl(或 Python)脚本?我是否错过了一个明显的现有命令,或为此而闻名的解决方案?提前致谢!
感谢 JJ 提供指向 Unix::Group 模块的指针,该模块是 Unix-ConfigFile 的一部分。看起来命令 deluser 会做我想做的事,但不在我现有的任何存储库中。我继续使用 Unix:Group 模块编写了 perl 脚本。这是您的系统管理乐趣的脚本。
#!/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;