from select import select
from os import popen4
from time import sleep
+import logging
LATCP_SOCKET = '/var/run/latlogin'
message = message + ch
else:
break
- print "Received message: ", repr(message)
+ logging.debug("Received message: ", repr(message))
return message
def write_and_get_response(rfh, wfh, message, expect_echo=True):
- print "Writing message:", repr(message)
+ logging.debug("Writing message:", repr(message))
wfh.write(message+'\r\n')
wfh.flush()
- print " --> Sent"
+ logging.debug(" --> 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")
if r:
l = self.rfh.readline()
if l.find('Service in use') >= 0:
- print "Service in use, apparently: restarting DEC server"
+ logging.warning("Service in use, apparently: restarting DEC server")
self.reboot_server()
def __del__(self):
def reboot_server(self):
self.sock.shutdown(2)
self.sock.close()
+
+ logging.info('Logging into DEC server')
mopw, mopr = popen4('/usr/sbin/moprc '+self.server_name)
write_and_get_response(mopr, mopw, '')
+
+ logging.info('Sending password')
r = write_and_get_response(mopr, mopw, self.connect_password, False)
if r.find('Enter username> ') == -1:
- print "Expected username prompt, got ", repr(r)
+ logging.warning("Expected username prompt, got " + repr(r))
raise LATClientException('failed to reboot server')
+
+ logging.info('Sending username')
r = write_and_get_response(mopr, mopw, 'grim reaper')
if r.find('Local> ') == -1:
- print "Expected DEC server prompt, got ", repr(r)
+ logging.warning("Expected DEC server prompt, got " + repr(r))
raise LATClientException('failed to reboot server')
+
+ logging.info('Requesting privileges')
r = write_and_get_response(mopr, mopw, 'set priv')
if r.find('Password> ') == -1:
- print "Expected priv password prompt, got ", repr(r)
+ logging.warning("Expected priv password prompt, got " + repr(r))
raise LATClientException('failed to reboot server')
+
+ logging.info('Sending password')
r = write_and_get_response(mopr, mopw, self.priv_password, False)
if r.find('Local> ') == -1:
- print "Expected DEC server prompt, got ", repr(r)
+ logging.warning("Expected DEC server prompt, got " + repr(r))
raise LATClientException('failed to reboot server')
+
+ logging.info('Sending reboot request')
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)
+ logging.warning("Expected DEC server to die, got " + repr(r))
raise LATClientException('failed to reboot server')
+
+ logging.info('Closed connection to server')
mopr.close()
mopw.close()
- print
- print "Waiting 10 seconds for DEC server to come back to life..."
+ logging.info("Waiting 10 seconds for DEC server to come back to life...")
sleep(10)
- print "Rightyo, back to vending!"
- print
+ logging.info("Rightyo, back to vending!")
raise LATClientException('needed to reboot server')
def read_reply(self):
import re
from CRC import do_crc
from select import select
-import socket
+import socket, logging
asynchronous_responses = [ '400', '401', # door open/closed
'610', # switches changed
try:
s = self.rfh.read(1)
except socket.error:
- print "Blah, seems DEC server has fallen over"
raise VendingException('failed to read input from vending machine')
if s == '': raise VendingException('nothing read!')
if s == '\n' or s == '\r':
elif code[0] == '2':
self.events.append((KEY, int(code[1:3])))
else:
- sys.stderr.write('WARNING: Unhandled event! (%s %s)\n'%(code,text))
+ logging.warning('Unhandled event! (%s %s)\n'%(code,text))
def authed_message(self, message):
if self.challenge == None: