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:
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.
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.
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
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"
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.
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.
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
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