Stella inattivaStella inattivaStella inattivaStella inattivaStella inattiva
 

I created a script PHP in collaboration with COPILOT to find all aliases not used in the PF Sense file config.xml.

 

Streamlining pfSense Configuration with a Smart Script

Managing a pfSense firewall configuration can be a daunting task, especially when it comes to keeping track of used and unused aliases. To address this challenge, a new script has been developed that simplifies the process, ensuring your configuration is clean and efficient.

Advantages of the Script:

  • Automated Detection: The script automatically identifies unused aliases within the config.xml file, helping to prevent clutter and potential confusion in firewall rules.
  • Dual Output: It generates two separate text files, Alias-Used.txt and Alias-NOT-Used.txt, for easy review and management of both used and unused aliases.
  • Time-Saving: By automating what would otherwise be a manual check, the script saves valuable time for system administrators.
  • Enhanced Accuracy: Manual checks are prone to error, but this script reduces the risk by methodically checking each alias against the configuration file.
  • Ease of Use: With a simple execution, the script provides immediate results, displayed directly and saved in text files for later reference.
  • Windows Compatibility: Designed for Windows environments, the script includes commands to open the resulting text files directly in Notepad for immediate viewing.

This script was created in collaboration with COPILOT, leveraging collective expertise to deliver a tool that enhances pfSense firewall management.

Feel free to use and share this script with others who might find it beneficial for their pfSense configurations. It’s a testament to the power of collaboration and innovation in the tech community.


Feel free to adjust the content as needed to fit your post. If you need further assistance or modifications, let me know!

 

 

<?php
// Load PF Sense config file
$xml = simplexml_load_file('config.xml');

// Get all aliases from config.xml
$aliases = $xml->xpath('//aliases/alias/name');

// Create array with alias names
$alias_names = array();
foreach ($aliases as $alias) {
    $alias_names[] = (string)$alias;
}

// Function to check if an alias is used or not
function is_alias_referenced($alias_name, $xml) {
    $references = $xml->xpath("//*[contains(@*,'$alias_name') or contains(text(),'$alias_name')]");

    // If array has just 1 element is itself and so it's not referenced
    if (count($references) == 1 && (string)$references[0] == $alias_name) {
        return false; // L'alias non è utilizzato
    }
    return !empty($references); // The alias is used if there are more references.
}

// -- Create 2 arrays with used and unused aliases
$used_aliases = array();
$unused_aliases = array();

foreach ($alias_names as $alias_name) {
    if (is_alias_referenced($alias_name, $xml)) {
        $used_aliases[] = $alias_name;
    } else {
        $unused_aliases[] = $alias_name;
    }
}

// -- Print and write used and unused aliases.
echo 'Alias used:' . PHP_EOL;
echo implode(PHP_EOL, $used_aliases);
file_put_contents('Alias-Used.txt', implode(PHP_EOL, $used_aliases));

echo 'Alias not used:' . PHP_EOL;
echo implode(PHP_EOL, $unused_aliases);
file_put_contents('Alias-NOT-Used.txt', implode(PHP_EOL, $unused_aliases));

// Apri i due file di testo con l'Explorer di Windows
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    pclose(popen("start /B explorer Alias-Used.txt", "r"));
    pclose(popen("start /B explorer Alias-NOT-Used.txt", "r"));
} else {
    echo "Comand not supported.";
}
echo "Results are stored in the files Alias-Used.txt and Alias-NOT-Used.txt";

 

- have fun -

 

 

 

 

 

 

DISQUS - Leave your comments here