about summary refs log tree commit diff stats
path: root/bin
diff options
context:
space:
mode:
authorahriman <ahriman@falte.red>2019-12-16 15:07:15 -0500
committerahriman <ahriman@falte.red>2019-12-16 15:07:15 -0500
commitcaa396ca806f7b7e34676b9ecd08813571b2b5a3 (patch)
tree6a3c28758a30e5d24f8871e686ec22ce5899a91d /bin
parent8e4d9c28b4f9d0d3d55eae7d610929832fa37100 (diff)
downloadadmin-caa396ca806f7b7e34676b9ecd08813571b2b5a3.tar.gz
added kneezle's new user notification script
Diffstat (limited to 'bin')
-rwxr-xr-xbin/newuseralert.py.template97
1 files changed, 97 insertions, 0 deletions
diff --git a/bin/newuseralert.py.template b/bin/newuseralert.py.template
new file mode 100755
index 0000000..5e05422
--- /dev/null
+++ b/bin/newuseralert.py.template
@@ -0,0 +1,97 @@
+#!/usr/local/bin/python3
+import os
+import argparse
+import http.client
+
+aboutme = """
+# New User Monitor
+# Created for tilde.institute by Kneezle
+# BSD Rocks!
+
+# assuming we're running once a day.
+# this replaces the 1.0 version that ran in a constant loop
+"""
+
+#put the tokens here for each admin to be alerted
+gotifytokens = []
+gotifytokens.append("your_gotify_token")
+
+defaulttitle = "Alert: New Users"
+
+defaultnewuserpath = "/path/to/new/user/dump/file"
+
+runcmdtemplate = """curl -X POST "https://gotify/message?token={}" -F "title=Alert: New Users" -F "message={}" -F "priority=5" """
+
+ap = argparse.ArgumentParser()
+ap.add_argument('--newuserpath', default=defaultnewuserpath,
+                    help='sets the path for the new user txt file')
+
+ap.add_argument('--title', default=defaulttitle,
+                    help='sets the title')
+
+ap.add_argument('--usecurl', default=False,
+                    help='use curl or build in sockets')
+
+ap.add_argument("-a", "--about", dest='about', action='store_true', help="about me")
+
+args = vars(ap.parse_args())
+
+if args['about']:
+    print(aboutme)
+    exit()
+
+title = args['title']
+newuserpath = args['newuserpath']
+usecurl = args['usecurl']
+
+boilerplate = """New pending user found!
+
+Username: {}
+Email: {}
+SSH Key {}
+
+"""
+push_notification = ""
+
+num_linesp = 0
+
+num_lines = sum(1 for line in open(args['newuserpath']))
+print(" I found: " + str(num_lines))
+
+numnonblanklines = 0
+if num_lines > 0:
+    with open(args['newuserpath']) as fp:
+        line = fp.readline()
+        cnt = 1
+        while line:
+            if line.strip() != "": #not a blank line
+                numnonblanklines = numnonblanklines + 1
+                print("Line {}: {}".format(cnt, line.strip()))
+                x = line.split(" ")
+                sshkey = ""
+                itemno = 0
+                for item in x:
+                    if itemno > 1:
+                        sshkey = sshkey + " " + item
+                    itemno = itemno + 1
+                sshkey = sshkey.replace('"', "")
+
+                push_notification = push_notification + boilerplate.format(x[0], x[1], sshkey)
+            cnt = cnt + 1
+            line = fp.readline()
+
+    if numnonblanklines > 0:
+        for key in gotifytokens:
+            if not usecurl:
+                conn = http.client.HTTPSConnection("gotify.tildeverse.org")
+                payload = """title={}&message={}&priority=5""".format(title, push_notification)
+                headers = {'content-type': "application/x-www-form-urlencoded"}
+                conn.request("POST", "/message?token={}".format(key), payload, headers)
+                res = conn.getresponse()
+                data = res.read()
+                print(data.decode("utf-8"))
+            else:
+                torun = runcmdtemplate.format(key, push_notification)
+                os.system(torun)
+    else:
+        print("only blank lines")