Python获取一个用户名的组ID过程解析
getpwname只能得到gid一个username。
import pwd myGroupId = pwd.getpwnam(username).pw_gid
getgroups只能获取groups脚本用户。
import os myGroupIds = os.getgroups()
我怎么能得到所有groups的任意一个username,比如id -Gn命令?
id -Gn `whoami`
解决方案
#!/usr/bin/env python import grp, pwd user = "myname" groups = [g.gr_name for g in grp.getgrall() if user in g.gr_mem] gid = pwd.getpwnam(user).pw_gid groups.append(grp.getgrgid(gid).gr_name) print groups
相关推荐
luguanyou 2020-10-05
sdmzhu 2020-09-01
深井兮兮 2020-07-18
yoshubom 2020-07-18
ITlover00 2020-04-29
baike 2020-04-26
atb 2020-04-18
hitxueliang 2020-03-23
ALiDan 2020-02-16
老谢的自留地 2020-01-11
赵家小少爷 2019-12-18
secondid 2019-12-23
chvnetcom 2019-12-15
benico 2019-12-15
pengkunstone 2019-12-14
zhongzhiwei 2019-12-11
Proudoffaith 2019-12-10
sunln00 2019-12-09