while 1:
r = select([rfh], [], [], 5.0)[0]
if r:
- message = message + rfh.read(1)
+ try:
+ ch = rfh.read(1)
+ except socket.error:
+ ch = ''
+ if ch == '':
+ break
+ message = message + ch
else:
break
print "Received message: ", repr(message)
return message
-def write_expecting_echo(rfh, wfh, message):
+def write_and_get_response(rfh, wfh, message, expect_echo=True):
print "Writing message:", repr(message)
- while len(message) > 0:
- wfh.write(message[0])
- wfh.flush()
- if message[0] == '\n':
- echo = rfh.read(2)
- expected_echo = '\r\n'
- else:
- echo = rfh.read(1)
- expected_echo = message[0]
- if echo != expected_echo:
- print "Gah, expected echo when writing", repr(message[0]), "but got", repr(echo)
- message = message[1:]
- print " --> Sent and echoed."
+ wfh.write(message+'\r\n')
+ wfh.flush()
+ print " --> Sent"
+ response = read_for_a_bit(rfh)
+ if response.find(message) == -1 and expect_echo:
+ raise LATClientException("Talking to DEC server, expected to find original message in echo but didn't")
+ return response
class LATClient:
def __init__(self, service = None, node = None, port = None,
self.sock.shutdown(2)
self.sock.close()
mopw, mopr = popen4('/usr/sbin/moprc '+self.server_name)
- read_for_a_bit(mopr)
- mopw.write('\n')
- read_for_a_bit(mopr)
- mopw.write(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)
+ write_and_get_response(mopr, mopw, '')
+ r = write_and_get_response(mopr, mopw, self.connect_password, False)
+ if r.find('Enter username> ') == -1:
+ print "Expected username prompt, got ", repr(r)
+ raise LATClientException('failed to reboot server')
+ r = write_and_get_response(mopr, mopw, 'grim reaper')
+ if r.find('Local> ') == -1:
+ print "Expected DEC server prompt, got ", repr(r)
+ raise LATClientException('failed to reboot server')
+ r = write_and_get_response(mopr, mopw, 'set priv')
+ if r.find('Password> ') == -1:
+ print "Expected priv password prompt, got ", repr(r)
+ raise LATClientException('failed to reboot server')
+ r = write_and_get_response(mopr, mopw, self.priv_password, False)
+ if r.find('Local> ') == -1:
+ print "Expected DEC server prompt, got ", repr(r)
+ raise LATClientException('failed to reboot server')
+ r = write_and_get_response(mopr, mopw, 'init del 0')
+ if r.find('Target does not respond') == -1:
+ print "Expected DEC server to die, got ", repr(r)
+ raise LATClientException('failed to reboot server')
mopr.close()
mopw.close()
print