Last active
January 11, 2018 16:13
-
-
Save AJ-Acevedo/83e4333eb42a36b8d3518498edba6ba2 to your computer and use it in GitHub Desktop.
Pull a complete hardware list from Samanage and generate a list of MAC addresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Description: Pull a complete hardware list from Samanage and generate a list of MAC addresses | |
# syntax: ./samanage_GET_MAC.sh | |
# | |
# Author: AJ Acevedo | |
# License: MIT License | |
# Version 0.5 | |
# API TOKEN | |
TOKEN="INSERT YOUR API TOKEN HERE" | |
# API Call to get the total number of Samanage Assets | |
TOTAL_ASSETS=$(curl -H "X-Samanage-Authorization: Bearer $TOKEN" \ | |
-H 'Accept: application/vnd.samanage.v2.1+xml' \ | |
-X GET 'https://api.samanage.com/hardwares.xml' | grep '<total_entries' | cut -f2 -d">"|cut -f1 -d"<") | |
# Samanage can only output 100 assets per page | |
# The following, determines the number of pages | |
if [ $(( $TOTAL_ASSETS % 100 )) -eq 0 ] ; then | |
PAGES=$((TOTAL_ASSETS / 100)) | |
else | |
PAGES=$(((TOTAL_ASSETS / 100) + 1)) | |
fi | |
# Output the first 100 assets to XML and overwrite file if it exists | |
curl -H "X-Samanage-Authorization: Bearer $TOKEN" \ | |
-H 'Accept: application/vnd.samanage.v2.1+xml' \ | |
-X GET 'https://api.samanage.com/hardwares.xml?per_page=100&page=1' -o samanageInventory.xml | |
# Loop through the number of pages 100 assets at a time | |
for ((i=1;i<=PAGES;i++)); do | |
curl -H "X-Samanage-Authorization: Bearer $TOKEN" \ | |
-H 'Accept: application/vnd.samanage.v2.1+xml' \ | |
-X GET "https://api.samanage.com/hardwares.xml?per_page=100&page=$i" >> samanageInventory.xml | |
done | |
# Copy all of the MAC addresses to a new file named rawMAC.txt | |
grep '<mac_address' samanageInventory.xml | cut -f2 -d">"|cut -f1 -d"<" > rawMAC.txt | |
# Strip all of the colons (:) from the MAC address and save to a file named finalMAC.txt | |
cat rawMAC.txt | sed -e 's/[:]//g' > finalMAC.txt | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment