added
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#! /usr/bin/env python3
|
||||
from pprint import pprint
|
||||
|
||||
import sys
|
||||
import utilities
|
||||
from argv import argv_value
|
||||
|
||||
|
||||
@@ -41,6 +42,7 @@ def build_userlist(lines) -> "List of user dicts":
|
||||
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()):
|
||||
@@ -52,7 +54,31 @@ def print_userlist_sorted_by_username(userlist):
|
||||
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']):
|
||||
@@ -61,6 +87,7 @@ def print_userlist_sorted_by_uid(userlist):
|
||||
def main():
|
||||
output_functions = {
|
||||
'pprint': pprint,
|
||||
'logger': print_to_file,
|
||||
'username': print_userlist_sorted_by_username,
|
||||
'uid': print_userlist_sorted_by_uid,
|
||||
|
||||
@@ -69,7 +96,8 @@ def main():
|
||||
outfunc = output_functions[argv_value('-o', default='uid')]
|
||||
lines = read_file("/etc/passwd")
|
||||
userlist = build_userlist(lines)
|
||||
|
||||
outfunc(userlist)
|
||||
|
||||
|
||||
main()
|
||||
utilities.hide_exception(main)
|
||||
BIN
Mittwoch/__pycache__/utilities.cpython-39.pyc
Normal file
BIN
Mittwoch/__pycache__/utilities.cpython-39.pyc
Normal file
Binary file not shown.
@@ -2,6 +2,7 @@
|
||||
import sys
|
||||
|
||||
def argv_value(param, default=None):
|
||||
"Ermittel dem Wert eines Parameter"
|
||||
idx = 1
|
||||
while idx < len(sys.argv):
|
||||
if sys.argv[idx] == param:
|
||||
30
Mittwoch/find_ip.py
Normal file
30
Mittwoch/find_ip.py
Normal file
@@ -0,0 +1,30 @@
|
||||
#! /usr/bin/env python3
|
||||
import utilities, os, sys, re
|
||||
|
||||
def read_from_command(command: str) -> list:
|
||||
try:
|
||||
with os.popen(command, 'r') as pipe:
|
||||
return list(map(str.rstrip, pipe.readlines()))
|
||||
#return [l.rstrip for l in pipe.readlines()]
|
||||
print(com_list)
|
||||
except OSError:
|
||||
print("Kann Program nicht auslesen", file=sys.stderr)
|
||||
|
||||
def extract_ip(lines: list) -> list:
|
||||
regex = r'.*inet\s(([0-9]{1,3}\.){3}[0-9]{1,3})'
|
||||
addresse= []
|
||||
for line in lines:
|
||||
match = re.match(regex,line)
|
||||
if match:
|
||||
#print("Gesamte IP :", match.group(1))
|
||||
#print("3. Oktett :", match.group(2))
|
||||
addresse.append(match.group(1))
|
||||
#print(line)
|
||||
return addresse
|
||||
def main():
|
||||
lines = read_from_command("ip address show")
|
||||
local_ip = extract_ip(lines)
|
||||
print("Aktuelle Adressen : ", local_ip)
|
||||
|
||||
|
||||
utilities.hide_exception(main)
|
||||
16
Mittwoch/module_path.py
Normal file
16
Mittwoch/module_path.py
Normal file
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
|
||||
#Umgebungsvaribale PYTHONPATH wird verwendet zum auslesen
|
||||
sys.path.insert(0, '/opt/project/python3')
|
||||
print("Verzeichnisse mit Modulen")
|
||||
newlist = []
|
||||
newlist2 = list(map(str, sys.path))
|
||||
for path in sys.path:
|
||||
newlist.append(path)
|
||||
print("-", path)
|
||||
|
||||
print(newlist)
|
||||
print("-------------------------------------------------")
|
||||
print(newlist2)
|
||||
6
Mittwoch/moduletest.py
Normal file
6
Mittwoch/moduletest.py
Normal file
@@ -0,0 +1,6 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
#import argv
|
||||
from argv import argv_value
|
||||
|
||||
print("Wert von -o :", argv_value('-o'))
|
||||
27
Mittwoch/utilities.py
Normal file
27
Mittwoch/utilities.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""
|
||||
Miscellaneous utilities
|
||||
"""
|
||||
import sys
|
||||
from traceback import format_tb
|
||||
|
||||
def hide_exception(func):
|
||||
try:
|
||||
return func()
|
||||
except Exception as e:
|
||||
name = type(e).__name__
|
||||
text = str(e)
|
||||
traceback = ''.join(format_tb(e.__traceback__))
|
||||
|
||||
if '-V' in sys.argv:
|
||||
print("Entwickler_Mode: wip")
|
||||
#Für Faule Leute einfacher weiterleiten mit raise
|
||||
#raise
|
||||
print("{name}: {text}".format(name=name, text=text), file=sys.stderr)
|
||||
print("Traceback:\n", traceback, sep='', file=sys.stderr)
|
||||
else:
|
||||
print("Interner Fehler,", e)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("from uitlities import hide_exception")
|
||||
print("hide_exception(main)")
|
||||
13
Mittwoch/write_file.py
Normal file
13
Mittwoch/write_file.py
Normal file
@@ -0,0 +1,13 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
|
||||
def write_into_files(path, lines):
|
||||
with open(path, 'w') as outfile:
|
||||
for line in lines:
|
||||
print(line, file=outfile)
|
||||
pass
|
||||
|
||||
|
||||
lines = ["Das ist ein zeichen", "Das ist ein zweites zeichen0000"]
|
||||
lines.append("Next line")
|
||||
write_into_files("/tmp/test.txt", lines)
|
||||
Reference in New Issue
Block a user