]>
code.delx.au - bg-scripts/blob - bin/pyfing
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...
7 import commands_async
, pwd
, socket
, sys
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())
15 pwall
= [parsePWDentry(entry
) for entry
in pwd
.getpwall()]
22 username
, realname
= entry
23 if username
.find(name
) >= 0 or realname
.find(name
) >= 0:
25 out
.append((username
, realname
))
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]"))
33 return data
.split('\n')[4:]
35 sshcmd
= "ssh %s -q -o StrictHostKeyChecking=no -o BatchMode=true '/usr/samba/bin/smbstatus -b'"
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()
43 for cmd
, output
in cmd_async
.getOutputs().items():
44 data
+= halfparse(output
)
48 line_split
= line
.strip().split()
49 if not line_split
or len(line_split
) != 5:
52 pid
, username
, group
, _
, ip
= line_split
53 host
= socket
.gethostbyaddr(ip
[1:-1])[0]
54 out
.append((username
, host
))
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)]
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\"'"
66 cmd_async
= commands_async
.CommandRunner()
68 cmd_async
.executeCommand(sshcmd
% host
)
70 cmd_async
.waitForCompletion()
71 data
= "".join(output
for cmd
,output
in cmd_async
.getOutputs().items())
74 for line
in data
.split('\n'):
78 chunk
= line
.strip().split()
82 print "Error:", line
, e
84 if ip
== 'in': # From 'still logged in'
88 host
= socket
.gethostbyaddr(ip
)[0]
91 out
.append((username
, host
))
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
)
100 print "Username %s:\n Full name: '%s'\n %s\n" % \
101 (username
, fullname
, '\n '.join('Location: %s' % ip
for ip
in locationList
))
104 names
= matchNames(sys
.argv
[1:])
105 smbStatus
= getSmbStatus()
106 lastStatus
= getLastStatus()
107 status
= smbStatus
+ lastStatus
110 printLocation(name
, status
)
112 if __name__
== "__main__":