Hi there,
So Im trying to create some python script to put computer into a static group through API, but somehow i cant get this working . I tried to use requests lib, with no luck. Does anyone one got some ideas how to do it?
Hi there,
So Im trying to create some python script to put computer into a static group through API, but somehow i cant get this working . I tried to use requests lib, with no luck. Does anyone one got some ideas how to do it?
Have you checked out GitHub? Jamf has some API scripts, maybe slightly outdated but I know there are others who have some api scripts out there as well. https://github.com/jamf/API_Scripts
This is the API script I use to add computers to static groups. Works pretty well
1#!/bin/sh23#API login info4apiuser="Username"5apipass="Password"6jamfProURL="https://org.jamfcloud.com"78#Grab serial number and OS Version of computer9SerialNumber=$(system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print $NF}')10macOSVersion=$(sw_vers -productVersion)1112#update group with Group ID you want13GroupID="Group ID"14apiURL="JSSResource/computergroups/id/${GroupID}"1516CheckIt=$(echo $macOSVersion | cut -d . -f 1)1718#Add Computer to group based on macOS Version19if [[ "$CheckIt" == "11" ]]20 then2122 GroupName=$(curl -H "Accept: application/xml" -sfku "${apiuser}:${apipass}" -X GET "${jamfProURL}/${apiURL}" | xpath -e '/computer_group/name/text()' 2>/dev/null)2324 else2526 GroupName=$(curl -H "Accept: application/xml" -sfku "${apiuser}:${apipass}" -X GET "${jamfProURL}/${apiURL}" | xpath '/computer_group/name/text()' 2>/dev/null)2728fi2930#XML header stuff31xmlHeader="<?xml version="1.0" encoding="UTF-8" standalone="no"?>"3233#API command to add the serial number to the Static Group34apiData="<computer_group><id>${GroupID}</id><name>${GroupName}</name><computer_additions><computer><serial_number>$SerialNumber</serial_number></computer></computer_additions></computer_group>"3536#Mix it all together37curl -sSkiu ${apiuser}:${apipass} "${jamfProURL}/${apiURL}" 38 -H "Content-Type: text/xml" 39 -d "${xmlHeader}${apiData}" 40 -X PUT > /dev/null4142#Run an Inventory Update43sudo /usr/local/jamf/bin/jamf recon4445sleep 54647exit 0
I solved my issue, thanks for help
@nelsoni and @Sebastian_Marynicz The xmlHeader line should be:
1xmlHeader="<?xml version="1.0" encoding="UTF-8"?>"
Including the standalone
attribute isn't necessary, and can cause problems when making multiple API calls.
I solved my issue, thanks for help
hey @seb-marynicz ,
Im transitioning my script from sh to py and was curious what you did to achieve the move.
theres a postman collection that has the user to static group, but i cant find the computer version.
if you could share, thatd be awesome.
I am looking to add computers to a computer static group using "computer name", can someone please help? It would be much appreciated. I Tried MUT but it failed because it is by default using the 'serial number' which appears that you cannot change.
I am looking to add computers to a computer static group using "computer name", can someone please help? It would be much appreciated. I Tried MUT but it failed because it is by default using the 'serial number' which appears that you cannot change.
here it is in SH
curl -sk -u "$apiUSER":"$apiPASS" -H "Content-Type: text/xml" -d "<?xml version=\\"1.0\\" encoding=\\"ISO-8859-1\\"?><computer_group><computer_additions><computer><id>$id</id></computer></computer_additions></computer_group>" "$jamfURL/JSSResource/computergroups/id/${staticCOMPUTERgroup}" -X PUT > /dev/null
here it is in python
url = f"{jamf_classic_url}/computergroups/id/{jamf_static_computer_group}"
header = {make your header}
payload = f"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>\\n<computer_group>\\n <computer_additions>\\n <computer>\\n <id>{each}</id>\\n </computer>\\n </computer_additions>\\n</computer_group>"
response = requests.put(url, headers=headers, data=payload)
IIRC, the spaces were a PITA for xml and i wanna say either i couldnt find it or this functionality isnt available on Jamf Pro API (could be wrong though, but i took a quick peak at the postman collection and didnt see anything just now either)
here it is in SH
curl -sk -u "$apiUSER":"$apiPASS" -H "Content-Type: text/xml" -d "<?xml version=\\"1.0\\" encoding=\\"ISO-8859-1\\"?><computer_group><computer_additions><computer><id>$id</id></computer></computer_additions></computer_group>" "$jamfURL/JSSResource/computergroups/id/${staticCOMPUTERgroup}" -X PUT > /dev/null
here it is in python
url = f"{jamf_classic_url}/computergroups/id/{jamf_static_computer_group}"
header = {make your header}
payload = f"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>\\n<computer_group>\\n <computer_additions>\\n <computer>\\n <id>{each}</id>\\n </computer>\\n </computer_additions>\\n</computer_group>"
response = requests.put(url, headers=headers, data=payload)
IIRC, the spaces were a PITA for xml and i wanna say either i couldnt find it or this functionality isnt available on Jamf Pro API (could be wrong though, but i took a quick peak at the postman collection and didnt see anything just now either)
Thank you very much for the quick response. But you you expound more on this? I do not know where to begin. I have a list of "Computer Name" in a csv file and i want to add the list of computers to a static group using the attribute "computer name". I do not have the serial numbers for the list of computers.
Thank you very much for the quick response. But you you expound more on this? I do not know where to begin. I have a list of "Computer Name" in a csv file and i want to add the list of computers to a static group using the attribute "computer name". I do not have the serial numbers for the list of computers.
this is based off a script i wrote - im not using mut, so as with most things, you will have to adjust your input accordingly,
based on the examples ive seen and what ive used, it seems like computer_id is either the only way or the ideal way.
eg: if your computername was "cömputer" for example, it would be problematic or at least extra steps to check and convert the characters, let alone the issue of jamf machine names NOT required to be unique.
so however you do it (do some research here), get the computer_ids.
not sure how else to expound of this, but all the stuff in curly braces, {}, are variables due to f-strings.
eg: jamf_static_computer_group is a variable that represents the numerical value (in string) of that group, so mine is "jamf_static_computer_group = "211""
my each variable is just a for loop, where for each in....
each is that computer's computer_id value, eg "972" or something.
ultimately, for each element in my target list,
i run those commands to add them to the specific static group
Hi there,
So Im trying to create some python script to put computer into a static group through API, but somehow i cant get this working . I tried to use requests lib, with no luck. Does anyone one got some ideas how to do it?
Have you checked out GitHub? Jamf has some API scripts, maybe slightly outdated but I know there are others who have some api scripts out there as well. https://github.com/jamf/API_Scripts
This is the API script I use to add computers to static groups. Works pretty well
1#!/bin/sh23#API login info4apiuser="Username"5apipass="Password"6jamfProURL="https://org.jamfcloud.com"78#Grab serial number and OS Version of computer9SerialNumber=$(system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print $NF}')10macOSVersion=$(sw_vers -productVersion)1112#update group with Group ID you want13GroupID="Group ID"14apiURL="JSSResource/computergroups/id/${GroupID}"1516CheckIt=$(echo $macOSVersion | cut -d . -f 1)1718#Add Computer to group based on macOS Version19if [[ "$CheckIt" == "11" ]]20 then2122 GroupName=$(curl -H "Accept: application/xml" -sfku "${apiuser}:${apipass}" -X GET "${jamfProURL}/${apiURL}" | xpath -e '/computer_group/name/text()' 2>/dev/null)2324 else2526 GroupName=$(curl -H "Accept: application/xml" -sfku "${apiuser}:${apipass}" -X GET "${jamfProURL}/${apiURL}" | xpath '/computer_group/name/text()' 2>/dev/null)2728fi2930#XML header stuff31xmlHeader="<?xml version="1.0" encoding="UTF-8" standalone="no"?>"3233#API command to add the serial number to the Static Group34apiData="<computer_group><id>${GroupID}</id><name>${GroupName}</name><computer_additions><computer><serial_number>$SerialNumber</serial_number></computer></computer_additions></computer_group>"3536#Mix it all together37curl -sSkiu ${apiuser}:${apipass} "${jamfProURL}/${apiURL}" 38 -H "Content-Type: text/xml" 39 -d "${xmlHeader}${apiData}" 40 -X PUT > /dev/null4142#Run an Inventory Update43sudo /usr/local/jamf/bin/jamf recon4445sleep 54647exit 0
I solved my issue, thanks for help
@nelsoni and @Sebastian_Marynicz The xmlHeader line should be:
1xmlHeader="<?xml version="1.0" encoding="UTF-8"?>"
Including the standalone
attribute isn't necessary, and can cause problems when making multiple API calls.
I solved my issue, thanks for help
hey @seb-marynicz ,
Im transitioning my script from sh to py and was curious what you did to achieve the move.
theres a postman collection that has the user to static group, but i cant find the computer version.
if you could share, thatd be awesome.
I am looking to add computers to a computer static group using "computer name", can someone please help? It would be much appreciated. I Tried MUT but it failed because it is by default using the 'serial number' which appears that you cannot change.
I am looking to add computers to a computer static group using "computer name", can someone please help? It would be much appreciated. I Tried MUT but it failed because it is by default using the 'serial number' which appears that you cannot change.
here it is in SH
curl -sk -u "$apiUSER":"$apiPASS" -H "Content-Type: text/xml" -d "<?xml version=\\"1.0\\" encoding=\\"ISO-8859-1\\"?><computer_group><computer_additions><computer><id>$id</id></computer></computer_additions></computer_group>" "$jamfURL/JSSResource/computergroups/id/${staticCOMPUTERgroup}" -X PUT > /dev/null
here it is in python
url = f"{jamf_classic_url}/computergroups/id/{jamf_static_computer_group}"
header = {make your header}
payload = f"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>\\n<computer_group>\\n <computer_additions>\\n <computer>\\n <id>{each}</id>\\n </computer>\\n </computer_additions>\\n</computer_group>"
response = requests.put(url, headers=headers, data=payload)
IIRC, the spaces were a PITA for xml and i wanna say either i couldnt find it or this functionality isnt available on Jamf Pro API (could be wrong though, but i took a quick peak at the postman collection and didnt see anything just now either)
here it is in SH
curl -sk -u "$apiUSER":"$apiPASS" -H "Content-Type: text/xml" -d "<?xml version=\\"1.0\\" encoding=\\"ISO-8859-1\\"?><computer_group><computer_additions><computer><id>$id</id></computer></computer_additions></computer_group>" "$jamfURL/JSSResource/computergroups/id/${staticCOMPUTERgroup}" -X PUT > /dev/null
here it is in python
url = f"{jamf_classic_url}/computergroups/id/{jamf_static_computer_group}"
header = {make your header}
payload = f"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>\\n<computer_group>\\n <computer_additions>\\n <computer>\\n <id>{each}</id>\\n </computer>\\n </computer_additions>\\n</computer_group>"
response = requests.put(url, headers=headers, data=payload)
IIRC, the spaces were a PITA for xml and i wanna say either i couldnt find it or this functionality isnt available on Jamf Pro API (could be wrong though, but i took a quick peak at the postman collection and didnt see anything just now either)
Thank you very much for the quick response. But you you expound more on this? I do not know where to begin. I have a list of "Computer Name" in a csv file and i want to add the list of computers to a static group using the attribute "computer name". I do not have the serial numbers for the list of computers.
Thank you very much for the quick response. But you you expound more on this? I do not know where to begin. I have a list of "Computer Name" in a csv file and i want to add the list of computers to a static group using the attribute "computer name". I do not have the serial numbers for the list of computers.
this is based off a script i wrote - im not using mut, so as with most things, you will have to adjust your input accordingly,
based on the examples ive seen and what ive used, it seems like computer_id is either the only way or the ideal way.
eg: if your computername was "cömputer" for example, it would be problematic or at least extra steps to check and convert the characters, let alone the issue of jamf machine names NOT required to be unique.
so however you do it (do some research here), get the computer_ids.
not sure how else to expound of this, but all the stuff in curly braces, {}, are variables due to f-strings.
eg: jamf_static_computer_group is a variable that represents the numerical value (in string) of that group, so mine is "jamf_static_computer_group = "211""
my each variable is just a for loop, where for each in....
each is that computer's computer_id value, eg "972" or something.
ultimately, for each element in my target list,
i run those commands to add them to the specific static group
25 likes
13 likes
11 likes
8 likes
8 likes
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
Sorry, we're still checking this file's contents to make sure it's safe to download. Please try again in a few minutes.
OKSorry, our virus scanner detected that this file isn't safe to download.
OKWe use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.
We use 3 different kinds of cookies. You can choose which cookies you want to accept. We need basic cookies to make this site work, therefore these are the minimum you can select. Learn more about our cookies.