免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1750 | 回复: 8
打印 上一主题 下一主题

用户账号管理 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-01 15:27 |只看该作者 |倒序浏览
用什么命令可以查看一个组下的所有用户啊?

论坛徽章:
0
2 [报告]
发表于 2008-07-01 15:46 |只看该作者
vigr 后面的凑字数

论坛徽章:
0
3 [报告]
发表于 2008-07-01 15:56 |只看该作者

继续

我用groupadd -g 600 hunter123
      user -u 601 -g hunter123 xiaoxu1
      user -u 602 -g hunter123 xiaoxu2
      user -u 603 -g hunter123 xiaoxu3
把xiaoxu1,2,3加入到组hunter123中去,但是我想查组下所有的用户,就是xiaoxu1,2,3
gigr后显示的还是hunter123:x:600:

论坛徽章:
0
4 [报告]
发表于 2008-07-01 16:24 |只看该作者
usermod -G user group

论坛徽章:
0
5 [报告]
发表于 2008-07-01 16:47 |只看该作者

不懂

??请具体写下过程

论坛徽章:
0
6 [报告]
发表于 2008-07-01 19:15 |只看该作者
一定是具体命令嘛?

一条shell命令行应该没问题!

我不会写!
先查看你所要查看组的GID与用户所属组的GID进行模式匹配就成了!
怎么写还是请高人吧!

[ 本帖最后由 志国 于 2008-7-1 19:17 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2008-07-01 22:47 |只看该作者
cat /etc/group |grep 你要找的组

论坛徽章:
0
8 [报告]
发表于 2008-07-02 03:11 |只看该作者
原帖由 collegecar 於 2008-7-1 15:27 發表
用什麼命令可以查看一個組下的所有用戶啊?


多年前我有寫個 shell code 給你參考,可能看起來有點笨,但是那是 2002 年寫的。


  1. names=`cut -d: -f 1 /etc/passwd`

  2. for grpname in `cut -d: -f 1 /etc/group`
  3. do
  4.   echo -n "$grpname group Users: "
  5.   for name in $names
  6.   do
  7.     if groups $name | grep -E -q ":.* $grpname( |$)"; then
  8.       echo -n "$name,"
  9.     fi
  10.   done
  11.   echo -e '\b '
  12. done
复制代码


執行通常至少需要 30 ~ 40 秒時間....

那時候我有寫成 c++ 版本,獻醜一下 ~.~


  1.   useradmin.h :

  2. #include <unistd.h>
  3. #include <grp.h>
  4. #include <pwd.h>

  5. #include <string>
  6. #include <iostream>
  7. #include <map>
  8. #include <vector>

  9. using namespace std;

  10. class passwdfile;
  11. class groupfile;
  12. class passwdfile_line;
  13. class groupfile_line;

  14. struct passwd_record {
  15.     string name;
  16.     string passwd;
  17.     uid_t uid;
  18.     gid_t gid;
  19.     string gecos;
  20.     string home;
  21.     string shell;
  22. };

  23. struct group_record {
  24.     string groupname;
  25.     string grouppasswd;
  26.     gid_t gid;
  27.     vector < string > member;
  28. };

  29. class passwdfile_line {
  30.   public:
  31.     passwdfile_line();
  32.     passwdfile_line(const struct passwd_record &p);

  33.     string getname(void);
  34.     string getpasswd(void);
  35.     string getgecos(void);
  36.     string gethome(void);
  37.     string getshell(void);
  38.     uid_t getuid(void);
  39.     gid_t getgid(void);

  40.   private:
  41.     struct passwd_record data;
  42. };

  43. class groupfile_line {
  44.   private:

  45.     struct group_record data;

  46.   public:

  47.      groupfile_line();
  48.      groupfile_line(struct group_record &p);

  49.     string getname(void);
  50.     string getpasswd(void);
  51.     gid_t getgid(void);
  52.     vector < string > getmember(void);
  53. };

  54. class passwdfile {

  55.   public:

  56.     passwdfile();

  57.     int getcount(void);
  58.     void release(void);
  59.     passwdfile_line get(int i);
  60.     void select(void);

  61.   private:

  62.     void setdata(struct passwd *pwstruct);

  63.     vector < passwd_record > user;
  64. };

  65. class groupfile {

  66.   public:

  67.     groupfile();

  68.     void release(void);
  69.     int getcount(void);
  70.     groupfile_line get(int index);
  71.     void select(void);

  72.   private:

  73.     void setdata(struct group *grp_struct);

  74.     vector < struct group_record >group;
  75. };

  76.    passwd.cpp :

  77. #include "useradmin.h"

  78. #include <string>
  79. #include <iostream>
  80. #include <map>
  81. #include <vector>

  82. using namespace std;

  83. passwdfile_line::passwdfile_line()
  84. {
  85.     memset(&data, 0, sizeof(struct passwd_record));
  86. }

  87. passwdfile_line::passwdfile_line(const struct passwd_record &p)
  88. {
  89.     data.name = p.name;
  90.     data.passwd = p.passwd;
  91.     data.gecos = p.gecos;
  92.     data.home = p.home;
  93.     data.shell = p.shell;
  94.     data.uid = p.uid;
  95.     data.gid = p.gid;
  96. }


  97. string passwdfile_line::getname(void)
  98. {
  99.     return data.name;
  100. }

  101. string passwdfile_line::getpasswd(void)
  102. {
  103.     return data.passwd;
  104. }

  105. string passwdfile_line::getgecos(void)
  106. {
  107.     return data.gecos;
  108. }

  109. string passwdfile_line::gethome(void)
  110. {
  111.     return data.home;
  112. }

  113. string passwdfile_line::getshell(void)
  114. {
  115.     return data.shell;
  116. }

  117. uid_t passwdfile_line::getuid(void)
  118. {
  119.     return data.uid;
  120. }

  121. gid_t passwdfile_line::getgid(void)
  122. {
  123.     return data.gid;
  124. }

  125. passwdfile::passwdfile()
  126. {
  127. }

  128. int passwdfile::getcount(void)
  129. {
  130.     return user.size();
  131. }
  132. void passwdfile::release(void)
  133. {
  134.     user.clear();
  135. }

  136. passwdfile_line passwdfile::get(int i)
  137. {
  138.     return i <= getcount()? passwdfile_line(user[i]) : passwdfile_line();
  139. }

  140. void passwdfile::select(void)
  141. {
  142.     struct passwd *pwstruct;

  143.     while ((pwstruct = getpwent()) != NULL)
  144.         setdata(pwstruct);
  145. }

  146. void passwdfile::setdata(struct passwd *pwstruct)
  147. {
  148.     struct passwd_record user_detail;
  149.     user_detail.name = pwstruct->pw_name;
  150.     user_detail.passwd = pwstruct->pw_passwd;
  151.     user_detail.uid = pwstruct->pw_uid;
  152.     user_detail.gid = pwstruct->pw_gid;
  153.     user_detail.gecos = pwstruct->pw_gecos;
  154.     user_detail.home = pwstruct->pw_dir;
  155.     user_detail.shell = pwstruct->pw_shell;
  156.     user.push_back(user_detail);
  157. }

  158.    group.cpp :

  159. #include "useradmin.h"

  160. #include <string>
  161. #include <iostream>
  162. #include <map>
  163. #include <vector>

  164. using namespace std;

  165. groupfile_line::groupfile_line()
  166. {
  167.     memset(&data, 0, sizeof(struct group_record));
  168. }

  169. groupfile_line::groupfile_line(struct group_record &p)
  170. {
  171.     data.groupname = p.groupname;
  172.     data.grouppasswd = p.grouppasswd;
  173.     data.gid = p.gid;
  174.     data.member = p.member;
  175. }

  176. string groupfile_line::getname(void)
  177. {
  178.     return data.groupname;
  179. }

  180. string groupfile_line::getpasswd(void)
  181. {
  182.     return data.grouppasswd;
  183. }
  184. gid_t groupfile_line::getgid(void)
  185. {
  186.     return data.gid;
  187. }

  188. vector < string > groupfile_line::getmember(void)
  189. {
  190.     return data.member;
  191. }

  192. groupfile::groupfile()
  193. {
  194. }

  195. void groupfile::release(void)
  196. {
  197.     group.clear();
  198. }

  199. int groupfile::getcount(void)
  200. {
  201.     return group.size();
  202. }

  203. groupfile_line groupfile::get(int index)
  204. {
  205.   return index<=getcount() ? groupfile_line(group[index]) : groupfile_line();
  206. }

  207. void groupfile::select(void)
  208. {
  209.     struct group *grp_struct;

  210.     while ((grp_struct = getgrent()) != NULL)
  211.         setdata(grp_struct);
  212. }

  213. void groupfile::setdata(struct group *grp_struct)
  214. {
  215.     struct group_record group_detail;

  216.     group_detail.groupname = grp_struct->gr_name;
  217.     group_detail.grouppasswd = grp_struct->gr_passwd;
  218.     group_detail.gid = grp_struct->gr_gid;

  219.     for (int i = 0; grp_struct->gr_mem[i] != NULL; ++i)
  220.         group_detail.member.push_back(string(grp_struct->gr_mem[i]));

  221.     group.push_back(group_detail);
  222. }

  223.   main.cpp :

  224. #include "useradmin.h"

  225. #include <string>
  226. #include <iostream>
  227. #include <map>
  228. #include <vector>

  229. using namespace std;

  230. int main()
  231. {
  232.     passwdfile users;
  233.     groupfile groups;
  234.     users.select();
  235.     groups.select();

  236.     for (int i = 0, j = groups.getcount(); i < j; i++) {
  237.         string g = groups.get(i).getname();

  238.         cout << "Group[" << g << "]: ";

  239.         vector < string > a1 = groups.get(i).getmember();

  240.         vector < string >::const_iterator cur1 = a1.begin();
  241.         vector < string >::const_iterator cur2 = a1.end();
  242.         vector < string >::const_iterator pos;

  243.         for (pos = cur1; pos != cur2; ++pos)
  244.             cout << *pos << " ";

  245.         cout << endl;

  246.     }

  247.     users.select();
  248.     groups.select();

  249.     for (int i = 0, j = groups.getcount(); i < j; i++) {
  250.         string g = groups.get(i).getname();

  251.         cout << "Group[" << g << "]: ";

  252.         vector < string > a1 = groups.get(i).getmember();
  253.     return 0;
复制代码


速度很快,需要幾秒即可完成。

不過剛好我最近有學 python,我剛剛寫了一下 code,我想也單純多.


  1. #!/usr/bin/python

  2. # ==========================================================================

  3. #group_info ==> { gid : [ groupname , user1 , user2 ... ] }

  4. group_info = {}

  5. group_file = "/etc/group"
  6. g_handle = open(group_file,"r")

  7. for line in g_handle:
  8.   group_name, group_passwd, group_gid, g_users = line.rstrip().split(":",4)
  9.   s = []
  10.   s.append(group_name)
  11.   for u in g_users.rstrip().split(","):
  12.     if u != "" :
  13.       s.append(u)
  14.   group_info[group_gid] = s

  15. # ==========================================================================
  16. #passwd_info  ==> { gid : username }

  17. password_file = "/etc/passwd"
  18. passwd_info = {}

  19. p_handle = open(password_file,"r")

  20. for line in p_handle:
  21.   user_name, user_passwd, uid, gid, left = line.rstrip().split(":",4)
  22.   passwd_info[gid] = user_name

  23. # ==========================================================================

  24. for gid in passwd_info.keys():
  25.   if group_info.has_key(gid):
  26.     group_info[gid].append(passwd_info[gid])

  27. # ==========================================================================
  28. for gid in passwd_info.keys():
  29.   s = group_info.get(gid)
  30.   print "group %s's member:" % s[0],
  31.   for member in s[1:]:
  32.     print "%s," % member,
  33.   print ""

  34. # ==========================================================================

  35. p_handle.close()
  36. g_handle.close()

复制代码


運作速度很快,提供給大家參考與指教。

--

[ 本帖最后由 kenduest 于 2008-7-2 03:14 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2008-07-02 08:52 |只看该作者
K 版给出的 Python 实现,会打印重复的用户名。
这里改进一下一个地方,加入一条判断语句。献丑了。

  1. for gid in passwd_info.keys():
  2.   if group_info.has_key(gid):
  3.     if passwd_info[gid] not in group_info[gid][1:]:
  4.       group_info[gid].append(passwd_info[gid])
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP