QUOTE:原帖由 collegecar 於 2008-7-1 15:27 發表

用什麼命令可以查看一個組下的所有用戶啊?
多年前我有寫個 shell code 給你參考,可能看起來有點笨,但是那是 2002 年寫的。
names=`cut -d: -f 1 /etc/passwd`
for grpname in `cut -d: -f 1 /etc/group`
do
echo -n "$grpname group Users: "
for name in $names
do
if groups $name | grep -E -q ":.* $grpname( |$)"; then
echo -n "$name,"
fi
done
echo -e '\b '
done
复制代码
執行通常至少需要 30 ~ 40 秒時間....
那時候我有寫成 c++ 版本,獻醜一下 ~.~
useradmin.h :
#include <unistd.h>
#include <grp.h>
#include <pwd.h>
#include <string>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
class passwdfile;
class groupfile;
class passwdfile_line;
class groupfile_line;
struct passwd_record {
string name;
string passwd;
uid_t uid;
gid_t gid;
string gecos;
string home;
string shell;
};
struct group_record {
string groupname;
string grouppasswd;
gid_t gid;
vector < string > member;
};
class passwdfile_line {
public:
passwdfile_line();
passwdfile_line(const struct passwd_record &p);
string getname(void);
string getpasswd(void);
string getgecos(void);
string gethome(void);
string getshell(void);
uid_t getuid(void);
gid_t getgid(void);
private:
struct passwd_record data;
};
class groupfile_line {
private:
struct group_record data;
public:
groupfile_line();
groupfile_line(struct group_record &p);
string getname(void);
string getpasswd(void);
gid_t getgid(void);
vector < string > getmember(void);
};
class passwdfile {
public:
passwdfile();
int getcount(void);
void release(void);
passwdfile_line get(int i);
void select(void);
private:
void setdata(struct passwd *pwstruct);
vector < passwd_record > user;
};
class groupfile {
public:
groupfile();
void release(void);
int getcount(void);
groupfile_line get(int index);
void select(void);
private:
void setdata(struct group *grp_struct);
vector < struct group_record >group;
};
passwd.cpp :
#include "useradmin.h"
#include <string>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
passwdfile_line::passwdfile_line()
{
memset(&data, 0, sizeof(struct passwd_record));
}
passwdfile_line::passwdfile_line(const struct passwd_record &p)
{
data.name = p.name;
data.passwd = p.passwd;
data.gecos = p.gecos;
data.home = p.home;
data.shell = p.shell;
data.uid = p.uid;
data.gid = p.gid;
}
string passwdfile_line::getname(void)
{
return data.name;
}
string passwdfile_line::getpasswd(void)
{
return data.passwd;
}
string passwdfile_line::getgecos(void)
{
return data.gecos;
}
string passwdfile_line::gethome(void)
{
return data.home;
}
string passwdfile_line::getshell(void)
{
return data.shell;
}
uid_t passwdfile_line::getuid(void)
{
return data.uid;
}
gid_t passwdfile_line::getgid(void)
{
return data.gid;
}
passwdfile::passwdfile()
{
}
int passwdfile::getcount(void)
{
return user.size();
}
void passwdfile::release(void)
{
user.clear();
}
passwdfile_line passwdfile::get(int i)
{
return i <= getcount()? passwdfile_line(user
) : passwdfile_line();
}
void passwdfile::select(void)
{
struct passwd *pwstruct;
while ((pwstruct = getpwent()) != NULL)
setdata(pwstruct);
}
void passwdfile::setdata(struct passwd *pwstruct)
{
struct passwd_record user_detail;
user_detail.name = pwstruct->pw_name;
user_detail.passwd = pwstruct->pw_passwd;
user_detail.uid = pwstruct->pw_uid;
user_detail.gid = pwstruct->pw_gid;
user_detail.gecos = pwstruct->pw_gecos;
user_detail.home = pwstruct->pw_dir;
user_detail.shell = pwstruct->pw_shell;
user.push_back(user_detail);
}
group.cpp :
#include "useradmin.h"
#include <string>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
groupfile_line::groupfile_line()
{
memset(&data, 0, sizeof(struct group_record));
}
groupfile_line::groupfile_line(struct group_record &p)
{
data.groupname = p.groupname;
data.grouppasswd = p.grouppasswd;
data.gid = p.gid;
data.member = p.member;
}
string groupfile_line::getname(void)
{
return data.groupname;
}
string groupfile_line::getpasswd(void)
{
return data.grouppasswd;
}
gid_t groupfile_line::getgid(void)
{
return data.gid;
}
vector < string > groupfile_line::getmember(void)
{
return data.member;
}
groupfile::groupfile()
{
}
void groupfile::release(void)
{
group.clear();
}
int groupfile::getcount(void)
{
return group.size();
}
groupfile_line groupfile::get(int index)
{
return index<=getcount() ? groupfile_line(group[index]) : groupfile_line();
}
void groupfile::select(void)
{
struct group *grp_struct;
while ((grp_struct = getgrent()) != NULL)
setdata(grp_struct);
}
void groupfile::setdata(struct group *grp_struct)
{
struct group_record group_detail;
group_detail.groupname = grp_struct->gr_name;
group_detail.grouppasswd = grp_struct->gr_passwd;
group_detail.gid = grp_struct->gr_gid;
for (int i = 0; grp_struct->gr_mem != NULL; ++i)
group_detail.member.push_back(string(grp_struct->gr_mem));
group.push_back(group_detail);
}
main.cpp :
#include "useradmin.h"
#include <string>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main()
{
passwdfile users;
groupfile groups;
users.select();
groups.select();
for (int i = 0, j = groups.getcount(); i < j; i++) {
string g = groups.get(i).getname();
cout << "Group[" << g << "]: ";
vector < string > a1 = groups.get(i).getmember();
vector < string >::const_iterator cur1 = a1.begin();
vector < string >::const_iterator cur2 = a1.end();
vector < string >::const_iterator pos;
for (pos = cur1; pos != cur2; ++pos)
cout << *pos << " ";
cout << endl;
}
users.select();
groups.select();
for (int i = 0, j = groups.getcount(); i < j; i++) {
string g = groups.get(i).getname();
cout << "Group[" << g << "]: ";
vector < string > a1 = groups.get(i).getmember();
return 0;
复制代码
速度很快,需要幾秒即可完成。
不過剛好我最近有學 python,我剛剛寫了一下 code,我想也單純多.
#!/usr/bin/python
# ==========================================================================
#group_info ==> { gid : [ groupname , user1 , user2 ... ] }
group_info = {}
group_file = "/etc/group"
g_handle = open(group_file,"r")
for line in g_handle:
group_name, group_passwd, group_gid, g_users = line.rstrip().split(":",4)
s = []
s.append(group_name)
for u in g_users.rstrip().split(","):
if u != "" :
s.append(u)
group_info[group_gid] = s
# ==========================================================================
#passwd_info ==> { gid : username }
password_file = "/etc/passwd"
passwd_info = {}
p_handle = open(password_file,"r")
for line in p_handle:
user_name, user_passwd, uid, gid, left = line.rstrip().split(":",4)
passwd_info[gid] = user_name
# ==========================================================================
for gid in passwd_info.keys():
if group_info.has_key(gid):
group_info[gid].append(passwd_info[gid])
# ==========================================================================
for gid in passwd_info.keys():
s = group_info.get(gid)
print "group %s's member:" % s[0],
for member in s[1:]:
print "%s," % member,
print ""
# ==========================================================================
p_handle.close()
g_handle.close()
复制代码
運作速度很快,提供給大家參考與指教。
--
[ 本帖最后由 kenduest 于 2008-7-2 03:14 编辑 ]