import XRegExp from 'xregexp' // Parser function const parseLog = (data: string) => { const patterns = [ // 1. Software Image & Version XRegExp( '^.*Software.*\\((?\\S+)\\),\\s+Version\\s+(?.+?),\\s+RELEASE.*\\((?\\S+)\\)' ), XRegExp('Active-image:\\s+(?\\S+)'), XRegExp('Version:\\s+(?\\S+)'), XRegExp('^ROM:\\s+(?\\S+)'), // 2. Uptime XRegExp('^(?\\S+)\\s+uptime\\s+is\\s+(?.+)'), XRegExp('Date:\\s+(?\\S+)'), XRegExp('uptime\\s+is.*\\s+(?\\d+)\\syear'), XRegExp('uptime\\s+is.*\\s+(?\\d+)\\sweek'), XRegExp('uptime\\s+is.*\\s+(?\\d+)\\sday'), XRegExp('uptime\\s+is.*\\s+(?\\d+)\\shour'), XRegExp('uptime\\s+is.*\\s+(?\\d+)\\sminute'), // 3. Reload Reason & Serial XRegExp('System\\s+image\\s+file\\s+is\\s+"(?:.*?):(?\\S+)"'), XRegExp('(?:Last reload reason:|System returned to ROM by)\\s+(?.+)'), XRegExp('[Pp]rocessor\\s+board\\s+ID\\s+(?\\w+)'), // 4. MEMORY & HARDWARE XRegExp('^(?\\d+[KMG])\\s+bytes\\s+of\\s+physical\\s+memory', 'i'), XRegExp( '[Cc]isco\\s+(?\\S+).+?with\\s+(?\\d+[KMG](?:\\/\\d+[KMG])?)\\s+bytes\\s+of\\s+memory', 'i' ), // 5. Flash / Storage XRegExp( '^(?\\d+[KMG])\\s+bytes\\s+of\\s+(?:' + '[Uu][Ss][Bb]+\\s+[Ff]lash' + '|ATA\\s+System\\s+CompactFlash.*' + '|[Ff]lash\\s+memory\\s+at\\s+(?:boot)?flash:' + '|[Ff]lash\\s+at\\s+flash:' + ')', 'i' ), // 6. Config & MAC XRegExp( 'Base\\s+[Ee]thernet\\s+MAC\\s+[Aa]ddress\\s+:\\s+(?[0-9a-fA-F]{2}(?::[0-9a-fA-F]{2}){5})' ), XRegExp('Configuration\\s+register\\s+is\\s+(?\\S+)'), ] const lines = data.split('\n') const records: any = { SOFTWARE_IMAGE: '', VERSION: '', RELEASE: '', ROMMON: '', HOSTNAME: '', UPTIME: '', UPTIME_YEARS: '', UPTIME_WEEKS: '', UPTIME_DAYS: '', UPTIME_HOURS: '', UPTIME_MINUTES: '', RELOAD_REASON: '', RUNNING_IMAGE: '', HARDWARE: '', SERIAL: '', CONFIG_REGISTER: '', MAC_ADDRESS: '', MEMORY: '', USB_FLASH: '', } for (const line of lines) { for (const pattern of patterns) { const match = XRegExp.exec(line, pattern) if (match) { const item = match?.groups || {} Object.keys(item).forEach((key) => { // Chỉ update nếu giá trị tìm thấy không undefined if (item[key] !== undefined) { records[key] = item[key] } }) // Break inner loop (patterns) để sang dòng tiếp theo break } } } return [records] } export default parseLog