new 'improved' (i.e. probably not working) code for rebooting dec server
authorCameron Patrick <cameron@ucc.gu.uwa.edu.au>
Sun, 1 Aug 2004 06:30:41 +0000 (06:30 +0000)
committerCameron Patrick <cameron@ucc.gu.uwa.edu.au>
Sun, 1 Aug 2004 06:30:41 +0000 (06:30 +0000)
sql-edition/servers/LATClient.py

index 7c21d0d..fa2b297 100644 (file)
@@ -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)

UCC git Repository :: git.ucc.asn.au