1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
#!/usr/bin/python
import utmp
from UTMPCONST import *
import time, pwd, grp, os, string, sys, socket
from stat import *
from string import upper
def boguslist():
# get list of (pid, tty, commandname)
# bogus because we are just guessing by the output of ps
pscmd = "ps ac" # seems to be common
psoutput = os.popen(pscmd).readlines()
output = []
del psoutput[0]
for i in psoutput:
l = string.split(i)
pid, tty, command = int(l[0]), l[1], l[-1]
output.append( (pid, tty, command) )
return output
def proctable():
#make table of ttys and corresponding pids
global proctbl
proctbl = {}
for i in boguslist():
if not proctbl.has_key(i[1]):
proctbl[i[1]] = []
proctbl[i[1]].append( (i[0], i[2]) )
def header(now, boottime, ops, ver):
print "Consortium for global VMSfication"
print time.strftime("%A, %d-%b-%Y %H:%M,", now), "%s V%s" %(ops, ver)
print "Cluster contains 1 node, formed", \
time.strftime("%A, %d-%b-%Y %H:%M", time.localtime(boottime))
print
def getrealname(gec):
# get real name from gecos fiels
return string.split(gec,",",1)[0]
def userlist(u, now, node, user=""):
proctable()
u.setutent()
tnow = time.mktime(now)
header = 0
output = [] # list of output lines, without header
while 1:
b = u.getutent_dict()
if not b:
break
if b['ut_type'] == USER_PROCESS:
username = b['ut_user']
if user and b['ut_user']<>user:
continue
try:
pwnam = pwd.getpwnam(username)
except KeyError:
continue
realname = getrealname(pwnam[4])
username = upper(username) # yeah, right :-)
if 0:
try:
# this works on linux and freebsd
# unfortunately, even if it works,
# it does not work well because it always returns shell...
f = open("/proc/%i/cmdline" % b['ut_pid'], "r")
program = f.read()
program = os.path.split(program)[1]
except:
program = "?"
tty = b['ut_line']
program = max(proctbl[tty])[1]
program = string.split(program, ".")[0] # SunOS uses dots in names
shell = os.path.split(pwnam[6])[1]
if program == shell or (program[0]=="-" and program[1:]==shell):
program = "$"
t = time.localtime(b['ut_tv'][0])
then = time.mktime(t)
if tnow<then: # login in the future?
login = "FUTURE"
elif t[7] == now[7] and t[0] == now[0]: # today
login = time.strftime("%H:%M", t)
elif tnow-then<60*60*24*7: # this week
login = time.strftime("%a", t)
elif tnow-then<60*60*24*365.: # this year
login = time.strftime("%d-%b", t)
else: # way down in the past
login = time.strftime("%Y", t)
location = b['ut_host']
if location:
location = "Host: "+location
else:
location = b['ut_line']
#length sanitation
username = username[:12]
realname = realname[:22]
program = upper(program[:10])
login = login[:6]
node = node[:6]
location = location[:20]
if not header:
print "%-12s%-23s%-10s%-7s%-7s%-20s" % \
("Username","Real Name","Program","Login","Node","Location")
header = 1
output.append( "%-12s%-23s%-10s%-7s%-7s%-20s" %
(username,realname,program,login,node,location) )
output.sort()
for i in output:
print i
return output
def lastlogin(u, user):
lastlogin = 0
u.setutent()
while 1:
b = u.getutent_dict()
if not b:
break
if b['ut_type'] in (USER_PROCESS, DEAD_PROCESS) and \
b['ut_user'] == user and \
b['ut_tv'][0]>lastlogin:
lastlogin = b['ut_tv'][0]
u = utmp.UtmpRecord(WTMP_FILE)
while 1:
b = u.getutent_dict()
if not b:
break
if b['ut_type'] in (USER_PROCESS, DEAD_PROCESS) and \
b['ut_user'] == user and \
b['ut_tv'][0]>lastlogin:
lastlogin = b['ut_tv'][0]
u.endutent()
return lastlogin
def userplan(homedir):
try:
f = open(homedir+"/.plan", "r")
print "Plan:"
while 1:
l = f.readline()
if not l:
break
print string.rstrip(l)
except:
pass
def oneuser(u, user):
pwent = pwd.getpwnam(user)
print "Login name: %-28sIn real life: %s" % \
(upper(user), getrealname(pwent[4]))
print " Directory: %-37sUIC: [%s,%s] ([%i,%i])" % \
(pwent[5],
upper(grp.getgrgid(pwent[3])[0]), upper(user),
pwent[3], pwent[2])
l = lastlogin(u, user)
if not l:
print "Never logged in."
else:
print "Last login:", time.strftime("%A, %d-%b-%Y %H:%M", time.localtime(l))
print
userplan(pwent[5])
def guessbotttime(u):
# try to find out boot time
boottime = os.stat("/")[ST_CTIME] #fallback
u.setutent()
while 1:
b = u.getutent_dict()
if not b:
break
if b['ut_type'] == BOOT_TIME:
boottime = b['ut_tv'][0]
return boottime
if len(sys.argv) == 2 and "@" in sys.argv[1]: # remote
user, host = string.split(sys.argv[1], "@", 1)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
FINGER_PORT = 79
s.connect( (host, FINGER_PORT) )
s.send(user + '\r\n')
while 1:
buf = s.recv(1024)
if not buf: break
sys.stdout.write(buf)
sys.stdout.flush()
sys.exit(0)
un = os.uname()
ops = un[0]
node = un[1]
ver = un[2]
node = upper(node)
now = time.localtime(time.time())
a = utmp.UtmpRecord()
boottime = guessbotttime(a)
if len(sys.argv) == 1: # list of all local users
header(now, boottime, ops, ver)
r = userlist(a, now, node)
if not r:
print "No such processes."
else:
#first find out if user exists
user = sys.argv[1]
try:
pwd.getpwnam(user)
r = userlist(a, now, node, user)
if not r:
print upper(user), "isn't logged in."
print
oneuser(a, user)
except KeyError:
lou = [] # list of matching users
if len(user)>=3:
for i in pwd.getpwall():
rn = getrealname(i[4])
if string.count(upper(rn), upper(user)):
lou.append( (i[0], rn) )
if not lou:
print upper(user), "does not match any user of this system."
else:
print 'Users who have "%s" in their real names:' % upper(user)
for i in lou:
print "%-13s- %s" % (upper(i[0]), i[1])
a.endutent()
|