From c7040f7ac7cb3498264d697822595cce14cba11d Mon Sep 17 00:00:00 2001 From: nguyentrungthat <80239428+nguentrungthat@users.noreply.github.com> Date: Sat, 21 Mar 2026 09:32:35 +0700 Subject: [PATCH] Update load IOS, License --- BACKEND/app/services/line_connection.ts | 106 +++++++++++--- BACKEND/app/ultils/helper.ts | 177 ------------------------ BACKEND/providers/socket_io_provider.ts | 8 +- 3 files changed, 97 insertions(+), 194 deletions(-) diff --git a/BACKEND/app/services/line_connection.ts b/BACKEND/app/services/line_connection.ts index f11239f..fe513b2 100644 --- a/BACKEND/app/services/line_connection.ts +++ b/BACKEND/app/services/line_connection.ts @@ -1390,16 +1390,14 @@ Ports Missing/Down: ${missing.length}\n\n` const [a, b] = network.split('.').map(Number) const timeZone = process.env.TIME_ZONE || 'Australia/Sydney' const startTime = momentTZ().tz(timeZone).format('YYYY/MM/DD, HH:mm:ss') + const address = `${a}.${b}.100.${this.config.id < 254 ? this.config.id : 254 - this.config.id}` + const gateway = `${station?.gateway ? station?.gateway : '0.0.0.0'}` + await this.configAddressGateway(address, gateway, 'vlan 1') + const pingSuccess = await this.pingToServer(tftpIp) + if (!pingSuccess) return await this.backupIos(nameIos) - const body = buildBody( - 'SWITCH_IOS', - tftpIp, - nameIos, - `${a}.${b}.100.${this.config.id < 254 ? this.config.id : 254 - this.config.id}`, - `${station?.gateway ? station?.gateway : '0.0.0.0'}`, - this.listDeviceIos - ) + const body = buildBody('SWITCH_IOS', tftpIp, nameIos, address, gateway, this.listDeviceIos) const script = { id: 0, @@ -1498,7 +1496,7 @@ Ports Missing/Down: ${missing.length}\n\n` const ROUTER_BIN_REGEX = /^\s*\d+\s+(\d+)\s+.*?\s+([^\s]+\.bin)\s*$/gim // 🔍 Detect device type - const isSwitch = output.includes('-rwx') + const isSwitch = output.includes('rwx') const regex = isSwitch ? SWITCH_BIN_REGEX : ROUTER_BIN_REGEX // reset regex state @@ -1624,13 +1622,18 @@ Ports Missing/Down: ${missing.length}\n\n` // Setup time/logging const timeZone = process.env.TIME_ZONE || 'Australia/Sydney' const startTime = momentTZ().tz(timeZone).format('YYYY/MM/DD, HH:mm:ss') + const address = `${a}.${b}.100.${this.config.id < 254 ? this.config.id : 254 - this.config.id}` + const gateway = `${station?.gateway ? station?.gateway : '0.0.0.0'}` + await this.configAddressGateway(address, gateway, portName) + const pingSuccess = await this.pingToServer(tftpIp) + if (!pingSuccess) return const body = buildBody( 'SWITCH_LICENSE', tftpIp, licenseFileName, - `${a}.${b}.100.${this.config.id < 254 ? this.config.id : 254 - this.config.id}`, - `${station?.gateway ? station?.gateway : '0.0.0.0'}`, + address, + gateway, this.listDeviceIos, portName ) @@ -1662,13 +1665,18 @@ Ports Missing/Down: ${missing.length}\n\n` const timeZone = process.env.TIME_ZONE || 'Australia/Sydney' const startTime = momentTZ().tz(timeZone).format('YYYY/MM/DD, HH:mm:ss') + const address = `${a}.${b}.100.${this.config.id < 254 ? this.config.id : 254 - this.config.id}` + const gateway = `${station?.gateway ? station?.gateway : '0.0.0.0'}` + await this.configAddressGateway(address, gateway, portName, true) + const pingSuccess = await this.pingToServer(tftpIp) + if (!pingSuccess) return const body = buildBody( 'ROUTER_LICENSE', tftpIp, licenseFileName, - `${a}.${b}.100.${this.config.id < 254 ? this.config.id : 254 - this.config.id}`, - `${station?.gateway ? station?.gateway : '0.0.0.0'}`, + address, + gateway, this.listDeviceIos, portName ) @@ -1964,14 +1972,80 @@ ${log} // console.log('[EXPECT]', expect, timeout) while (Date.now() - start < 60000) { if (this.outputPingToServer.includes('Success rate')) { - this.outputPingToServer = '' - this.isPingToServer = false - return true + const match = this.outputPingToServer.match(/Success rate is (\d+) percent/) + if (match) { + const rate = Number(match[1]) + if (rate > 0) { + this.outputPingToServer = '' + this.isPingToServer = false + return true + } else { + this.isPingToServer = false + this.outputPingToServer = '' + this.config.output += '\nConnect to server tftp fail\n' + this.socketIO.emit('line_output', { + stationId: this.config.stationId, + lineId: this.config.id, + data: '\nConnect to server tftp fail\n', + }) + return false + } + } } await sleep(500) } this.isPingToServer = false this.outputPingToServer = '' + this.config.output += '\nConnect to server tftp fail\n' + this.socketIO.emit('line_output', { + stationId: this.config.stationId, + lineId: this.config.id, + data: '\nConnect to server tftp fail\n', + }) return false } + + /** + * Config ip address and default gateway for line + */ + async configAddressGateway( + address: string, + gateway: string, + portName: string, + isRouter?: boolean + ) { + this.config.runningScenario = 'Config Network' + this.socketIO.emit('running_scenario', { + stationId: this.config.stationId, + lineId: this.config.id, + title: 'Config Network', + }) + await this.writeCommand(`enable\r\n`) + await sleep(500) + await this.writeCommand(`configure terminal\r\n`) + await sleep(500) + await this.writeCommand(`interface ${portName}\r\n`) + await sleep(500) + await this.writeCommand(`ip address ${address} 255.255.0.0\r\n`) + await sleep(500) + await this.writeCommand(`no shutdown\r\n`) + await sleep(500) + await this.writeCommand(`exit\r\n`) + await sleep(500) + await this.writeCommand( + !isRouter ? `ip default-gateway ${gateway}\r\n` : `ip route 0.0.0.0 0.0.0.0 ${gateway}` + ) + await sleep(500) + await this.writeCommand(`end\r\n`) + await sleep(500) + await this.writeCommand(`\r\n`) + await sleep(1000) + + this.config.runningScenario = '' + this.socketIO.emit('running_scenario', { + stationId: this.config.stationId, + lineId: this.config.id, + title: '', + }) + } } diff --git a/BACKEND/app/ultils/helper.ts b/BACKEND/app/ultils/helper.ts index 3ac8dc6..8e577c6 100644 --- a/BACKEND/app/ultils/helper.ts +++ b/BACKEND/app/ultils/helper.ts @@ -897,13 +897,6 @@ export function buildBody( /* ================= SWITCH LOAD IOS ================= */ case 'SWITCH_IOS': return [ - { - expect: '', - send: ``, - delay: '1', - repeat: '1', - note: '', - }, { expect: '', send: `enable`, @@ -911,58 +904,6 @@ export function buildBody( repeat: '1', note: '', }, - { - expect: '#', - send: `configure terminal`, - delay: '1', - repeat: '1', - note: '', - }, - { - expect: '#', - send: `interface vlan 1`, - delay: '1', - repeat: '1', - note: '', - }, - { - expect: '#', - send: `ip address ${address} 255.255.0.0`, - delay: '1', - repeat: '1', - note: '', - }, - { - expect: '#', - send: `no shutdown`, - delay: '1', - repeat: '1', - note: '', - }, - { - expect: '#', - send: `exit`, - delay: '1', - repeat: '1', - note: '', - }, - { - expect: '#', - send: `ip default-gateway ${gateway}`, - delay: '1', - repeat: '1', - note: '', - }, - { - expect: '#', - send: `end`, - delay: '1', - repeat: '1', - note: '', - }, - { expect: '', send: ``, delay: '4', repeat: '1', note: '' }, - { expect: '', send: `ping ${tftpIp}`, delay: '3', repeat: '1', note: '' }, - { expect: 'Success rate', send: ``, delay: '2', repeat: '1', note: '' }, { expect: '', send: ``, delay: '2', repeat: '1', note: '' }, { expect: '#', @@ -1092,65 +1033,6 @@ export function buildBody( repeat: '1', note: 'Enter Enable mode', }, - { - expect: '#', - send: `configure terminal`, - delay: '1', - repeat: '1', - note: 'Enter Config mode', - }, - { - expect: '#', - send: `interface ${portName ? portName : 'vlan 1'}`, - delay: '1', - repeat: '1', - note: 'Select Interface Vlan 1', - }, - { - expect: '#', - send: `ip address ${address} 255.255.0.0`, - delay: '1', - repeat: '1', - note: 'Set IP Address', - }, - { - expect: '#', - send: `no shutdown`, - delay: '1', - repeat: '1', - note: 'Up interface', - }, - { - expect: '#', - send: `exit`, - delay: '1', - repeat: '1', - note: 'Exit interface', - }, - { - expect: '#', - send: `ip default-gateway ${gateway}`, - delay: '1', - repeat: '1', - note: 'Set Gateway', - }, - { - expect: '#', - send: `end`, - delay: '1', - repeat: '1', - note: 'End config', - }, - { - expect: '', - send: ``, - delay: '4', - repeat: '1', - note: '', - }, - { expect: '', send: `ping ${tftpIp}`, delay: '3', repeat: '1', note: '' }, - { expect: 'Success rate', send: ``, delay: '2', repeat: '1', note: '' }, - { expect: '', send: ``, delay: '2', repeat: '1', note: '' }, { expect: '#', send: `license install tftp://${tftpIp}/License/${fileName}`, @@ -1256,65 +1138,6 @@ export function buildBody( repeat: '1', note: 'Enter Enable mode', }, - { - expect: '#', - send: `configure terminal`, - delay: '1', - repeat: '1', - note: 'Enter Config mode', - }, - { - expect: '#', - send: `interface ${portName ? portName : 'GigabitEthernet0/0'}`, - delay: '1', - repeat: '1', - note: 'Select management interface', - }, - { - expect: '#', - send: `ip address ${address} 255.255.0.0`, - delay: '1', - repeat: '1', - note: 'Set IP Address', - }, - { - expect: '#', - send: `no shutdown`, - delay: '1', - repeat: '1', - note: 'Up interface', - }, - { - expect: '#', - send: `exit`, - delay: '1', - repeat: '1', - note: 'Exit interface', - }, - { - expect: '#', - send: `ip route 0.0.0.0 0.0.0.0 ${gateway}`, - delay: '1', - repeat: '1', - note: 'Set default route', - }, - { - expect: '#', - send: `end`, - delay: '1', - repeat: '1', - note: 'End config', - }, - { - expect: '', - send: ``, - delay: '4', - repeat: '1', - note: '', - }, - { expect: '', send: `ping ${tftpIp}`, delay: '3', repeat: '1', note: '' }, - { expect: 'Success rate', send: ``, delay: '2', repeat: '1', note: '' }, - { expect: '', send: ``, delay: '2', repeat: '1', note: '' }, { expect: '#', send: `license install tftp://${tftpIp}/License/${fileName}`, diff --git a/BACKEND/providers/socket_io_provider.ts b/BACKEND/providers/socket_io_provider.ts index fdee533..531cf21 100644 --- a/BACKEND/providers/socket_io_provider.ts +++ b/BACKEND/providers/socket_io_provider.ts @@ -773,7 +773,13 @@ export class WebSocketIo { [lineId], async (lineCon) => { if (isReboot) { - const pingSuccess = await lineCon.pingToServer(station?.tftp_ip || '172.16.7.69') + const network = station?.gateway || '172.25.1.1' + const tftpIp = station?.tftp_ip || '172.16.7.69' + const [a, b] = network.split('.').map(Number) + const address = `${a}.${b}.100.${lineCon.config.id < 254 ? lineCon.config.id : 254 - lineCon.config.id}` + const gateway = `${station?.gateway ? station?.gateway : '0.0.0.0'}` + await lineCon.configAddressGateway(address, gateway, 'GigabitEthernet0/0', true) + const pingSuccess = await lineCon.pingToServer(tftpIp) if (!pingSuccess) return await lineCon.backupIos(iosName) if (!outletNumber || outletNumber < 0) return