make me a nodejs script that scan the 192.168.19.0/24 network using ping with only a count of one ping and a timeout of 100ms
时间: 2024-10-22 09:06:02 浏览: 17
Sure, I can help you create a simple Node.js script to perform a single-ping check with a timeout of 100 milliseconds on IP addresses within the 192.168.19.0/24 subnet. Here's an example using the `net` and `child_process` modules:
```javascript
const net = require('net');
const { exec } = require('child_process');
// Define the target subnet and timeout
const subnet = '192.168.19.';
const timeoutMs = 100;
function pingHost(ip) {
const cmd = `ping -c 1 -w ${timeoutMs} ${ip}`;
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.log(`Failed to ping ${ip}:`, error);
reject(error);
} else {
// Check if ping was successful (no "Request timed out" in output)
if (!stdout.includes('Request timed out')) {
resolve(true); // Ping succeeded
} else {
resolve(false); // Ping timed out
}
}
});
});
}
async function scanSubnet() {
try {
for (let i = 1; i <= 254; i++) {
const address = `${subnet}${i}`;
await pingHost(address);
}
} catch (error) {
console.error('Error scanning subnet:', error);
}
}
scanSubnet();
```
This script defines a function `pingHost` that sends a single ping command with a given timeout and checks if it succeeds or times out. The `scanSubnet` function iterates through all possible IP addresses in the 192.168.19.0/24 range and calls `pingHost` for each one.
Keep in mind that this script assumes you have a Unix-like system with `ping` utility installed. If your environment is different, you might need to adjust the command accordingly.
阅读全文