103 lines
3.2 KiB
Python
103 lines
3.2 KiB
Python
#! /usr/bin/env python3
|
|
from pprint import pprint
|
|
import sys
|
|
import utilities
|
|
from argv import argv_value
|
|
|
|
|
|
def read_file(path: str) -> "List of lines":
|
|
#f = open(path, 'r')
|
|
with open(path, 'r') as f:
|
|
# f.read(buflen) - binary read
|
|
# f.readline() - 1 Textzeile lesen -> str
|
|
# f.readlines - alle Textzeilen lesen ->str
|
|
# f ist gleichzeitig Iterator
|
|
# 1 möglichkeit
|
|
#lines = list(map(lambda s : s.rstrip(), f))
|
|
# 2 möflichkeit
|
|
lines = list(map(str.rstrip, f))
|
|
# 3 möglichkeit
|
|
#lines = []
|
|
#for line in f.readlines():
|
|
# lines.append(line.rstrip())
|
|
#f.close()
|
|
return lines
|
|
|
|
def parse_passwd_lines(line: str) -> "Dict of passwd details":
|
|
parts = line.split(':')
|
|
userdict = {
|
|
"username" : parts[0],
|
|
"uid" : int(parts[2]),
|
|
"gid" : int(parts[3]),
|
|
"realname" : parts[4].split(',')[0],
|
|
"gecos" : parts[4],
|
|
"home" : parts[5],
|
|
"shell": parts[6]
|
|
}
|
|
return userdict
|
|
|
|
def build_userlist(lines) -> "List of user dicts":
|
|
result = []
|
|
for line in lines:
|
|
result.append(parse_passwd_lines(line))
|
|
return result
|
|
|
|
|
|
def print_userlist_sorted_by_username(userlist):
|
|
#ruft für jedes Element key(elem) also e im Lambda-Ausdruck ist ein Element der Liste user dict
|
|
for user in sorted(userlist, key=lambda e: e['username'].lower()):
|
|
#print("{username:{width}} {realname}".format(width=32 **user))
|
|
print("{:5} {:32} {}".format(
|
|
user['uid'],
|
|
user['username'],
|
|
user['realname'],
|
|
user['home'],
|
|
user['shell']
|
|
))
|
|
def print_to_file(userlist):
|
|
userlen = max(map(lambda u: len(u['username']), userlist))
|
|
try:
|
|
with open('/etc/userlist.txt', 'w') as outfile:
|
|
for user in userlist:
|
|
print("{uid:5} {username:{width}} {realname}".format(
|
|
width=userlen, **user),
|
|
file=outfile)
|
|
except ImportError:
|
|
pass
|
|
# Wird vom Mdule utilities gesteuert
|
|
# except OSError as e:
|
|
# print("Kann Datei nicht schreiben", e, file=sys.stderr)
|
|
else:
|
|
print("Wird ausgefuehrt wenn keine Execption ausgeloest wird.")
|
|
finally:
|
|
print("Wird immer durchlaufen, unabhaengig von ausgeloesten Exceptions")
|
|
|
|
|
|
#with open('/tmp/userlist.txt', 'w') as outfile:
|
|
# for user in userlist:
|
|
# print("{uid:5} {username:{width}} {realname}".format(
|
|
# width=userlen, **user),
|
|
# file=outfile)
|
|
|
|
def print_userlist_sorted_by_uid(userlist):
|
|
userlen = max(map(lambda u: len(u['username']), userlist))
|
|
for user in sorted(userlist, key=lambda e: e['uid']):
|
|
print("{uid:5} {username:{width}} {realname}".format(width=userlen, **user))
|
|
|
|
def main():
|
|
output_functions = {
|
|
'pprint': pprint,
|
|
'logger': print_to_file,
|
|
'username': print_userlist_sorted_by_username,
|
|
'uid': print_userlist_sorted_by_uid,
|
|
|
|
}
|
|
#outfunc = output_functions['username']
|
|
outfunc = output_functions[argv_value('-o', default='uid')]
|
|
lines = read_file("/etc/passwd")
|
|
userlist = build_userlist(lines)
|
|
|
|
outfunc(userlist)
|
|
|
|
|
|
utilities.hide_exception(main) |