add devices name
This commit is contained in:
parent
f8349cdde2
commit
106a596dbf
13
app.js
13
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 });
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user