Jelajahi Sumber

add hostclean stuff

James Seward 13 tahun lalu
induk
melakukan
d2bdf327ce
2 mengubah file dengan 75 tambahan dan 0 penghapusan
  1. 43 0
      HostClean/hostclean.tcl
  2. 32 0
      HostClean/parse.py

+ 43 - 0
HostClean/hostclean.tcl

@@ -0,0 +1,43 @@
+
+set seen_hostnames [list]
+
+proc load_hostnames { filename } {
+	global seen_hostnames
+
+	set filehandle [open $filename "r"]
+	set line [gets $filehandle]
+
+	set count 0
+
+	while {![eof $filehandle]} {
+		set line [string trim $line]
+		lappend seen_hostnames $line
+		incr count
+		set line [gets $filehandle]
+	}
+
+	putlog "hostclean: loaded [llength $seen_hostnames] hostnames from cache"
+}
+
+
+proc hostclean { user } {
+  	global seen_hostnames
+	set hostnames [getuser $user HOSTS]
+	putlog "hostclean: found [llength $hostnames] hosts for $user"
+	if {[llength $hostnames] > 0} {
+		foreach $hostnames $host {
+			set regexp_hostname [string map { . \\. * .+ ? . } $host]
+			set seen 0
+			foreach $seen_hostnames $seen_host {
+			  if [regexp $regexp_hostname $seen_host] {
+				set seen 1
+				break
+			  }
+			}
+			if {!$seen} {
+			  # XXX delete this host
+			  putlog "$host is a candidate for deletion from user $user"
+			}
+		}
+	}
+}

+ 32 - 0
HostClean/parse.py

@@ -0,0 +1,32 @@
+#!/usr/local/bin/python
+
+import sys
+import re
+
+def parse_log(filename):
+    hosts = []
+
+    try:
+        fh = open(filename, 'r')
+    except:
+        print "Unable to open logfile"
+        sys.exit(1)
+
+    for line in fh:
+        matches = re.search('joins \((.+)\)', line)
+        if matches:
+            hostname = matches.group(1)
+            if not hostname in hosts:
+                hosts.append(hostname)
+
+    fh.close()
+    return hosts
+
+def main():
+    hosts = parse_log(sys.argv[1])
+    for host in hosts:
+        print host
+
+if __name__ == "__main__":
+    main()
+