diff --git a/app/utils/checkIndexSN.ts b/app/utils/checkIndexSN.ts index bec3225..af7ae73 100644 --- a/app/utils/checkIndexSN.ts +++ b/app/utils/checkIndexSN.ts @@ -187,7 +187,7 @@ export const checkIndexSN = async (content, beginLine, nameF) => { { headers: { Authorization: "Bearer " + token } } ); - if (Env.get("RUN_ENV") !== "prod") { + if (Env.get("RUN_ENV") === "prod") { const response_int = await axios.post( "https://int.ipsupply.com.au/api/transferPostData", data, diff --git a/app/utils/runtimeCheckLogs.ts b/app/utils/runtimeCheckLogs.ts index bc09fbb..2b29ecd 100644 --- a/app/utils/runtimeCheckLogs.ts +++ b/app/utils/runtimeCheckLogs.ts @@ -38,9 +38,9 @@ export async function runtimeCheckLogs(folderPath) { try { const regex = /\(CAT3K_CAA-UNIVERSALK9-M\), Version 16\.9\.[2-9]/; const regex1 = - /\(CAT3K_CAA-UNIVERSALK9-M\), Version 1[7-9]\.[0-9]\.[2-9]/; + /\(CAT3K_CAA-UNIVERSALK9-M\), Version 1[7-9]\.[0-9]\.[0-9]/; const regex2 = - /\(CAT3K_CAA-UNIVERSALK9-M\), Version [2-9][0-9]\.[0-9]\.[2-9]/; + /\(CAT3K_CAA-UNIVERSALK9-M\), Version [2-9][0-9]\.[0-9]\.[0-9]/; // Use the regular expression to find the match const match = paragraph.match(regex); const match1 = paragraph.match(regex1); diff --git a/app/utils/sendDeviceInfor.ts b/app/utils/sendDeviceInfor.ts new file mode 100644 index 0000000..162c7c8 --- /dev/null +++ b/app/utils/sendDeviceInfor.ts @@ -0,0 +1,314 @@ +import Env from "@ioc:Adonis/Core/Env"; +import fs from "fs"; +import moment from "moment/moment"; +import dotenv from "dotenv"; +import nodeMailer from "nodemailer"; +dotenv.config(); + +export const sendDeviceInfora = async () => { + try { + //Regex check show inventory + const regexInventory = /sh.*? inv.*/; + //Regex check show version + const regexVersion = /sh.*? ver.*/; + const regexMemory = /(\d+)K/g; + const date = moment(Date.now()).format("YYYYMMDD"); + const listInformation = []; + let html = ""; + //List file today + const listFile = fs + .readdirSync(Env.get("FOLDER_LOGS")) + + //Configure mail + const transporter = nodeMailer.createTransport({ + pool: true, + host: "mail.ipsupply.com.au", + port: 465, + secure: true, + auth: { + user: "admin@apactech.io", + pass: "BGK!dyt6upd2eax1bhz", + }, + }); + + //Read file in listFile + await listFile.map((file) => { + fs.readFile(Env.get("FOLDER_LOGS") + "/" + file, "utf8", (err, data) => { + if (err) { + console.log(`Error reading file: ${err}`); + } else { + //Array line + const lines = data?.split("\n"); + const linesInventory = []; + //Get index of "lines" with show inv + lines?.map((line, index) => { + if (line.match(regexInventory) !== null) { + linesInventory.push(index); + } + }); + // console.log(linesInventory); + //cut content with content1 = [linesInventory[index],linesInventory[index+1]] ... + linesInventory?.map((line, index) => { + const deviceContent = lines?.slice( + linesInventory[index], + linesInventory[index + 1] + ); + let backgroundColor = "rgb(200 200 200 / 30%)" + const showInventory = []; + let check = true; + let begin = 0; + let end = 4; + + //get showInventory content + while (check === true) { + if ( + deviceContent + .slice(begin, end) + .filter( + (i) => + i.includes("PID:") && + i.includes("VID:") && + i.includes("SN:") + ).length > 0 + ) { + showInventory.push(deviceContent.slice(begin, end).join("\n")); + begin = end; + end = end + 4; + } else { + check = false; + } + } + + const showInventoryContent = showInventory + .join("\n") + .split("\n") + .filter( + (i) => + i.includes("PID:") && i.includes("VID:") && i.includes("SN:") + ); + + //show version exists + if ( + deviceContent.filter((line) => line.match(regexVersion) !== null) + .length > 0 + ) { + const lineShowver = deviceContent.indexOf( + deviceContent.filter( + (line) => line.match(regexVersion) !== null + )[0] + ); + + const showVersion = deviceContent.slice( + lineShowver, + deviceContent.indexOf( + deviceContent.filter( + (line) => line.search("Configuration register") !== -1 + )[0] + ) + 1 + ); + + showInventoryContent.map((u, index) => { + const PID = u + ?.split("VID:")[0] + ?.split("PID:")[1] + ?.replace(/,/g, "") + .trim(); + const VID = u + ?.split("VID:")[1] + ?.split("SN:")[0] + ?.replace(/,/g, "") + .trim(); + const SN = u?.split("SN:")[1]?.replace(/,/g, "").trim(); + // let memory = + if (index > 0) { + if (PID !== "" && SN !== "") { + // let RAM = + // showVersion + // .filter((line) => line.includes("bytes of memory")) + // .join("
") + // .match(regexMemory) !== null + // ? ( + // parseInt( + // showVersion + // .filter((line) => + // line.includes("bytes of memory") + // ) + // .join("
") + // .match(regexMemory)[0] + // ) / + // 1024 / + // 1024 + // ).toFixed(2) + "G" + // : ""; + + // let flash = showVersion + // .filter((line) => + // line.toLocaleLowerCase().includes("compactflash") + // ) + // .join("
"); + listInformation.push({ + PID: PID, + VID: VID, + SN: SN, + RAM: "", + flash: "", + extraItem: "yes", + }); + + html += `${PID} + ${VID} + ${SN} + + + yes`; + } + } else { + if (PID !== "" && SN !== "") { + let RAM = + showVersion + .filter((line) => line.includes("bytes of memory") || line.includes("bytes of physical memory")) + .join("
") + .match(regexMemory) !== null + ? ( + parseInt( + showVersion + .filter((line) => + line.includes("bytes of memory") || line.includes("bytes of physical memory") + ) + .join("
") + .match(regexMemory)[0] + ) / + 1024 / + 1024 + ).toFixed(2) + "G" + : ""; + + let flash = showVersion + .filter((line) => + line.toLocaleLowerCase().includes("compactflash") + ) + .join("
"); + listInformation.push({ + PID: PID, + VID: VID, + SN: SN, + RAM: RAM, + flash: + flash.match(regexMemory) !== null + ? ( + parseInt(flash.match(regexMemory)[0]) / + 1024 / + 1024 + ).toFixed(2) + "G" + : "", + extraItem: "no", + }); + + html += `${PID} + ${VID} + ${SN} + ${RAM} + ${ + flash.match(regexMemory) !== null + ? ( + parseInt(flash.match(regexMemory)[0]) / + 1024 / + 1024 + ).toFixed(2) + "G" + : "" + } + no`; + } + } + }); + } else { + //show version not exists --> RAM = N/A, Flash = N/A + showInventoryContent.map((u, index) => { + const PID = u + ?.split("VID:")[0] + ?.split("PID:")[1] + ?.replace(/,/g, "") + .trim(); + const VID = u + ?.split("VID:")[1] + ?.split("SN:")[0] + ?.replace(/,/g, "") + .trim(); + const SN = u?.split("SN:")[1]?.replace(/,/g, "").trim(); + + if (index > 0) { + if (PID !== "" && SN !== "") { + listInformation.push({ + PID: PID, + VID: VID, + SN: SN, + RAM: "", + flash: "", + extraItem: "yes", + }); + + html += `${PID} + ${VID} + ${SN} + + + yes`; + } + } else { + if (PID !== "" && SN !== "") { + listInformation.push({ + PID: PID, + VID: VID, + SN: SN, + RAM: "", + flash: "", + extraItem: "no", + }); + + html += `${PID} + ${VID} + ${SN} + + + no`; + } + } + }); + } + }); + } + }); + }); + + setTimeout(() => { + console.log(listInformation); + const options = { + from: "admin@apactech.io", + to: "joseph@apactech.io", + subject: "SN AUTO REPORT", + html: + "\ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + " + + html + + "\ +
PIDVIDSNRAMFlashExtra Item
", + }; + transporter.sendMail(options); + }, 5000); + // const match = "show inventory".match(regexInventory); + + // console.log(match) + } catch (error) { + console.log(error); + } +}; diff --git a/package-lock.json b/package-lock.json index 004497e..d89afc5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,12 +17,14 @@ "axios": "^1.4.0", "child_process": "^1.0.2", "chokidar": "^3.5.3", + "dotenv": "^16.3.1", "fs": "^0.0.1-security", "helpers": "^0.0.6", "jsonwebtoken": "^9.0.1", "luxon": "^3.4.0", "moment": "^2.29.4", "mysql2": "^3.6.0", + "nodemailer": "^6.9.5", "path": "^0.12.7", "proxy-addr": "^2.0.7", "puppeteer": "^21.2.1", @@ -6200,6 +6202,14 @@ "acorn-walk": "^8.0.2" } }, + "node_modules/nodemailer": { + "version": "6.9.5", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.5.tgz", + "integrity": "sha512-/dmdWo62XjumuLc5+AYQZeiRj+PRR8y8qKtFCOyuOl1k/hckZd8durUUHs/ucKx6/8kN+wFxqKJlQ/LK/qR5FA==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", @@ -14080,6 +14090,11 @@ "acorn-walk": "^8.0.2" } }, + "nodemailer": { + "version": "6.9.5", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.5.tgz", + "integrity": "sha512-/dmdWo62XjumuLc5+AYQZeiRj+PRR8y8qKtFCOyuOl1k/hckZd8durUUHs/ucKx6/8kN+wFxqKJlQ/LK/qR5FA==" + }, "normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", diff --git a/package.json b/package.json index 32c734b..2a1b2db 100644 --- a/package.json +++ b/package.json @@ -30,12 +30,14 @@ "axios": "^1.4.0", "child_process": "^1.0.2", "chokidar": "^3.5.3", + "dotenv": "^16.3.1", "fs": "^0.0.1-security", "helpers": "^0.0.6", "jsonwebtoken": "^9.0.1", "luxon": "^3.4.0", "moment": "^2.29.4", "mysql2": "^3.6.0", + "nodemailer": "^6.9.5", "path": "^0.12.7", "proxy-addr": "^2.0.7", "puppeteer": "^21.2.1", diff --git a/screenshot.js b/screenshot.js index 3c35259..10f0f01 100644 --- a/screenshot.js +++ b/screenshot.js @@ -1,40 +1,48 @@ +const regex = /(\d+)K/g; + +console.log( + "1000944K bytes of ATA System CompactFlash 0 (Read/Write) " + .match(regex) + .map((obj) => (parseInt(obj.replace("K", ""))/1024/1024).toFixed(2)+"G") +); + // let a = [1,2,3] // console.log(a.slice(0,4)) -const checkSpecialVersion = (paragraph) => { - try { - const regex = /\(CAT3K_CAA-UNIVERSALK9-M\), Version 16\.9\.[2-9]/; - const regex1 = /\(CAT3K_CAA-UNIVERSALK9-M\), Version 1[7-9]\.[0-9]\.[2-9]/; - const regex2 = - /\(CAT3K_CAA-UNIVERSALK9-M\), Version [2-9][0-9]\.[0-9]\.[2-9]/; - // Use the regular expression to find the match - const match = paragraph.match(regex); - const match1 = paragraph.match(regex1); - const match2 = paragraph.match(regex2); - if (match || match1 || match2) { - if (match) { - console.log(match[0]); - } +// const checkSpecialVersion = (paragraph) => { +// try { +// const regex = /\(CAT3K_CAA-UNIVERSALK9-M\), Version 16\.9\.[2-9]/; +// const regex1 = /\(CAT3K_CAA-UNIVERSALK9-M\), Version 1[7-9]\.[0-9]\.[2-9]/; +// const regex2 = +// /\(CAT3K_CAA-UNIVERSALK9-M\), Version [2-9][0-9]\.[0-9]\.[2-9]/; +// // Use the regular expression to find the match +// const match = paragraph.match(regex); +// const match1 = paragraph.match(regex1); +// const match2 = paragraph.match(regex2); +// if (match || match1 || match2) { +// if (match) { +// console.log(match[0]); +// } - if (match1) { - console.log(match1[0]); - } +// if (match1) { +// console.log(match1[0]); +// } - if (match2) { - console.log(match2[0]); - } - } else { - return ""; - } - } catch (error) { - console.log(error); - } -}; +// if (match2) { +// console.log(match2[0]); +// } +// } else { +// return ""; +// } +// } catch (error) { +// console.log(error); +// } +// }; -checkSpecialVersion( - "Cisco IOS Software [Fuji], Catalyst L3 Switch Software (CAT3K_CAA-UNIVERSALK9-M), Version 16.9.1, RELEASE SOFTWARE (fc2)" -); +// checkSpecialVersion( +// "Cisco IOS Software [Fuji], Catalyst L3 Switch Software (CAT3K_CAA-UNIVERSALK9-M), Version 16.9.1, RELEASE SOFTWARE (fc2)" +// ); // const puppeteer = require("puppeteer"); // const zulip = require("zulip-js"); diff --git a/start/routes.ts b/start/routes.ts index f5c0786..0e45dca 100644 --- a/start/routes.ts +++ b/start/routes.ts @@ -25,6 +25,7 @@ import Env from "@ioc:Adonis/Core/Env"; import { sendMessToZulip } from "App/utils/sendMessToZulip"; import moment from "moment"; import Product from "App/Models/Product"; +import { sendDeviceInfora } from "App/utils/sendDeviceInfor"; runtimeCheckLogs(Env.get("FOLDER_LOGS")); @@ -279,3 +280,12 @@ Route.post("/api/backupProduct", async ({ request, response }) => { ); } }).middleware("writeLog"); + +Route.post("/api/test", async ({ request, response }) => { + try { + sendDeviceInfora() + } catch (error) { + console.log(error) + } +}) +