about summary refs log tree commit diff stats
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
parent8e4d9c28b4f9d0d3d55eae7d610929832fa37100 (diff)
downloadadmin-caa396ca806f7b7e34676b9ecd08813571b2b5a3.tar.gz
added kneezle's new user notification script
-rw-r--r--.gitignore1
-rw-r--r--README.md3
-rwxr-xr-xbin/newuseralert.py.template97
3 files changed, 100 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index b3cdfcf..790d2f4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@ misc/toot.json
 fuzzies.log
 users-added.log
 newusers.dat
+newuseralert.py
diff --git a/README.md b/README.md
index d909d33..39d0e0b 100644
--- a/README.md
+++ b/README.md
@@ -8,8 +8,9 @@ and other miscellaneous tasks, such as various monitoring scripts.
 The scripts are a mixture of both `Bash` and `Python`  
 
 Everything was written by me, except for the following, which were *not* written by me:
-  * `bin/showwhoison` (written by my co-administrator who joined the project in March 2019)
+  * `bin/showwhoison` (written by Kneezle, the co-administrator who joined the project in March 2019)
   * `bin/newmail.sh` (same as above)
+  *  bin/newuseralert.py (same as above)
   * `bin/toot.py` (written by Ben Harris of [tilde.team](https://tilde.team))
 
 
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")