用于从组中删除用户的 Perl 或 Python 脚本

发布于 2024-07-05 16:49:30 字数 2090 浏览 11 评论 0原文

我正在将一个基于 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;

I am putting together a Samba-based server as a Primary Domain Controller, and ran into a cute little problem that should have been solved many times over. But a number of searches did not yield a result. I need to be able to remove an existing user from an existing group with a command line script. It appears that the usermod easily allows me to add a user to a supplementary group with this command:

usermod -a -G supgroup1,supgroup2 username

Without the "-a" option, if the user is currently a member of a group which is not listed, the user will be removed from the group. Does anyone have a perl (or Python) script that allows the specification of a user and group for removal? Am I missing an obvious existing command, or well-known solution forthis? Thanks in advance!

Thanks to J.J. for the pointer to the Unix::Group module, which is part of Unix-ConfigFile. It looks like the command deluser would do what I want, but was not in any of my existing repositories. I went ahead and wrote the perl script using the Unix:Group Module. Here is the script for your sysadmining pleasure.

#!/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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

纵山崖 2024-07-12 16:49:30

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

要将成员添加到组中,请使用带有 -a 开关的 gpasswd 命令和您要添加的用户 ID:

gpasswd -a userid mygroup

使用相同的命令从组中删除用户,但 -d 开关而不是 -a:

gpasswd -d userid mygroup

“man gpasswd”以获取更多信息...

我寻找了很长时间才找到这个。 有时需要付出太多的努力才能不重新发明轮子......

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

To add members to the group, use the gpasswd command with the -a switch and the user id you wish to add:

gpasswd -a userid mygroup

Remove users from a group with the same command, but a -d switch rather than -a:

gpasswd -d userid mygroup

"man gpasswd" for more info...

I looked for ages to find this. Sometimes it takes too much effort not to reinvent the wheel...

生死何惧 2024-07-12 16:49:30

我为你找到了这个。 它应该做你需要的事情。 据我所知,Perl 没有任何内置函数用于从组中删除用户。 它有几个用于查看用户或进程的组 ID。

I found This for you. It should do what you need. As far as I can tell Perl does not have any built in functions for removing users from a group. It has several for seeing the group id of a user or process.

软甜啾 2024-07-12 16:49:30

看起来 deluser --group [groupname] 应该这样做。

如果没有,groups 命令会列出用户所属的组。 应该相当简单地使用一些 Perl 将该列表捕获到数组中(或将其映射到散列中),删除不需要的组,然后将其反馈给用户模式

It looks like deluser --group [groupname] should do it.

If not, the groups command lists the groups that a user belongs to. It should be fairly straightforward to come up with some Perl to capture that list into an array (or map it into a hash), delete the unwanted group(s), and feed that back to usermod.

百变从容 2024-07-12 16:49:30

这是一个非常简单的小 Perl 脚本,它应该为您提供所需的组列表:

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

获取和清理所需的参数留给读者作为练习。

Here's a very simple little Perl script that should give you the list of groups you need:

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

Getting and sanitizing the required arguments is left as an execrcise for the reader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文