ipad API restart script not working

rotorstudios
New Contributor

Hi,
We have a script that takes a group of IOS devices and restart all devices in it. The part of the script that gets the iOS devices works but the restart part doesnt work.Any suggestions?
The script looks something like this:

#!/bin/sh
# Define Variables

### JAMF Pro username / password
apiUser="username"
apiPass="password"

### JAMF Pro URL
jssurl="jssname.jamfcloud.com"

### advanced mobile device search ID number
amds_id="10"

# Get Mobile Device IDs from advanced mobile device search. This will retreive all device IDs from advanced mobile device search id 1. Change the id # to retreive from different advanced mobile device search.
mobile_devices=`curl -ksu "$apiUser:$apiPass" -X GET "https://$jssurl/JSSResource/advancedmobiledevicesearches/id/$amds_id" -H "accept: application/xml" | xmllint --xpath "//mobile_devices/mobile_device/id" - | sed 's/</id>/
/g' | sed 's/<id>//g'`

for mobile_device in $mobile_devices
do
  echo "Attempting to send a RestartDevice command to Mobile Device with ID: $mobile_device"
curl -ksu "$apiUser:$apiPass" -X POST "https://$jssurl:8443/JSSResource/mobiledevicecommands/command/RestartDevice/id/$mobile_device" -H "accept: application/xml"

done

exit
5 REPLIES 5

DanielMa
New Contributor III

@rotorstudios embrace awk

mobile_devices=$(curl -ksu "$apiUser:$apiPass" -X GET "$jssurl/JSSResource/advancedmobiledevicesearches/id/$amds_id" -H "accept: application/xml" | xpath //mobile_devices/mobile_device/id | awk -F'<id>|</id>' '{print $2}')

Hope that helps

when in doubt echo out your $mobile_devices to see what you're getting

I also made a change where the url variable includes your https://<serverurl>:8443 makes it easer to change in the long run

rotorstudios
New Contributor

Sorry, the issue is in the RestartDevices command. The part to get mobile_devices work fine in the original script.
Also, it seems that the part to get mobile_devices doesnt need port 8443 but if we do not include port 8443 in the RestartDevice url, it fails with an authentication error.

DanielMa
New Contributor III

Sorry I mis understood,

I recommend having a look at the code example on developer.jamf.com
might give you an idea on combining them both
developer.jamf.com

Regards,

Daniel MacLaughlin

rotorstudios
New Contributor

ok. looks like my script was fine but the user's permission for whatever reason, in the jamf pro server objects under the create colum, the mobile device checkbox needs to be ticked. not sure why or if this is going to be a problem but without this, the script fails/hangs

russeller
Contributor III

I work in K-12 and an app vendor suggested we reboot the devices weekly to resolve some issue the app was having. Awesome. I wrote this script that I run with a launchAgent to execute it weekly to reboot some Mobile Device Smart Groups in the jamfPro server. It has a nested for loops that grabs the device ids out of a group and reboots them all, then continues to the next group.
You can grab the ID of the smart group by looking for it in the URL when viewing the Smart Group.

Like this:

https://company.jss.com:8443/smartMobileDeviceGroups.html?id=711&o=r

So the Group ID in this example would be 711

#!/bin/bash
# written by Steven Russell
# supply the id of the smart groups in the group_id_array variable 
# to have them reboot all the mobile devices in the smart group

jssuser="username"
jsspass="password_here"
jssurl="https://company.jss.com:8443"

## Fill in the Smart Group ID(s) here:
group_id_array=(471 472 2617)
for group_id in "${group_id_array[@]}"; do
    ## Grab the members of the group:
    members_of_group=$(curl -su "${jssuser}":"${jsspass}" "${jssurl}"/JSSResource/mobiledevicegroups/id/${group_id} | xpath //mobile_devices/mobile_device/id | tr '</id>' '
')
    array_id=$(echo $members_of_group | awk '$1=$1' | tr '
' ' ')
    sleep 1 ## slow things down a bit cowboy

    ## Loop through the users and perform the reboot:
    for mobile_id in $array_id; do
        curl -su "${jssuser}":"${jsspass}" "${jssurl}"/JSSResource/mobiledevicecommands/command/RestartDevice/id/"${mobile_id}" -X POST
        sleep 1
    done
done

exit 0