from socket import *
from select import select
-from os import popen
+from os import popen4
from time import sleep
LATCP_SOCKET = '/var/run/latlogin'
class LATClientException(Exception): pass
+def read_for_a_bit(rfh):
+ message = ''
+ while 1:
+ r = select([rfh], [], [], 5.0)[0]
+ if r:
+ message = message + rfh.read(1)
+ else:
+ break
+ print "Received message: ", message
+ return message
+
+def write_expecting_echo(rfh, wfh, message):
+ print "Writing message:",message
+ while len(message) > 0:
+ wfh.write(message[0])
+ wfh.flush()
+ echo = rfh.read(1)
+ if echo != message[0]:
+ print "Gah, expected echo when writing", message[0]
+ message = message[1:]
+ print " --> Sent and echoed."
+
class LATClient:
def __init__(self, service = None, node = None, port = None,
localport = None, password = None, is_queued = False,
def reboot_server(self):
self.sock.shutdown(2)
self.sock.close()
- mop = popen('/usr/sbin/moprc '+self.server_name, 'w')
- mop.write('\n')
- mop.write(self.connect_password+'\n')
- mop.write('grim reaper\n')
- mop.write('set priv\n')
- mop.write(self.priv_password+'\n')
- mop.write('init del 0\n')
- mop.close()
+ mopw, mopr = popen4('/usr/sbin/moprc '+self.server_name)
+ write_expecting_echo(mopr, mopw, '\n')
+ read_for_a_bit(mopr)
+ write_expecting_echo(mopr, mopw, self.connect_password+'\n')
+ read_for_a_bit(mopr)
+ write_expecting_echo(mopr, mopw, 'grim reaper\n')
+ read_for_a_bit(mopr)
+ write_expecting_echo(mopr, mopw, 'set priv\n')
+ read_for_a_bit(mopr)
+ write_expecting_echo(mopr, mopw, self.priv_password+'\n')
+ read_for_a_bit(mopr)
+ write_expecting_echo(mopr, mopw, 'init del 0\n')
+ read_for_a_bit(mopr)
+ mopr.close()
+ mopw.close()
print
print "Waiting 10 seconds for DEC server to come back to life..."
sleep(10)