add devices name

This commit is contained in:
jafar 2025-02-08 09:39:28 +07:00
parent f8349cdde2
commit 106a596dbf

13
app.js
View File

@ -7,11 +7,12 @@ const app = express();
const PORT = process.env.PORT; const PORT = process.env.PORT;
async function startPing(ips, res) { async function startPing(ips,req, res) {
try { try {
const pingResults = await Promise.all( const pingResults = await Promise.all(
ips.map(async (ip) => { ips.map(async (ip, index) => {
try { try {
console.log(req[index]);
const result = await ping.promise.probe(ip); const result = await ping.promise.probe(ip);
if (result.alive == 1){ if (result.alive == 1){
return { return {
@ -22,6 +23,7 @@ async function startPing(ips, res) {
alive: result.alive ? 1 : 0, alive: result.alive ? 1 : 0,
"time ms": result.time + ' ms', "time ms": result.time + ' ms',
output: result.output, output: result.output,
devices: req[index]
} }
] ]
}; };
@ -35,6 +37,7 @@ async function startPing(ips, res) {
"time ms": result.time + ' ms', "time ms": result.time + ' ms',
"status": "error", "status": "error",
"message": "Network Unreachable", "message": "Network Unreachable",
devices: req[index],
output: result.output output: result.output
} }
] ]
@ -57,11 +60,15 @@ async function startPing(ips, res) {
// Endpoint to check ping status // Endpoint to check ping status
app.get('/ping', async (req, res) => { app.get('/ping', async (req, res) => {
try { 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()) : []; const IPs = process.env.TARGET_IPS ? process.env.TARGET_IPS.split(',').map(ip => ip.trim()) : [];
if (IPs.length === 0) { if (IPs.length === 0) {
return res.status(400).json({ error: 'No target IPs configured in .env' }); return res.status(400).json({ error: 'No target IPs configured in .env' });
} }
startPing(IPs, res); startPing(IPs, device, res);
} catch (error) { } catch (error) {
res.status(500).json({ error: 'Failed to ping', details: error.message }); res.status(500).json({ error: 'Failed to ping', details: error.message });
} }