exec-notify/execNotify

41 lines
905 B
Plaintext
Raw Normal View History

2020-12-06 13:18:32 +01:00
#!/usr/bin/env python3
from sys import argv
import socket
2020-12-06 16:40:03 +01:00
from typing import List
2020-12-06 13:18:32 +01:00
2020-12-06 16:40:03 +01:00
from execNotifyDir import exec, config, mail
2020-12-06 13:18:32 +01:00
def main():
2020-12-06 16:40:03 +01:00
if len(argv) >= 2:
2020-12-26 15:15:54 +01:00
if not executeCommand(argv[1:]):
exit(1)
exit(0)
2020-12-06 13:18:32 +01:00
2020-12-06 16:40:03 +01:00
def executeCommand(command: List) -> bool:
keys = ['command', 'status', 'stderr', 'stdout']
code, stdout, stderr = exec.execute(command)
values = [str(command), str(code), stderr, stdout]
BODY = ''
for key, value in zip(keys, values):
BODY += '=== {} ===\n{}\n'.format(key, value)
print(BODY)
2020-12-06 13:18:32 +01:00
2020-12-06 16:40:03 +01:00
if code != 0:
hostname = socket.gethostname()
2020-12-06 13:18:32 +01:00
2020-12-06 16:40:03 +01:00
SUBJECT = '{} | {}'.format(hostname, str(command))
mail.sendMailOrWriteToFile(SUBJECT=SUBJECT, BODY=BODY)
2020-12-06 13:18:32 +01:00
return False
2020-12-06 16:40:03 +01:00
else:
return True
2020-12-06 13:18:32 +01:00
if __name__ == '__main__':
main()
2020-12-26 15:15:54 +01:00
if mail.prevMailNotSent():
mail.informAboutOldMail()