-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
30 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,35 @@ | ||
#!/usr/bin/env python3 | ||
# https://github.com/ifor2u/specbase | ||
|
||
import json | ||
import sys | ||
|
||
file = open("/etc/group", "r") | ||
def parse_group_file(file_path): | ||
""" | ||
Parse the Unix group file and return data as a dictionary. | ||
:param file_path: Path to the group file. | ||
:return: Dictionary containing group data. | ||
""" | ||
list_groups = {} | ||
try: | ||
with open(file_path, "r+") as file: | ||
for line in file: | ||
tokens = line.rstrip().split(":") | ||
group = {"gid": tokens[2], "group-list": []} | ||
if len(tokens) == 4 and tokens[3]: | ||
group["group-list"] = tokens[3].split(",") | ||
list_groups[tokens[0]] = group | ||
except IOError as e: | ||
print(f"Failed to open {file_path}: {e}", file=sys.stderr) | ||
sys.exit(1) | ||
except IndexError: | ||
print(f"Unexpected file format in {file_path}", file=sys.stderr) | ||
sys.exit(1) | ||
return list_groups | ||
|
||
list_groups = {} | ||
while True: | ||
line = file.readline() | ||
if not line: | ||
break | ||
tokens = line.rstrip().split(":") | ||
group = {"gid": tokens[2], "group-list": []} | ||
if len(tokens) == 4: | ||
for token in tokens[3].split(","): | ||
if token == "": | ||
break | ||
group["group-list"].append(token) | ||
list_groups[tokens[0]] = group | ||
def main(): | ||
group_file_path = "/etc/group" | ||
groups_data = parse_group_file(group_file_path) | ||
print(json.dumps(groups_data)) | ||
|
||
print(json.dumps(list_groups)) | ||
if __name__ == "__main__": | ||
main() |