import XRegExp from 'xregexp' // Parser function const parseLog = (data: string) => { const patterns = [ XRegExp( '^.*Software.*\\((?\\S+)\\),\\s+Version\\s+(?.+?),\\s+RELEASE.*\\((?\\S+)\\)' ), XRegExp('Active-image:\\s+(?\\S+)'), XRegExp('Version:\\s+(?\\S+)'), XRegExp('^ROM:\\s+(?\\S+)'), 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'), 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+)'), XRegExp( '[Cc]isco\\s+(?\\S+|\\S+\\d\\S+)\\s+\\(.+\\)\\s+with\\s+(?.+)\\s+bytes' ), // XRegExp("^(?.+)\\s+bytes\\s+of\\s+[Uu][Ss][Bb]+\\s+[Ff]lash"), XRegExp( '^(?.+?)\\s+bytes\\s+of\\s+(?:' + '[Uu][Ss][Bb]+\\s+[Ff]lash' + // USB Flash '|ATA\\s+System\\s+CompactFlash.*' + // ATA System CompactFlash '|flash\\s+memory\\s+at\\s+bootflash:' + // flash memory at bootflash ')' ), 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) => { if (item[key] !== undefined) { records[key] = item[key] } }) break } } } return [records] } export default parseLog