-
Notifications
You must be signed in to change notification settings - Fork 22
/
DownloadNetworkSegments.sh
executable file
·123 lines (91 loc) · 4.08 KB
/
DownloadNetworkSegments.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/sh
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Written by: William Smith
# Professional Services Engineer
# Jamf
# https://github.com/talkingmoose/Jamf-Scripts
#
# Originally posted: July 9, 2016
# Last updated: August 13, 2018
#
# Purpose: Downloads each network segment from your Jamf Pro server and
# saves it as an XML file in the JSS_Output directory. When used with
# UploadNetworkSegments.sh, a Jamf Pro administrator can start with a list
# from an old Jamf Pro server, delete unwanted network segment files and
# then upload the remaining files to another Jamf Pro server.
#
# 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) Modify URL, userName and passWord below to access your source Jamf Pro server.
# 2) Save and run this script via Terminal or an editor with a "run script" feature.
# 3) Review the XML files in the JSS_Output folder and Trash any you do not wish to upload to your destination Jamf Pro server.
# 4) Run the UploadNetworkSegments.sh script to populate your destination Jamf Pro server.
URL="https://jamfpro.talkingmoose.net:8443"
userName="API-Auditor"
passWord="password"
# define 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" )
# set the JSS_Output directory in the same directory as script
outputDirectory="$currentDirectory/JSS_Output"
# set the 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 ---------------------"
# create a working directory on the desktop
if [ -d "$outputDirectory" ] ; then
/bin/rm -R "$outputDirectory"
logresult "Removed old $outputDirectory directory." "Failed removing old $outputDirectory directory."
/bin/mkdir -p "$outputDirectory"
logresult "Created new $outputDirectory directory." "Failed creating new $outputDirectory directory."
else
/bin/mkdir -p "$outputDirectory"
logresult "Created $outputDirectory directory." "Failed creating $outputDirectory directory."
fi
# download a list of network segment IDs
nsIDs=$( /usr/bin/curl -k $URL/JSSResource/networksegments --user "$userName:$passWord" -H "Accept: text/xml" -X GET | /usr/bin/perl -lne 'BEGIN{undef $/} while (/<id>(.*?)<\/id>/sg){print $1}' )
logresult "Created Network Segments ID list." "Failed to create Network Segments ID list."
# download XML for each network segment and prepare for upload
while IFS= read aLine
do
# get the full XML for a network segment
ITEMXML=$( /usr/bin/curl -k $URL/JSSResource/networksegments/id/$aLine --user "$userName:$passWord" -H "Accept: text/xml" -X GET | /usr/bin/xmllint --format - )
nsName=$( echo "$ITEMXML" | /usr/bin/awk -F "[><]" '/name/{print $3;exit}' )
logresult "Retrieved XML for network segment \"$nsName\"." "Failed to retrieve XML for network segment \"$nsName\"."
# modify the returned XML and write to a file
echo "$ITEMXML" > "$outputDirectory/$nsName.xml"
logresult "Created XML file for network segment \"$nsName\"." "Failed to create XML file for network segment \"$nsName\"."
downLoad=$((downLoad+1))
done <<< "$nsIDs"
# stop the timer
# calculate how long the script ran
logresult "Completing script."
logresult "Processed $downLoad network segments."
# 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