How can PHP be used to automate the process of identifying active hosts in a network using tools like nmap?

Identifying active hosts in a network can be automated using PHP by utilizing the nmap tool. Nmap is a powerful network scanning tool that can be executed from the command line. By using PHP to execute nmap commands and parse the output, we can easily identify active hosts on a network.

<?php
// Define the network range to scan
$network_range = "192.168.1.0/24";

// Execute nmap command to scan for active hosts
$output = shell_exec("nmap -sn $network_range");

// Parse the output to identify active hosts
preg_match_all('/Nmap scan report for (.*) \(/', $output, $matches);

// Print out the list of active hosts
echo "Active hosts on the network:\n";
foreach ($matches[1] as $host) {
    echo $host . "\n";
}
?>