PHP+SQL实现192.168.1.0/24网段IP扫描及在线状态表格展示
Solution: Generate 192.168.1.0/24 IP List with Ping Status in Table
Hey Ruben, let's work through this problem together—since you already have some PHP/SQL background, I'll keep this practical and focused on a working solution you can tweak easily.
Step 1: Generate the Full IP Range
First, we'll loop through 1 to 254 to create every valid IP in the 192.168.1.0/24 subnet. No fancy tricks here, just a simple for loop to cover all 254 addresses.
Step 2: Implement Cross-OS Ping Checks
Ping commands vary between Windows and Unix-like systems (Linux/macOS), so we'll add a check to use the right parameters:
- Windows:
ping -n 2 [IP](sends 2 packets, matches your 1-2 ping requirement) - Linux/macOS:
ping -c 2 -W 1 [IP](sends 2 packets with a 1-second timeout to keep things fast)
Step 3: Display Results in a Table with Visual Status
We'll output an HTML table with each IP, its online/offline status, and a clear emoji indicator (no external image links needed—emojis work everywhere).
Full Working PHP Code
<!DOCTYPE html> <html> <head> <title>192.168.1.0/24 Network Scan</title> <style> table { border-collapse: collapse; width: 80%; margin: 2rem auto; } th, td { border: 1px solid #ddd; padding: 0.5rem; text-align: center; } th { background-color: #f5f5f5; font-weight: bold; } .online { color: #2ecc71; } .offline { color: #e74c3c; } </style> </head> <body> <h1 style="text-align:center;">192.168.1.0/24 Ping Scan Results</h1> <table> <tr> <th>IP Address</th> <th>Status</th> <th>Indicator</th> </tr> <?php // Detect operating system to use correct ping command $isWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; // Loop through all valid IPs in the subnet for ($i = 1; $i <= 254; $i++) { $ip = "192.168.1.$i"; $isOnline = false; // Run ping command based on OS if ($isWindows) { exec("ping -n 2 -w 1000 $ip > nul 2>&1", $output, $returnCode); $isOnline = $returnCode === 0; } else { exec("ping -c 2 -W 1 $ip > /dev/null 2>&1", $output, $returnCode); $isOnline = $returnCode === 0; } // Set status text and icon $status = $isOnline ? 'Online' : 'Offline'; $icon = $isOnline ? '✅' : '❌'; $class = $isOnline ? 'online' : 'offline'; // Print table row echo "<tr> <td>$ip</td> <td class='$class'>$status</td> <td>$icon</td> </tr>"; } ?> </table> </body> </html>
Key Notes for Your Use Case:
- Cross-OS Support: The code automatically detects if it's running on Windows or Linux/macOS, so you don't have to tweak commands manually.
- Ping Efficiency: We're sending exactly 2 pings per IP with a short timeout to avoid waiting too long for unresponsive devices.
- No External Dependencies: Emojis are used instead of external images to keep things self-contained and avoid broken links.
- Tweakable: If you want to adjust ping count or timeout, just modify the numbers in the
pingcommand strings.
Quick Troubleshooting:
- If pings aren't returning correctly, check that PHP has permission to run system commands (disable
safe_modein php.ini if you're on an older PHP version). - On Linux, ensure your firewall allows ICMP requests (you might need to run
sudo iptables -A INPUT -p icmp -j ACCEPTtemporarily).
内容的提问来源于stack exchange,提问作者Ruben
相关产品推荐
相关产品推荐

