Advertisement
Guest User

Untitled

a guest
Dec 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import csv
  4.  
  5. FILENAME = "users.csv"
  6.  
  7. all_data = []
  8. users = []
  9. systems = []
  10.  
  11. try:
  12.     etc_pass = open("/etc/passwd", "r").read()[:-1]
  13. except IOError:
  14.     exit()
  15.  
  16.  
  17. for line in etc_pass.split('\n'):
  18.     uid = int(line.split(':')[2])
  19.     username = line.split(':')[0]
  20.     if uid > 999 and uid < 65534 :
  21.         users.append(username)
  22.     else:
  23.         systems.append(username)
  24.  
  25. try:
  26.     group_all = open("/etc/group", "r").read()[:-1]
  27. except IOError:
  28.     exit()
  29.  
  30. for user in users:
  31.     collect_groups = []
  32.     for i in group_all.split('\n'):
  33.         group_name = i.split(':')[0]
  34.         group_user = i.split(':')[3]
  35.         if user in group_user:
  36.             collect_groups.append(group_name)
  37.     collect_list = []
  38.     collect_list.append(user)
  39.     collect_list.append(collect_groups)
  40.     all_data.append(collect_list)
  41.  
  42. with open(FILENAME, "w") as file:
  43.     for i in all_data:
  44.         writer = csv.writer(file)
  45.         writer.writerow(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement