]> code.delx.au - bg-scripts/blob - bin/pyfing
RandomBG: Make Listener non-writeable
[bg-scripts] / bin / pyfing
1 #!/usr/bin/env python
2 # Copyright 2007 James Bunton <jamesbunton@fastmail.fm>
3 # Modified by Greg Darke <gdar9540@usyd.edu.au> (2007)
4 # Licensed for distribution under the GPL version 2, check COPYING for details
5 # Check to see if people are online...
6
7 import commands_async, pwd, socket, sys
8
9 def matchNames(names):
10 def getFullName(gecos_entry):
11 return gecos_entry[: entry.pw_gecos.find(',')]
12 def parsePWDentry(entry):
13 return (entry.pw_name.lower(), getFullName(entry.pw_gecos).lower())
14
15 pwall = [parsePWDentry(entry) for entry in pwd.getpwall()]
16
17 out = []
18 for name in names:
19 found = False
20 name = name.lower()
21 for entry in pwall:
22 username, realname = entry
23 if username.find(name) >= 0 or realname.find(name) >= 0:
24 found = True
25 out.append((username, realname))
26 if not found:
27 print "User '%s' not found in /etc/passwd, assuming you gave a username and you are not on the IT machines..." % name
28 out.append((name, "[Not Found in /etc/passwd]"))
29 return out
30
31 def getSmbStatus():
32 def halfparse(data):
33 return data.split('\n')[4:]
34
35 sshcmd = "ssh %s -q -o StrictHostKeyChecking=no -o BatchMode=true '/usr/samba/bin/smbstatus -b'"
36
37 cmd_async = commands_async.CommandRunner()
38 cmd_async.executeCommand(sshcmd % "ugitsmb.ug.it.usyd.edu.au")
39 cmd_async.executeCommand(sshcmd % "itsmb.ug.it.usyd.edu.au")
40 cmd_async.waitForCompletion()
41
42 data = []
43 for cmd, output in cmd_async.getOutputs().items():
44 data += halfparse(output)
45
46 out = []
47 for line in data:
48 line_split = line.strip().split()
49 if not line_split or len(line_split) != 5:
50 continue
51
52 pid, username, group, _, ip = line_split
53 host = socket.gethostbyaddr(ip[1:-1])[0]
54 out.append((username, host))
55 return out
56
57 def getLastStatus():
58 hosts = ["mono"]
59 hosts += ['congo%d' % i for i in range(1,5)]
60 hosts += ['nlp%d' % i for i in range(0,9)]
61 #hosts += ['userf%d' % i for i in range(1,6)]
62
63 sshcmd = "ssh %s -q -o StrictHostKeyChecking=no -o BatchMode=true 'last -a -t $(date +%%Y%%m%%d%%H%%M%%S)|grep \"still logged in\"'"
64 ### sshcmd = "rsh -n %s 'last -a -t $(date +%%Y%%m%%d%%H%%M%%S)|grep \"still logged in\"'"
65
66 cmd_async = commands_async.CommandRunner()
67 for host in hosts:
68 cmd_async.executeCommand(sshcmd % host)
69
70 cmd_async.waitForCompletion()
71 data = "".join(output for cmd,output in cmd_async.getOutputs().items())
72
73 out = []
74 for line in data.split('\n'):
75 if not line.strip():
76 continue
77 try:
78 chunk = line.strip().split()
79 username = chunk[0]
80 ip = chunk[-1]
81 except Exception, e:
82 print "Error:", line, e
83 return []
84 if ip == 'in': # From 'still logged in'
85 host = "unknown"
86 else:
87 try:
88 host = socket.gethostbyaddr(ip)[0]
89 except:
90 host = "unknown"
91 out.append((username, host))
92 return out
93
94
95 def printLocation((username, fullname), smbStatus):
96 # Since we only want to know if they are at a location, and now how many times they are at
97 # the location, we store it in a set
98 locationList = set(ip for username2, ip in smbStatus if username == username2)
99 if locationList:
100 print "Username %s:\n Full name: '%s'\n %s\n" % \
101 (username, fullname, '\n '.join('Location: %s' % ip for ip in locationList))
102
103 def main():
104 names = matchNames(sys.argv[1:])
105 smbStatus = getSmbStatus()
106 lastStatus = getLastStatus()
107 status = smbStatus + lastStatus
108
109 for name in names:
110 printLocation(name, status)
111
112 if __name__ == "__main__":
113 main()