added
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#! /usr/bin/env python3
|
#! /usr/bin/env python3
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
import sys
|
||||||
|
import utilities
|
||||||
from argv import argv_value
|
from argv import argv_value
|
||||||
|
|
||||||
|
|
||||||
@@ -41,6 +42,7 @@ def build_userlist(lines) -> "List of user dicts":
|
|||||||
result.append(parse_passwd_lines(line))
|
result.append(parse_passwd_lines(line))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def print_userlist_sorted_by_username(userlist):
|
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()):
|
for user in sorted(userlist, key=lambda e: e['username'].lower()):
|
||||||
@@ -52,6 +54,30 @@ def print_userlist_sorted_by_username(userlist):
|
|||||||
user['home'],
|
user['home'],
|
||||||
user['shell']
|
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):
|
def print_userlist_sorted_by_uid(userlist):
|
||||||
userlen = max(map(lambda u: len(u['username']), userlist))
|
userlen = max(map(lambda u: len(u['username']), userlist))
|
||||||
@@ -61,6 +87,7 @@ def print_userlist_sorted_by_uid(userlist):
|
|||||||
def main():
|
def main():
|
||||||
output_functions = {
|
output_functions = {
|
||||||
'pprint': pprint,
|
'pprint': pprint,
|
||||||
|
'logger': print_to_file,
|
||||||
'username': print_userlist_sorted_by_username,
|
'username': print_userlist_sorted_by_username,
|
||||||
'uid': print_userlist_sorted_by_uid,
|
'uid': print_userlist_sorted_by_uid,
|
||||||
|
|
||||||
@@ -69,7 +96,8 @@ def main():
|
|||||||
outfunc = output_functions[argv_value('-o', default='uid')]
|
outfunc = output_functions[argv_value('-o', default='uid')]
|
||||||
lines = read_file("/etc/passwd")
|
lines = read_file("/etc/passwd")
|
||||||
userlist = build_userlist(lines)
|
userlist = build_userlist(lines)
|
||||||
|
|
||||||
outfunc(userlist)
|
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.
13
Mittwoch/argv2.py
Normal file
13
Mittwoch/argv2.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#! /usr/bin/env python3
|
||||||
|
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:
|
||||||
|
if idx+1 < len(sys.argv) and not sys.argv[idx+1].startswith('-'):
|
||||||
|
return sys.argv[idx+1]
|
||||||
|
idx += 1
|
||||||
|
|
||||||
|
return default
|
||||||
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)
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
def argv_value(param, default=None):
|
def argv_value(param, default=None):
|
||||||
|
"Ermittel dem Wert eines Parameter"
|
||||||
idx = 1
|
idx = 1
|
||||||
while idx < len(sys.argv):
|
while idx < len(sys.argv):
|
||||||
if sys.argv[idx] == param:
|
if sys.argv[idx] == param:
|
||||||
27
Module/utilities.py
Normal file
27
Module/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)")
|
||||||
14
Readme.md
14
Readme.md
@@ -33,3 +33,17 @@ Stringformat :
|
|||||||
{0:->13} 0x{1:0>4b}".format("abc", 23) -> Beispiel oder "{0:->{width}} 0x{1:0>4b}".format("abc", 23, width=13)
|
{0:->13} 0x{1:0>4b}".format("abc", 23) -> Beispiel oder "{0:->{width}} 0x{1:0>4b}".format("abc", 23, width=13)
|
||||||
|
|
||||||
pydoc3 zeigt die Helps an Beispiel pydoc3 sys
|
pydoc3 zeigt die Helps an Beispiel pydoc3 sys
|
||||||
|
|
||||||
|
IN $PYTHONPATH sucht Python nach Modulen
|
||||||
|
|
||||||
|
Die wichtigen vars von Funktion
|
||||||
|
|
||||||
|
__name__
|
||||||
|
__file__
|
||||||
|
|
||||||
|
z.B print(str.__name__)
|
||||||
|
|
||||||
|
Alle Exceptions sind im Netz https://docs.python.org/3/library/exceptions.html
|
||||||
|
|
||||||
|
mit join mach ich die Liste wieder zusammen
|
||||||
|
.join(format_tb(e.__traceback__)
|
||||||
|
|||||||
Reference in New Issue
Block a user