python实现查询gitlab所有组人员和权限

通过使用python-gitlab库实现,环境是python3.6.9,实现统计gitlab所有组人员和权限,直接贴出代码

import gitlab
import sys

class Git_rightread():

	def __init__(self):
		self.gl = gitlab.Gitlab.from_config('outnet', ['gitlab/gitlab.cfg'])


	def get_all_groups(self):
		groups = self.gl.groups.list(all=True)
		return groups

	def get_group_members(self, group):
		members = group.members.list()
		return members


if __name__ == '__main__':
	out_git = Git_rightread()
	right = []
	groups = out_git.get_all_groups()
	savaout = sys.stdout
	file = open('out.txt', 'w+')
	sys.stdout = file
	for group in groups:
		right.append(group.name+'\n')
		members = out_git.get_group_members(group)
		for member in members:
			right.append(member.name+','+str(member.access_level)+'\n')
			#print(member.name,member.access_level)
	for i in right:
		sys.stdout.write(i)
	sys.stdout = savaout
	file.close()

这里基本都是调用已经实现的库,可以看看我这篇文档Gitlab使用python批量创建用户
,里面给出了这个库的地址
可以把我主函数部门实现成一个函数进行调用,程序最后会输出到out.txt里面

你可能感兴趣的:(运维相关)