1992-12-20 14:14:23 -04:00
|
|
|
# Remote nusers client interface
|
|
|
|
|
|
|
|
import rpc
|
1992-12-21 10:32:55 -04:00
|
|
|
from rpc import Packer, Unpacker, UDPClient, BroadcastUDPClient
|
1992-12-20 14:14:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
class RnusersPacker(Packer):
|
|
|
|
def pack_utmp(self, ui):
|
|
|
|
ut_line, ut_name, ut_host, ut_time = utmp
|
|
|
|
self.pack_string(ut_line)
|
|
|
|
self.pack_string(ut_name)
|
|
|
|
self.pack_string(ut_host)
|
|
|
|
self.pack_int(ut_time)
|
|
|
|
def pack_utmpidle(self, ui):
|
|
|
|
ui_itmp, ui_idle = ui
|
|
|
|
self.pack_utmp(ui_utmp)
|
|
|
|
self.pack_uint(ui_idle)
|
|
|
|
def pack_utmpidlearr(self, list):
|
|
|
|
self.pack_array(list, self.pack_itmpidle)
|
|
|
|
|
|
|
|
|
|
|
|
class RnusersUnpacker(Unpacker):
|
|
|
|
def unpack_utmp(self):
|
|
|
|
ut_line = self.unpack_string()
|
|
|
|
ut_name = self.unpack_string()
|
|
|
|
ut_host = self.unpack_string()
|
|
|
|
ut_time = self.unpack_int()
|
|
|
|
return ut_line, ut_name, ut_host, ut_time
|
|
|
|
def unpack_utmpidle(self):
|
|
|
|
ui_utmp = self.unpack_utmp()
|
|
|
|
ui_idle = self.unpack_uint()
|
|
|
|
return ui_utmp, ui_idle
|
|
|
|
def unpack_utmpidlearr(self):
|
|
|
|
return self.unpack_array(self.unpack_utmpidle)
|
|
|
|
|
|
|
|
|
1992-12-21 10:32:55 -04:00
|
|
|
class PartialRnusersClient:
|
1992-12-20 14:14:23 -04:00
|
|
|
|
|
|
|
def addpackers(self):
|
1993-12-17 10:32:26 -04:00
|
|
|
self.packer = RnusersPacker()
|
|
|
|
self.unpacker = RnusersUnpacker('')
|
1992-12-20 14:14:23 -04:00
|
|
|
|
|
|
|
def Num(self):
|
1992-12-21 10:32:55 -04:00
|
|
|
return self.make_call(1, None, None, self.unpacker.unpack_int)
|
1992-12-20 14:14:23 -04:00
|
|
|
|
|
|
|
def Names(self):
|
1992-12-21 10:32:55 -04:00
|
|
|
return self.make_call(2, None, \
|
|
|
|
None, self.unpacker.unpack_utmpidlearr)
|
1992-12-20 14:14:23 -04:00
|
|
|
|
|
|
|
def Allnames(self):
|
1992-12-21 10:32:55 -04:00
|
|
|
return self.make_call(3, None, \
|
|
|
|
None, self.unpacker.unpack_utmpidlearr)
|
|
|
|
|
|
|
|
|
|
|
|
class RnusersClient(PartialRnusersClient, UDPClient):
|
|
|
|
|
1993-12-17 10:32:26 -04:00
|
|
|
def __init__(self, host):
|
|
|
|
UDPClient.__init__(self, host, 100002, 2)
|
1992-12-21 10:32:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
class BroadcastRnusersClient(PartialRnusersClient, BroadcastUDPClient):
|
|
|
|
|
1993-12-17 10:32:26 -04:00
|
|
|
def __init__(self, bcastaddr):
|
|
|
|
BroadcastUDPClient.__init__(self, bcastaddr, 100002, 2)
|
1992-12-20 14:14:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test():
|
|
|
|
import sys
|
1992-12-21 10:32:55 -04:00
|
|
|
if not sys.argv[1:]:
|
|
|
|
testbcast()
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
host = sys.argv[1]
|
1993-12-17 10:32:26 -04:00
|
|
|
c = RnusersClient(host)
|
1992-12-20 14:14:23 -04:00
|
|
|
list = c.Names()
|
|
|
|
for (line, name, host, time), idle in list:
|
|
|
|
line = strip0(line)
|
|
|
|
name = strip0(name)
|
|
|
|
host = strip0(host)
|
1992-12-21 10:32:55 -04:00
|
|
|
print `name`, `host`, `line`, time, idle
|
|
|
|
|
|
|
|
def testbcast():
|
1993-12-17 10:32:26 -04:00
|
|
|
c = BroadcastRnusersClient('<broadcast>')
|
1992-12-21 10:32:55 -04:00
|
|
|
def listit(list, fromaddr):
|
|
|
|
host, port = fromaddr
|
|
|
|
print host + '\t:',
|
|
|
|
for (line, name, host, time), idle in list:
|
|
|
|
print strip0(name),
|
|
|
|
print
|
|
|
|
c.set_reply_handler(listit)
|
|
|
|
all = c.Names()
|
|
|
|
print 'Total Count:', len(all)
|
|
|
|
|
|
|
|
def strip0(s):
|
|
|
|
while s and s[-1] == '\0': s = s[:-1]
|
|
|
|
return s
|
|
|
|
|
|
|
|
test()
|