Sorry, I made a small logic error in the script. Here the updated and I hope the final one.
#!/bin/bash
echo "- Obteniendo token..."
TOKEN=$(curl -s -u USER:PASSWORD -k -X GET "http://localhost:55000/security/user/authenticate?raw=true")
echo "- Obteniendo info de los agentes..."
curl -s -k -XGET "http://localhost:55000/agents?pretty=true&select=id,name,ip,os.uname" -H "Authorization: Bearer $TOKEN" | jq -r '.data.affected_items[]| .id + "," + .name + "," + .ip + "," + .os.uname' >> tmp.txt
# Por cada cliente:
# - Obtener el ID de cada cliente
# - Guardar en 'info' datos del agente.
# - Consultar los grupos de cada agente y guardarlo en variable 'grupos'.
# - Generar mensaje de salida con id de cliente, info y grupos. Guardar en fichero csv.csv.
echo "- Obteniendo grupos y generando salida deseada..."
for i in `cut -d"," -f1 tmp.txt`
do
info=`grep "^${i}," tmp.txt | cut -d"," -f2- `
Example to get agent ID, name, IP and groups to which it belongs:
- Obteniendo info de los agentes...
- Obteniendo grupos y generando salida deseada...
000,wazuh,127.0.0.1,Linux |wazuh |4.19.0-10-amd64 |#1 SMP Debian 4.19.132-1 (2020-07-24) |x86_64,null
002,SRVWIN01,192.168.50.10,Microsoft Windows Server 2012 R2 Standard,["default","windows_servidores","web_server"]
004,SRVWIN02,192.168.50.12,Microsoft Windows Server 2012 Standard,["default","windows_servidores"]
007,SRVWIN03,192.168.50.13,Microsoft Windows Server 2012 R2 Standard,["default","windows_servidores"]
008,SRVWIN04,192.168.50.14,Microsoft Windows Server 2012 R2 Standard,["default"]
018,SRVLINUX,192.168.50.20,Linux |srvlinux |3.10.0-862.9.1.el7.x86_64 |#1 SMP Mon Jul 16 16:29:36 UTC 2018 |x86_64,["default"]
Thank you all and thank you for making this product and this community possible. And if someone knows a more efficient solution to this issue, please comment it.