from socket import *
+from select import select
+from os import popen
+from time import sleep
LATCP_SOCKET = '/var/run/latlogin'
LATCP_CMD_ERRORMSG = 99
class LATClient:
- def __init__(self, service = None, node = None, port = None, \
- localport = None, password = None, is_queued = False):
+ def __init__(self, service = None, node = None, port = None,
+ localport = None, password = None, is_queued = False,
+ server_name = '', connect_password='', priv_password=''):
+
+ self.server_name = server_name
+ self.connect_password = connect_password
+ self.priv_password = priv_password
+
self.sock = socket(AF_UNIX, SOCK_STREAM, 0);
self.sock.connect(LATCP_SOCKET)
self.send_msg(LATCP_CMD_VERSION, LAT_VERSION+'\000')
if ord(cmd) == LATCP_CMD_ERRORMSG:
raise Exception('LATClient: '+msg)
+ r = select([self.sock], [], [], 2.0)[0]
+ if r:
+ l = self.sock.readline()
+ if l.find('Service in use') >= 0:
+ print "Service in use, apparently: restarting DEC server"
+ self.reboot_server()
+
def __del__(self):
try:
self.sock.close()
def send_msg(self, cmd, msg):
self.sock.send('%c%c%c%s'%(cmd, len(msg)/256, len(msg)%256, msg))
+ 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()
+ print "Waiting 10 seconds for DEC server to come back to life..."
+ sleep(10)
+
def read_reply(self):
head = self.sock.recv(3)
if len(head) != 3: