Beispiel #1
0
 def delete_host(self, mac):
     msg = OMAPIMessage.open(b'host')
     msg.obj.append((b'hardware-address', utils.pack_mac_address(mac)))
     msg.obj.append((b'hardware-type', struct.pack('!I', 1)))
     response = self.query_server(msg)
     if response.opcode != OMAPIMessage.OMAPI_OP_UPDATE:
         raise ObjectNotFound()
     if response.handle == 0:
         raise OMAPIException('received invalid handle from server')
     response = self.query_server(OMAPIMessage.delete(response.handle))
     if response.opcode != OMAPIMessage.OMAPI_OP_STATUS:
         raise OMAPIException('delete failed')
Beispiel #2
0
    def get_message(self, sign=True):
        data = self.transport.read()
        message = OMAPIMessage.from_bytes(data)

        if sign and not message.verify(self.authenticator):
            self.close()
            raise OMAPIException('bad omapi message signature')

        return message
Beispiel #3
0
 def add_host(self, ip, mac):
     msg = OMAPIMessage.open(b'host')
     msg.message.append((b'create', struct.pack('!I', 1)))
     msg.message.append((b'exclusive', struct.pack('!I', 1)))
     msg.obj.append((b'hardware-address', utils.pack_mac_address(mac)))
     msg.obj.append((b'hardware-type', struct.pack('!I', 1)))
     msg.obj.append((b'ip-address', utils.pack_ip_address(ip)))
     response = self.query_server(msg)
     if response.opcode != OMAPIMessage.OMAPI_OP_UPDATE:
         raise OMAPIException('add failed')
Beispiel #4
0
 def lookup_ip(self, mac):
     msg = OMAPIMessage.open(b'lease')
     msg.obj.append((b'hardware-address', utils.pack_mac_address(mac)))
     response = self.query_server(msg)
     if response.opcode != OMAPIMessage.OMAPI_OP_UPDATE:
         raise ObjectNotFound()
     try:
         return utils.unpack_ip_address(dict(response.obj)[b'ip-address'])
     except KeyError:  # ip-address
         raise ObjectNotFound()
Beispiel #5
0
 def add_host_without_ip(self, mac_address):
     message_data = [(b'create', struct.pack('!I', 1)),
                     (b'exclusive', struct.pack('!I', 1))]
     obj_data = [(b'hardware-address', utils.pack_mac_address(mac_address)),
                 (b'hardware-type', struct.pack('!I', 1))]
     message = OMAPIMessage.open(b'host')
     message.message.extend(message_data)
     message.obj.extend(obj_data)
     response = self.query_server(message)
     if response.opcode != OMAPIMessage.OMAPI_OP_UPDATE:
         raise OMAPIException('Failed to add host {}'.format(mac_address))
Beispiel #6
0
 def lookup_hostname(self, ip):
     obj_data = [(b'ip-address', utils.pack_ip_address(ip))]
     message = OMAPIMessage.open(b'lease')
     message.obj.extend(obj_data)
     response = self.query_server(message)
     if response.opcode != OMAPIMessage.OMAPI_OP_UPDATE:
         raise ObjectNotFound()
     try:
         hostname = dict(response.obj)[b'client-hostname']
         return hostname
     except KeyError:  # client hostname
         raise ObjectNotFound()
Beispiel #7
0
    def authenticate(self, authenticator):
        message = OMAPIMessage.open(b'authenticator')
        message.update_object(authenticator.to_dict())
        response = self.query_server(message, sign=False)

        if response.opcode != OMAPIMessage.OMAPI_OP_UPDATE:
            raise OMAPIException('received non-update response for open')

        if response.handle == 0:
            raise OMAPIException('received invalid auth id from server')

        authenticator.auth_id = response.handle
        self.authenticator = authenticator
Beispiel #8
0
 def add_host_supersede_name(self, ip, mac, name):  # pylint:disable=E0213
     msg = OMAPIMessage.open(b'host')
     msg.message.append((b'create', struct.pack('!I', 1)))
     msg.message.append((b'exclusive', struct.pack('!I', 1)))
     msg.obj.append((b'hardware-address', utils.pack_mac_address(mac)))
     msg.obj.append((b'hardware-type', struct.pack('!I', 1)))
     msg.obj.append((b'ip-address', utils.pack_ip_address(ip)))
     msg.obj.append((b'name', name.encode('utf-8')))
     msg.obj.append(
         (b'statements',
          'supersede host-name "{0}";'.format(name).encode('utf-8')))
     response = self.query_server(msg)
     if response.opcode != OMAPIMessage.OMAPI_OP_UPDATE:
         raise OMAPIException('add failed')
Beispiel #9
0
 def lookup_host(self, name):
     msg = OMAPIMessage.open(b'host')
     msg.obj.append((b'name', name.encode('utf-8')))
     response = self.query_server(msg)
     if response.opcode != OMAPIMessage.OMAPI_OP_UPDATE:
         raise ObjectNotFound()
     try:
         ip = utils.unpack_ip_address(dict(response.obj)[b'ip-address'])
         mac = utils.unpack_mac_address(
             dict(response.obj)[b'hardware-address'])
         hostname = dict(response.obj)[b'name']
         return {'ip': ip, 'mac': mac, 'hostname': hostname.decode('utf-8')}
     except KeyError:
         raise ObjectNotFound()