From 106a596dbf19f3c9b5abaa2112635444cd5c20e1 Mon Sep 17 00:00:00 2001 From: jafar Date: Sat, 8 Feb 2025 09:39:28 +0700 Subject: [PATCH] add devices name --- app.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index 8597081..9b17057 100644 --- a/app.js +++ b/app.js @@ -7,11 +7,12 @@ const app = express(); const PORT = process.env.PORT; -async function startPing(ips, res) { +async function startPing(ips,req, res) { try { const pingResults = await Promise.all( - ips.map(async (ip) => { + ips.map(async (ip, index) => { try { + console.log(req[index]); const result = await ping.promise.probe(ip); if (result.alive == 1){ return { @@ -22,6 +23,7 @@ async function startPing(ips, res) { alive: result.alive ? 1 : 0, "time ms": result.time + ' ms', output: result.output, + devices: req[index] } ] }; @@ -35,6 +37,7 @@ async function startPing(ips, res) { "time ms": result.time + ' ms', "status": "error", "message": "Network Unreachable", + devices: req[index], output: result.output } ] @@ -57,11 +60,15 @@ async function startPing(ips, res) { // Endpoint to check ping status app.get('/ping', async (req, res) => { try { + const device = process.env.TARGET_DEVICE ? process.env.TARGET_DEVICE.split(',').map(dev => dev.trim()) : []; + if (device.length === 0) { + return res.status(400).json({ error: 'No target devices configured in .env' }); + } const IPs = process.env.TARGET_IPS ? process.env.TARGET_IPS.split(',').map(ip => ip.trim()) : []; if (IPs.length === 0) { return res.status(400).json({ error: 'No target IPs configured in .env' }); } - startPing(IPs, res); + startPing(IPs, device, res); } catch (error) { res.status(500).json({ error: 'Failed to ping', details: error.message }); }