Last active
August 29, 2015 14:20
-
-
Save talkingmoose/1b212f84873a570a1d52 to your computer and use it in GitHub Desktop.
Retrieve JSS asset tags for list of serial numbers
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
#!/bin/bash | |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
# | |
# Written by: William Smith | |
# | |
# Last updated: May 7, 2015 | |
# | |
# Purpose: Retrieves list of asset tags from list of iOS serial numbers. | |
# Serial numbers should be in a serialnumbers.txt file in the same | |
# directory as this script with one serial number per line. | |
# | |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
#################################### | |
# start the timer | |
#################################### | |
# the time right now | |
STARTTIME=$( /bin/date '+%s' ) | |
#################################### | |
# JSS URL and credentials | |
#################################### | |
URL="https://jss.domain.com:8443" | |
USERNAME="JSSAPI" | |
PASSWORD="password" | |
#################################### | |
# File locations | |
#################################### | |
# path to this script | |
CURRENTDIRECTORY=$( /usr/bin/dirname "$0" ) | |
# name of this script | |
CURRENTSCRIPT=$( /usr/bin/basename -s .sh "$0" ) | |
# create log file in same directory as script | |
LOGFILE="$CURRENTDIRECTORY/$CURRENTSCRIPT - $( /bin/date '+%y-%m-%d' ).log" | |
# read the serial number list file | |
SERIALNUMBERLIST=$( cat "$CURRENTDIRECTORY/serialnumbers.txt" ) | |
#################################### | |
# Functions | |
#################################### | |
function logresult() { | |
if [ $? = 0 ] ; then | |
/bin/date "+%Y-%m-%d %H:%M:%S $1" >> "$LOGFILE" | |
else | |
/bin/date "+%Y-%m-%d %H:%M:%S $2" >> "$LOGFILE" | |
fi | |
} | |
#################################### | |
# Rotate logs | |
#################################### | |
# start the log | |
logresult "--------------------- Begin Script ---------------------" | |
# rotate logs -- delete all but the five most recent log files | |
DELETEOLDLOGS=$( /bin/ls -1t "$CURRENTDIRECTORY/$CURRENTSCRIPT"*.log | /usr/bin/tail -n +6 ) | |
while IFS= read -r ALINE | |
do | |
LOGFILENAME=$( /usr/bin/basename "$ALINE" ) | |
/bin/rm "$ALINE" | |
logresult "Deleting old log file: $LOGFILENAME." | |
done <<< "$DELETEOLDLOGS" | |
#################################### | |
# Read each serial number in the | |
# list and retrieve each asset tag | |
# from the JSS. | |
#################################### | |
while IFS= read -r ALINE | |
do | |
DEVICERECORD=$( /usr/bin/curl -k 0 $URL/JSSResource/mobiledevices/serialnumber/$ALINE --user "$USERNAME:$PASSWORD" -H "Content-Type: text/xml" -X GET ) | |
echo "$DEVICERECORD" | |
ASSETTAG=$( /bin/echo "$DEVICERECORD" | /usr/bin/perl -lne 'BEGIN{undef $/} while (/<asset_tag>(.*?)<\/asset_tag>/sg){print $1}' ) | |
echo "$ASSETTAG" | |
# log the result | |
logresult "$ALINE - $ASSETTAG" "$ALINE - Asset tag not found" | |
done <<< "$SERIALNUMBERLIST" | |
#################################### | |
# stop the timer | |
# calculate how long the script ran | |
#################################### | |
logresult "Completing script." | |
# the time right now | |
STOPTIME=$( /bin/date '+%s' ) | |
# subtract start time from stop time and log the time in seconds | |
DIFF=$(($STOPTIME-$STARTTIME)) | |
logresult "Script operations took $DIFF seconds to complete." | |
logresult "---------------------- End Script ---------------------- | |
" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment