commit message from python script
This commit is contained in:
66
Mittwoch/User_modify.py
Normal file → Executable file
66
Mittwoch/User_modify.py
Normal file → Executable file
@@ -2,40 +2,42 @@
|
||||
from pprint import pprint
|
||||
import sys
|
||||
import utilities
|
||||
from argv import argv_value
|
||||
from argv2 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
|
||||
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():
|
||||
# for line in f.readlines():
|
||||
# lines.append(line.rstrip())
|
||||
#f.close()
|
||||
# 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],
|
||||
"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:
|
||||
@@ -44,24 +46,26 @@ def build_userlist(lines) -> "List of user dicts":
|
||||
|
||||
|
||||
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
|
||||
# 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['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)
|
||||
width=userlen, **user),
|
||||
file=outfile)
|
||||
except ImportError:
|
||||
pass
|
||||
# Wird vom Mdule utilities gesteuert
|
||||
@@ -72,17 +76,19 @@ def print_to_file(userlist):
|
||||
finally:
|
||||
print("Wird immer durchlaufen, unabhaengig von ausgeloesten Exceptions")
|
||||
|
||||
|
||||
#with open('/tmp/userlist.txt', 'w') as outfile:
|
||||
# with open('/tmp/userlist.txt', 'w') as outfile:
|
||||
# for user in userlist:
|
||||
# print("{uid:5} {username:{width}} {realname}".format(
|
||||
# width=userlen, **user),
|
||||
# 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))
|
||||
print("{uid:5} {username:{width}} {realname}".format(
|
||||
width=userlen, **user))
|
||||
|
||||
|
||||
def main():
|
||||
output_functions = {
|
||||
@@ -90,14 +96,14 @@ def main():
|
||||
'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')]
|
||||
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)
|
||||
utilities.hide_exception(main)
|
||||
|
||||
0
Mittwoch/__pycache__/argv.cpython-39.pyc
Normal file → Executable file
0
Mittwoch/__pycache__/argv.cpython-39.pyc
Normal file → Executable file
BIN
Mittwoch/__pycache__/argv2.cpython-310.pyc
Executable file
BIN
Mittwoch/__pycache__/argv2.cpython-310.pyc
Executable file
Binary file not shown.
BIN
Mittwoch/__pycache__/utilities.cpython-310.pyc
Executable file
BIN
Mittwoch/__pycache__/utilities.cpython-310.pyc
Executable file
Binary file not shown.
0
Mittwoch/__pycache__/utilities.cpython-39.pyc
Normal file → Executable file
0
Mittwoch/__pycache__/utilities.cpython-39.pyc
Normal file → Executable file
0
Mittwoch/argv2.py
Normal file → Executable file
0
Mittwoch/argv2.py
Normal file → Executable file
0
Mittwoch/command.py
Normal file → Executable file
0
Mittwoch/command.py
Normal file → Executable file
0
Mittwoch/find_ip.py
Normal file → Executable file
0
Mittwoch/find_ip.py
Normal file → Executable file
0
Mittwoch/import.py
Normal file → Executable file
0
Mittwoch/import.py
Normal file → Executable file
0
Mittwoch/module_path.py
Normal file → Executable file
0
Mittwoch/module_path.py
Normal file → Executable file
2
Mittwoch/moduletest.py
Normal file → Executable file
2
Mittwoch/moduletest.py
Normal file → Executable file
@@ -1,6 +1,6 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
#import argv
|
||||
from argv import argv_value
|
||||
from argv2 import argv_value
|
||||
|
||||
print("Wert von -o :", argv_value('-o'))
|
||||
|
||||
0
Mittwoch/utilities.py
Normal file → Executable file
0
Mittwoch/utilities.py
Normal file → Executable file
0
Mittwoch/write_file.py
Normal file → Executable file
0
Mittwoch/write_file.py
Normal file → Executable file
Reference in New Issue
Block a user