--- /dev/null
+from socket import *
+
+LATCP_SOCKET = '/var/run/latlogin'
+
+LAT_VERSION = '1.21'
+LATCP_CMD_VERSION = 8
+LATCP_CMD_TERMINALSESSION = 26
+LATCP_CMD_ERRORMSG = 99
+
+class LATClient:
+ def __init__(self, service = None, node = None, port = None, \
+ localport = None, password = None, is_queued = False):
+ self.sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ self.sock.connect(LATCP_SOCKET)
+ self.send_msg(LATCP_CMD_VERSION, LAT_VERSION+'\000')
+ (cmd, msg) = self.read_reply()
+ if service == None: service = ''
+ if node == None: node = ''
+ if port == None: port = ''
+ if localport == None: localport = ''
+ if password == None: password = ''
+ if is_queued == True:
+ is_queued = 1
+ else:
+ is_queued = 0
+ self.send_msg(LATCP_CMD_TERMINALSESSION, '%c%c%s%c%s%c%s%c%s%c%s' % \
+ (is_queued,
+ len(service), service,
+ len(node), node,
+ len(port), port,
+ len(localport), localport,
+ len(password), password
+ ))
+ (cmd, msg) = self.read_reply()
+ if cmd == LATCP_CMD_ERRORMSG:
+ raise Exception
+
+ def send_msg(self, cmd, msg):
+ self.sock.send('%c%c%c%s'%(cmd, len(msg)/256, len(msg)%256, msg))
+
+ def read_reply(self):
+ head = self.sock.recv(3)
+ if len(head) != 3:
+ sys.stderr.write('Error: Short LAT packet\n')
+ return None
+ cmd = head[0]
+ length = ord(head[1])*256 + ord(head[2])
+ msg = self.sock.recv(length)
+ if cmd == LATCP_CMD_ERRORMSG:
+ sys.stderr.write('Error: Received LAT error: %s\n'%msg)
+ return (cmd, msg)
+
+ def get_fh(self):
+ return (self.sock.makefile('r'), self.sock.makefile('w'))