Create a Post
cancel
Showing results for 
Search instead for 
Did you mean: 
Adam_Forester
Ambassador
Ambassador

Checking a list of IP's for Host Objects; Add or Set Group Membership

This was born out of a one-off customer request that I thought would serve as a good example for how to use a query to provide if/then results. The customer really wanted to do this in bash and not have to work in python.

Use Case:

The customer wanted to be able to take a txt file with IPv4 Address in it and do a check on each IP to see if a host object already existed. If the object already existed they wanted to create a 'set' command to assign it to a group. If the IP did not exist under a host object they wanted to create the host object using a generic name and assign it to the same group.

You can find the full script here: GitHub - WadesWeaponShed/IP-List-Host-Check-Add-or-Set 

The meat of the script is below; Using if statements we search the object database and if the search returns an object [1] then we issue the set command for it, if the search returns nothing [0] then we build the add command for the host.

printf "\nChecking For Existing Hosts\n"
   for line in $(cat ip.txt)
         do
            if [[ $(mgmt_cli -r true -d $DOMAIN show objects type host filter "$line" ip-only true offset $I limit 500 --format json |jq '.total') = 1 ]]
            then
               mgmt_cli -r true -d $DOMAIN show objects type host filter "$line" ip-only true offset $I limit 500 --format json | jq --raw-output '.objects[] | "mgmt_cli -s id.txt set host name " +.name + " groups auto-group"' >> host_set.txt
            elif [[ $(mgmt_cli -r true -d $DOMAIN show objects type host filter "$line" ip-only true offset $I limit 500 --format json |jq '.total') = 0 ]]; then
               printf "mgmt_cli -s id.txt add host name Host-$line ip-address $line groups auto-group\n" >> host_set.txt
      fi
  done

1 Reply
Juan_Concepcion
Advisor

I see the limit is set to 500 in script, does this mean it will only handle the first 500 hosts??

0 Kudos

Leaderboard

Epsum factorial non deposit quid pro quo hic escorol.

Upcoming Events

    CheckMates Events