43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
|
|
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 build_userlist(lines) -> "List of user dicts":
|
|
result = []
|
|
for line in lines:
|
|
result.append(parse_passwd_lines(line))
|
|
return result
|
|
|
|
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
|
|
|
|
|
|
test = read_file("/etc/passwd")
|
|
userlist = [i['username'] for i in build_userlist(test)]
|
|
print(userlist) |