获取linux用户和组映射脚本

记录一下 python代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
import os
import sys

p = os.popen('id %s' % sys.argv[1])
s = p.read()

# s = 'uid=486(yarn) gid=484(yarn) groups=484(yarn),493(hadoop),513(supergroup)'

user = re.findall("uid=\d+\((.*?)\)", s)[0]


s = s.split("groups=")[1]
match = re.findall("\((.*?)\)", s)

groups = []
for group in match:
    groups.append(group)

print user + "=" +  ",".join(sorted(groups))

shell脚本

for user in $(cat /etc/passwd|gawk -F':' '{print $1}'); do
        python ./f.py $user
done
updatedupdated2024-11-302024-11-30