diff --git a/src/ssh/manager.js b/src/ssh/manager.js index 5f3c07d..5d2ae5d 100644 --- a/src/ssh/manager.js +++ b/src/ssh/manager.js @@ -1087,8 +1087,8 @@ class SSHManager { }; } - // Extrair todos os service-ports da saída - const servicePorts = this.parseServicePorts(result.output); + // Extrair service-ports que pertencem EXATAMENTE à ONU (match exato, não substring) + const servicePorts = this.parseServicePortsForOnu(result.output, slot, onuId); if (servicePorts.length === 0) { return { @@ -1150,6 +1150,43 @@ class SSHManager { return ports; } + /** + * Extrai service-ports que pertencem EXATAMENTE à ONU indicada. + * O context-match faz substring match, então "onu 3" bate em "onu 30", "onu 31", etc. + * Este método verifica a linha de configuração de cada bloco para garantir match exato. + * @param {string} output - Saída do comando show running-config service-port + * @param {string} slot - Slot da ONU (ex: 1/1/9) + * @param {string|number} onuId - ID exato da ONU + * @returns {string[]} - Array com os service-ports que pertencem exclusivamente à ONU + */ + parseServicePortsForOnu(output, slot, onuId) { + const escapedSlot = slot.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + const escapedOnuId = String(onuId).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + // Garante que "onu 3" não case com "onu 30": exige espaço ou fim de linha após o ID + const onuPattern = new RegExp(`gpon\\s+${escapedSlot}\\s+onu\\s+${escapedOnuId}(?:\\s|$)`); + + const lines = output.split('\n'); + const ports = []; + let currentPort = null; + + for (const line of lines) { + const portMatch = line.match(/^service-port\s+(\d+)/); + if (portMatch) { + currentPort = portMatch[1]; + } else if (currentPort !== null) { + if (onuPattern.test(line)) { + if (!ports.includes(currentPort)) { + ports.push(currentPort); + } + } + // Cada bloco tem apenas uma linha de configuração; reseta após verificar + currentPort = null; + } + } + + return ports; + } + /** * Remove uma ONU específica (processo completo) * Busca automaticamente todos os service-ports e remove cada um deles. @@ -1177,7 +1214,8 @@ class SSHManager { const searchCommand = `show running-config service-port | context-match "gpon ${slot} onu ${onuId}"`; console.log(`🔍 Buscando service-ports: ${searchCommand}`); const spResult = await this.executeCommand(connection.connectionId, searchCommand); - const servicePorts = spResult.success ? this.parseServicePorts(spResult.output) : []; + // Match exato: "onu 3" não pode casar com "onu 30", "onu 31", etc. + const servicePorts = spResult.success ? this.parseServicePortsForOnu(spResult.output, slot, onuId) : []; if (servicePorts.length === 0) { this.disconnect(connection.connectionId); @@ -1863,7 +1901,8 @@ class SSHManager { // Buscar service-ports existentes const spCommand = `show running-config service-port | context-match "gpon ${slot} onu ${onuId}"`; const spResult = await this.executeCommand(connection.connectionId, spCommand); - const servicePorts = spResult.success ? this.parseServicePorts(spResult.output) : []; + // Match exato: "onu 3" não pode casar com "onu 30", "onu 31", etc. + const servicePorts = spResult.success ? this.parseServicePortsForOnu(spResult.output, slot, onuId) : []; if (servicePorts.length === 0) { this.disconnect(connection.connectionId);