Posted on 06-06-2024 02:25 AM
Hi everyone,
I'm encountering an issue when running a shell script via JAMF. The script only works if it is written as a one-liner. When I try to run the same script in its normal, multi-line format, JAMF throws errors. Interestingly, if I save the script as a file and execute it, it works perfectly. Additionally, JAMF seems to dislike empty lines in the script and throws errors related to them.
Here is a snippet from my Script which fails:
to_json_array(){
local list=("$1")
local array="["
while IFS= read -r item; do
item=$(printf "%s" "$item" | sed 's/"/\\"/g')
array+="\"$item\", "
done <<< "$list"
array="${array%, }]"
echo "$array"
}
And that's the error to this:
1:233: execution error: /Library/Application Support/ZuluDesk Scripting/com.zuludesk.scripting.60a1e7ef-234b-11ef-8da8-024f99ac106f/com.zuludesk.scripting.60a1e7ef-234b-11ef-8da8-024f99ac106f.command: line 25: syntax error near unexpected token `{
'
/Library/Application Support/ZuluDesk Scripting/com.zuludesk.scripting.60a1e7ef-234b-11ef-8da8-024f99ac106f/com.zuludesk.scripting.60a1e7ef-234b-11ef-8da8-024f99ac106f.command: line 25: `to_json_array(){
' (2)
Has anyone else encountered similar issues with multi-line scripts in JAMF? Is there a known workaround or best practice to avoid these errors?
Thanks in advance for any insights or suggestions!
Posted on 06-06-2024 06:09 AM
I have never really had any issues with Jamf perse, but I have had issues where Jamf will act wonky with scripts using specific interpreters. For example, a script will work fine locally with bash or zsh, but will only work with bash from Jamf.
Does the script run as a .sh locally without issues? Can you share the rest of the script?
Posted on 06-06-2024 08:15 AM
Sure. That's the full Script:
#!/bin/sh
API_URL="https://XXXXXXXXXXXX/api/metrics"
deviceName=$(hostname)
deviceSerial=$(system_profiler SPHardwareDataType | awk -F ": " '/Serial/ {print $2}')
modelIdentifier=$(system_profiler SPHardwareDataType | awk -F ": " '/Model Identifier/ {print $2}')
deviceIPs=$(ifconfig | awk '/inet / && !/127.0.0.1/ {print $2}')
loggedInUsers=$(last | grep "logged in" | awk '{print $1}' | sort | uniq)
lastNetwork="N/A"
lastSignalStrength="N/A"
lastNetworkChannel="N/A"
networkInterface=$(route get default | awk '/interface/ {print $2}')
networkInfo=$(networksetup -listallhardwareports | grep -C1 "$networkInterface")
if echo "$networkInfo" | grep -q "Wi-Fi"; then
system_profiler -xml > "/tmp/systemProfiler.xml"
xpath_lastNetwork='string(//dict[key[text()="spairport_current_network_information"]]/dict/key[text()="_name"]/following-sibling::string[1])'
xpath_lastSignalStrength='string(//dict[key[text()="spairport_current_network_information"]]/dict/key[text()="spairport_signal_noise"]/following-sibling::string[1])'
xpath_lastNetworkChannel='string(//dict[key[text()="spairport_current_network_information"]]/dict/key[text()="spairport_network_channel"]/following-sibling::string[1])'
lastNetwork=$(xmllint --xpath "$xpath_lastNetwork" "/tmp/systemProfiler.xml")
lastSignalStrength=$(xmllint --xpath "$xpath_lastSignalStrength" "/tmp/systemProfiler.xml")
lastNetworkChannel=$(xmllint --xpath "$xpath_lastNetworkChannel" "/tmp/systemProfiler.xml")
rm /tmp/systemProfiler.xml
else
lastNetwork="Ethernet"
fi
to_json_array() {
local list=("$1")
local array="["
while IFS= read -r item; do
item=$(printf "%s" "$item" | sed 's/"/\\"/g')
array+="\"$item\", "
done <<< "$list"
array="${array%, }]"
echo "$array"
}
echo "Device Name: $deviceName"
echo "Device Serial: $deviceSerial"
echo "Model Identifier: $modelIdentifier"
echo "Device IPs: $(to_json_array "${deviceIPs[@]}")"
echo "Logged In Users: $(to_json_array "${loggedInUsers[@]}")"
echo "Last Network: $lastNetwork"
echo "Last Signal Strength: $lastSignalStrength"
echo "Last Network Channel: $lastNetworkChannel"
echo ""
payload=$(cat <<EOF
{
"deviceName": "$deviceName",
"deviceSerial": "$deviceSerial",
"deviceType": "$modelIdentifier",
"lastIPs": $(to_json_array "${deviceIPs[@]}"),
"lastUsers": $(to_json_array "${loggedInUsers[@]}"),
"lastSignalStrength": "$lastSignalStrength",
"lastNetworkChannel": "$lastNetworkChannel",
"lastNetwork": "$lastNetwork"
}
EOF
)
export CURL_SSL_BACKEND=secure-transport
curl -k -X POST \
--cert "Mac-Client" \
-H "Content-Type: application/json" \
-d "$payload" $API_URL
The Script works without any Problems local as .sh file. But might be that the Problem is the sh shell and JAMF expects bash syntax. Not sure if the Syntax for bash is any different tho