How to Get Groupname When I Have The Groupid

How to get groupname when I have the groupid

Leaving aside the possibility that you're supplying a wrong group ID, this
might be a bug in LDAP setup, which manifests in reverse group resolution not
working. This is reinforced by the fact that this works on a plain "files"
setup.

The getent(1) states:

group     When no key is provided, use setgrent(3), getgrent(3), and
endgrent(3) to enumerate the group database. When one or
more key arguments are provided, pass each numeric key to
getgrgid(3) and each nonnumeric key to getgrnam(3) and
display the result.

This could mean that getgrgid(3) fails on your setup.

To test this compile this program (getgrgid_test.c) with "make getgrgid_test":

#include <stdio.h>
#include <sys/types.h>
#include <grp.h>

int
main(int argc, char **argv)
{
int gid;
struct group *g;

if (argc != 2) {
fprintf(stderr, "Invalid number of positional arguments\n");
fprintf(stderr, "Usage getgrid_test GID\n");
return 1;
}
gid = atoi(argv[1]);
g = getgrgid(gid);
if (g == NULL) {
fprintf(stderr, "gid %d not found\n", gid);
return 1;
}
printf("%s\n", g->gr_name);
return 0;
}

Then run it with your gid like this:

getgrgid_test GID

If it doesn't produce a group name report to your system administrators.

Otherwise, if it does work, but "getent group GID" doesn't, it's a bug in
"getent".

Linux: How to get group id from group name? and vice versa?

getent group 124
# mysql:x:124:

getent group mysql
# mysql:x:124:

How to get group names based on group id in azure active directory using graph service client

Looks like you want to store the first member display name of each page, however accessGroups.Add(new AccessGroup { Id = id, Name = group_name }); is being called only once. You should add it within the while block.

If you want to get the displayName only for the first group member then you may try something like this:

async Task GetGroupFirstMemberDisplayName()
{

var members = (await client.Groups.Request().Select(g => g.Id).Expand("members($select=displayName)").GetAsync())
.Select(g => new { GroupId = g.Id, FirstMemberDisplayName = g.Members.OfType<Group>().FirstOrDefault()?.DisplayName });

foreach (var item in members)
{
Console.WriteLine($"Group {item.GroupId} first member display name: {item?.FirstMemberDisplayName ?? ""}");
}
}

If you just want to get the displayName for all your groups then you can try something like:

async Task GetGroupDisplayNames()
{

var names = (await client.Groups.Request().Select(g => g.DisplayName).GetAsync())
.Select(g => g.DisplayName);

Console.WriteLine(string.Join(", ", names));
}

How to get the users of the group when group id and group name is known

This is really just a "Just in case FYI" - You have not stated what you are trying to do with the members of a group once you have retrieved them, but keep in mind that most members are members of groups with a certain scope rather than for all publications.

So be careful if you are trying to do something like getting all the editors for Publication A, you may run into problems if you are using just the Editors group, as it could contain members who do not have access to Publication A.

Output groups one per line, with group name and group ID

And there comes perl:

$ id | perl -pe "s/.*=/,/;s/,(\d+)\(([^)]+)\)/'\2',\1\n/g"                                                                 
'arobert',1000
'adm',4
'cdrom',24
'sudo',27
'dip',30
'plugdev',46
'lpadmin',113
'sambashare',128

Explanations:

Two find and replace commands will be executed:

  • s/.*groups=/,/ is used to remove everything before the groups definition
  • s/,(\d+)\(([^)]+)\)/'\2',\1\n/g will put in place the following actions detailed @demo

It would be difficult to write something shorter than this.
Also if you are already good with sed and regex, than perl can be a nice plus to add to your list of skills.

As mentioned by Ed Morton, the current perl command generates an extra EOL at the end of the output. If you want to remove it you can use the following enhanced perl command:

$ id | perl -pe "s/.*=/,/;s/,(\d+)\(([^)]+)\)/'\2',\1\n/g;s/\n\n/\n/"

Thanks Ed!!!

get group id by group name (Python, Unix)

If you read the grp module documentation you'll see that grp.getgrnam(groupname) will return one entry from the group database, which is a tuple-like object. You can either access the information by index or by attribute:

>>> import grp
>>> groupinfo = grp.getgrnam('root')
>>> print groupinfo[2]
0
>>> print groupinfo.gr_gid
0

Other entries are the name, the encrypted password (usually empty, if using a shadow file, it'll be a dummy value) and all group member names. This works fine on any Unix system, including my Mac OS X laptop:

>>> import grp
>>> admin = grp.getgrnam('admin')
>>> admin
('admin', '*', 80, ['root', 'admin', 'mj'])
>>> admin.gr_name
'admin'
>>> admin.gr_gid
80
>>> admin.gr_mem
['root', 'admin', 'mj']

The module also offers a method to get entries by gid, and as you discovered, a method to loop over all entries in the database:

>>> grp.getgrgid(80)
('admin', '*', 80, ['root', 'admin', 'mj'])
>>> len(grp.getgrall())
73

Last but not least, python offers similar functionality to get information on the password and shadow files, in the pwd and spwd modules, which have a similar API.

mysql - display all group name and show the name belong to that group

You need to use LEFT JOIN to get groups that have no members.

SELECT g.groupname, IFNULL(m.name, '') name
FROM group AS g
LEFT JOIN member AS m ON g.groupID = m.groupID AND m.classname = '1A'


Related Topics



Leave a reply



Submit