]> code.delx.au - bg-scripts/blobdiff - bin/pyfing
Initial import
[bg-scripts] / bin / pyfing
diff --git a/bin/pyfing b/bin/pyfing
new file mode 100755 (executable)
index 0000000..664a1df
--- /dev/null
@@ -0,0 +1,113 @@
+#!/usr/bin/env python
+# Copyright 2007 James Bunton <jamesbunton@fastmail.fm>
+# Modified by Greg Darke <gdar9540@usyd.edu.au> (2007)
+# Licensed for distribution under the GPL version 2, check COPYING for details
+# Check to see if people are online...
+
+import commands_async, pwd, socket, sys
+
+def matchNames(names):
+       def getFullName(gecos_entry):
+               return gecos_entry[: entry.pw_gecos.find(',')]
+       def parsePWDentry(entry):
+               return (entry.pw_name.lower(), getFullName(entry.pw_gecos).lower())
+
+       pwall = [parsePWDentry(entry) for entry in pwd.getpwall()]
+
+       out = []
+       for name in names:
+               found = False
+               name = name.lower()
+               for entry in pwall:
+                       username, realname = entry
+                       if username.find(name) >= 0 or realname.find(name) >= 0:
+                               found = True
+                               out.append((username, realname))
+               if not found:
+                       print "User '%s' not found in /etc/passwd, assuming you gave a username and you are not on the IT machines..." % name
+                       out.append((name, "[Not Found in /etc/passwd]"))
+       return out
+
+def getSmbStatus():
+       def halfparse(data):
+               return data.split('\n')[4:]
+
+       sshcmd = "ssh %s -q -o StrictHostKeyChecking=no -o BatchMode=true '/usr/samba/bin/smbstatus -b'"
+
+       cmd_async = commands_async.CommandRunner()
+       cmd_async.executeCommand(sshcmd % "ugitsmb.ug.it.usyd.edu.au")
+       cmd_async.executeCommand(sshcmd % "itsmb.ug.it.usyd.edu.au")
+       cmd_async.waitForCompletion()
+
+       data = []
+       for cmd, output in cmd_async.getOutputs().items():
+               data += halfparse(output)
+
+       out = []
+       for line in data:
+               line_split = line.strip().split()
+               if not line_split or len(line_split) != 5:
+                       continue
+
+               pid, username, group, _, ip = line_split
+               host = socket.gethostbyaddr(ip[1:-1])[0]
+               out.append((username, host))
+       return out
+
+def getLastStatus():
+       hosts = ["mono"]
+       hosts += ['congo%d' % i for i in range(1,5)]
+       hosts += ['nlp%d' % i for i in range(0,9)]
+       #hosts += ['userf%d' % i for i in range(1,6)]
+
+       sshcmd = "ssh %s -q -o StrictHostKeyChecking=no -o BatchMode=true 'last -a -t $(date +%%Y%%m%%d%%H%%M%%S)|grep \"still logged in\"'"
+###    sshcmd = "rsh -n %s 'last -a -t $(date +%%Y%%m%%d%%H%%M%%S)|grep \"still logged in\"'"
+
+       cmd_async = commands_async.CommandRunner()
+       for host in hosts:
+               cmd_async.executeCommand(sshcmd % host)
+
+       cmd_async.waitForCompletion()
+       data = "".join(output for cmd,output in cmd_async.getOutputs().items())
+       
+       out = []
+       for line in data.split('\n'):
+               if not line.strip():
+                       continue
+               try:
+                       chunk = line.strip().split()
+                       username = chunk[0]
+                       ip = chunk[-1]
+               except Exception, e:
+                       print "Error:", line, e
+                       return []
+               if ip == 'in': # From 'still logged in'
+                       host = "unknown"
+               else:
+                       try:
+                               host = socket.gethostbyaddr(ip)[0]
+                       except:
+                               host = "unknown"
+               out.append((username, host))
+       return out
+
+
+def printLocation((username, fullname), smbStatus):
+       # Since we only want to know if they are at a location, and now how many times they are at
+       # the location, we store it in a set
+       locationList = set(ip for username2, ip in smbStatus if username == username2)
+       if locationList:
+               print "Username %s:\n  Full name: '%s'\n  %s\n" % \
+                       (username, fullname, '\n  '.join('Location: %s' % ip for ip in locationList))
+
+def main():
+       names =  matchNames(sys.argv[1:])
+       smbStatus = getSmbStatus()
+       lastStatus = getLastStatus()
+       status = smbStatus + lastStatus
+       
+       for name in names:
+               printLocation(name, status)
+
+if __name__ == "__main__":
+       main()