22. 03. 2025 Alessandro Paoli Asset Management, NetEye

Automating Network Discovery and Data Injection for GLPI

Preamble:
A use case from one of our clients required scanning several subnets to locate all the printers to be imported into GLPI. Their infrastructure features a cloud-based Neteye Master and a Satellite within their farm. The connection between the master and the satellite is unidirectional—from the Satellite to the Master. With this type of setup, it is not possible to use the tasks provided by the GLPI Inventory Plugin, so I developed this idea:

Overview

In this project, we developed a Bash script to automate the discovery of network devices and integrate the data into a GLPI inventory system. Our goal was to speed up the scanning process by targeting only active IP addresses and to streamline data ingestion into the GLPI system. Here’s an overview of our approach and improvements.

How It Works

1. IP Range Processing

The script reads an input file containing multiple IP ranges. Each line in the file specifies a start and an end IP (for example, 172.1.1.1 172.1.1.254). By converting each IP address into an integer, the script efficiently iterates over each range and converts the numbers back to the standard dotted-quad format for processing.

2. Preliminary Ping Check

To reduce unnecessary processing and speed up the overall scan, the script performs a ping test for each IP address. Only IP addresses that respond to the ping are further processed with the glpi-netinventory command. This approach minimizes the execution time by skipping inactive hosts.

for (( ip_int=start_int; ip_int<=end_int; ip_int++ )); do
        ip=$(int2ip "$ip_int")
        echo "Ping di $ip..."

        if ping -c 1 -W 1 "$ip" > /dev/null 2>&1; then

3. Executing glpi-netinventory

For each reachable IP, the script executes the glpi-netinventory command using predefined credentials and a timeout setting. The command queries the device and retrieves its configuration details in XML format.

while read -r start_ip end_ip; do
    start_int=$(ip2int "$start_ip")
    end_int=$(ip2int "$end_ip")
    
    for (( ip_int=start_int; ip_int<=end_int; ip_int++ )); do
        ip=$(int2ip "$ip_int")
        echo "Ping di $ip..."
        
        if ping -c 1 -W 1 "$ip" > /dev/null 2>&1; then
            output=$(glpi-netinventory --host "$ip" --credentials version:2c,community:public --timeout 10)
            
            if [ -n "$output" ] && ! echo "$output" | grep -q "SNMP communication error:"; then
                echo "Ok for $ip, salvataggio in $output_dir/$ip.xml"
                echo "$output" > "$output_dir/$ip.xml"
            else
                echo "KO"
            fi
        else
            echo "$ip don't responde, jump..."
        fi
    done
done < "$ip_ranges_file"

4. Filtering Error Responses

The output from glpi-netinventory is analyzed to ensure that it is valid. In particular, if the response contains an error message indicating an SNMP communication error (such as a message stating “SNMP communication error: no response from host”), the result is discarded. This ensures that only valid data is saved.

5. Storing Valid Results

Successful responses are saved as XML files in a dedicated folder. Each file is named according to the IP address (for example, 172.1.1.15.xml), making it easy to manage the collected data.

6. Data Injection Post-Scan

After scanning all IP ranges, the script triggers the glpi-injector command to push the collected XML files into the GLPI inventory system. This final step is executed only once, ensuring that the data is sent in one coherent batch.

glpi-injector -d "$output_dir" -u "https://user:password@serverglpi.it/glpi/front/inventory.php" --no-ssl-check

Conclusion

This Bash scripting solution significantly enhances the efficiency of network device discovery for GLPI. By incorporating a preliminary ping test, filtering out error responses, and automating data injection after scanning all IP ranges, the overall execution time is reduced while ensuring that only high-quality data is forwarded to the GLPI system. This approach is particularly useful for environments with large IP ranges, where performance and data accuracy are critical.

Feel free to try out and customize the script further to meet your specific network management needs!

references

https://glpi-agent.readthedocs.io/en/1.7/man/glpi-netinventory.html

Alessandro Paoli

Alessandro Paoli

My name is Alessandro Paoli and I've been a Technical Consultant at Wurth Phoenix since May 2024. I've always had a great passion for IT and since 2004 it has also become my job. In 2015 I found my role in the field, monitoring. I have had the opportunity to use various monitoring products, both open source and proprietary, I have worked on numerous projects from small businesses to global companies. I am married and have 2 wonderful daughters. My passions are travel, cinema, games (video and board) and comics, and every now and then I manage to indulge in a few days of sport (Padel and gym).

Author

Alessandro Paoli

My name is Alessandro Paoli and I've been a Technical Consultant at Wurth Phoenix since May 2024. I've always had a great passion for IT and since 2004 it has also become my job. In 2015 I found my role in the field, monitoring. I have had the opportunity to use various monitoring products, both open source and proprietary, I have worked on numerous projects from small businesses to global companies. I am married and have 2 wonderful daughters. My passions are travel, cinema, games (video and board) and comics, and every now and then I manage to indulge in a few days of sport (Padel and gym).

Leave a Reply

Your email address will not be published. Required fields are marked *

Archive