Skip to content

Instantly share code, notes, and snippets.

@talkingmoose
Created September 13, 2019 18:33
Show Gist options
  • Select an option

  • Save talkingmoose/a6d4629398491dcd67d7be0eddf2a638 to your computer and use it in GitHub Desktop.

Select an option

Save talkingmoose/a6d4629398491dcd67d7be0eddf2a638 to your computer and use it in GitHub Desktop.
Creates network segments in Jamf Pro from a supplied CSV file.
#!/bin/sh
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Written by: William Smith
# Professional Services Engineer
# Jamf
# [email protected]
#
# Originally posted: September 13, 2019
#
# Purpose: Uploads network segments defined in a CSV file to Jamf Pro.
#
# The script creates a log file in the same folder as the script.
#
# Except where otherwise noted, this work is licensed under
# http://creativecommons.org/licenses/by/4.0/
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# INSTRUCTIONS
# 1) Create a CSV file with the following columns: name,starting_address,ending_address,distribution_point,url,netboot_server,swu_server,building,department,override_buildings,override_departments
# 2) Place the CSV file in same location as this script.
# 3) Modify URL, userName and passWord below.
# 4) Run the script.
# the time right now
startTime=$( /bin/date '+%s' )
URL="https://jamfpro.talkingmoose.net:8443"
userName="API-Editor"
passWord="password"
# create the output directory and log file
# in the same directory as this script
# path to this script
currentDirectory=$( /usr/bin/dirname "$0" )
# name of this script
currentScript=$( /usr/bin/basename -s .sh "$0" )
# create the JSS_Output directory in the same directory as script
networkCSV="$HOME/Desktop/subnets.csv"
# create log file in same directory as script
logFile="$currentDirectory/$currentScript - $( /bin/date '+%y-%m-%d' ).log"
# 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
}
# the time right now
startTime=$( /bin/date '+%s' )
# start the log
logresult "--------------------- Begin Script ---------------------"
networkSegmentsList=$( /bin/cat "$networkCSV" )
# upload XML files to create network segments
while IFS= read aLine
do
name=$( awk -F, '{ print $1 }' <<< $aLine )
starting_address=$( awk -F, '{ print $2 }' <<< $aLine )
ending_address=$( awk -F, '{ print $3 }' <<< $aLine )
url=$( awk -F, '{ print $5 }' <<< $aLine )
bulding=$( awk -F, '{ print $8 }' <<< $aLine )
department=$( awk -F, '{ print $9 }' <<< $aLine )
override_buildings=$( awk -F, '{ print $10 }' <<< $aLine )
override_departments=$( awk -F, '{ print $11 }' <<< $aLine )
# read the XML file and remove formatting
theXML="<network_segment><name>$name</name><starting_address>$starting_address</starting_address><ending_address>$ending_address</ending_address><url>$url</url><building>$building</building><department>$department</department><override_buildings>$override_buildings</override_buildings><override_departments>$override_departments</override_departments></network_segment>"
/usr/bin/curl -k $URL/JSSResource/networksegments --user "$userName:$passWord" -H "Content-Type: text/xml" -X POST -d "$theXML"
logresult "Uploaded XML file \"$aLine\"" "Failed to upload XML file \"$aLine\""
upload=$((upload+1))
done <<< "$networkSegmentsList"
# stop the timer
# calculate how long the script ran
logresult "Completing script."
logresult "Processed $upload network segment XML files."
# 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