From d7ce99409c36acde1d1322db8418dc13cb002842 Mon Sep 17 00:00:00 2001 From: Cameron Patrick Date: Sun, 1 Aug 2004 06:30:41 +0000 Subject: [PATCH] new 'improved' (i.e. probably not working) code for rebooting dec server --- sql-edition/servers/LATClient.py | 47 ++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/sql-edition/servers/LATClient.py b/sql-edition/servers/LATClient.py index 7c21d0d..fa2b297 100644 --- a/sql-edition/servers/LATClient.py +++ b/sql-edition/servers/LATClient.py @@ -1,6 +1,6 @@ from socket import * from select import select -from os import popen +from os import popen4 from time import sleep LATCP_SOCKET = '/var/run/latlogin' @@ -12,6 +12,28 @@ LATCP_CMD_ERRORMSG = 99 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, @@ -70,14 +92,21 @@ class LATClient: 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) -- 2.20.1