Python script that calls jamfHelper binary - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python script that calls jamfHelper binary (/thread-17975.html) Pages:
1
2
|
RE: Python script that calls jamfHelper binary - kennethdean2010 - May-04-2019 Is there a way I could just add a counter to loop through the following code X amount of times: def display_prompt(): """Displays prompt to allow user to schedule update installation Args: None Returns: (int) defer_seconds: Number of seconds user wishes to defer policy OR None if an error occurs """ cmd = [JAMFHELPER, '-windowType', 'hud', '-title', GUI_WINDOW_TITLE, '-heading', GUI_HEADING, '-icon', GUI_ICON, '-description', GUI_MESSAGE, '-button1', GUI_BUTTON, '-showDelayOptions', ' '.join(GUI_DEFER_OPTIONS), '- '] error_values = ['2', '3', '239', '243', '250', '255'] # Instead of returning an error code to stderr, jamfHelper always returns 0 # and possibly returns an 'error value' to stdout. This makes it somewhat # spotty to check for some deferrment values including 0 for 'Start Now'. # The return value is an integer, so leading zeroes are dropped. Selecting # 'Start Now' should technically return '01'; instead, only '1' is returned # which matches the 'error value' for 'The Jamf Helper was unable to launch' # All we can do is make sure the subprocess doesn't raise an error, then # assume (yikes!) a return value of '1' equates to 'Start Now' try: proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate() # Check that the return value does not represent an 'error value' if not out in error_values: # Special case for 'Start Now' which returns '1' if out == '1': return 0 else: return int(out[:-1]) else: return None except: # Catch possible CalledProcessError and OSError print "An error occurred when displaying the user prompt." return NoneBut at the specified intervals: GUI_DEFER_OPTIONS = ["300", "0", "7200", "14400", "28000"] RE: Python script that calls jamfHelper binary - SheeppOSU - May-04-2019 (May-04-2019, 08:00 PM)kennethdean2010 Wrote: But at the specified intervals:You want it to loop only at specific intervals? Like this? - count = 0 while True: count += 1 display_prompt() if str(count) in GUI_DEFER_OPTIONS: for x in range(1, loopTimes + 1): display_prompt() RE: Python script that calls jamfHelper binary - kennethdean2010 - May-05-2019 (May-04-2019, 09:04 PM)SheeppOSU Wrote:(May-04-2019, 08:00 PM)kennethdean2010 Wrote: But at the specified intervals:You want it to loop only at specific intervals? Yes exactly. Now I am thinking this is what I am looking for a while loop to loop through until no more options. That make sense. Thanks SheepOSU... I have been crash coursing Python this weekend using "Automate the Boring Stuff with Python" by Al Sweigart on Udemy plus referencing the companion book at Safari Online. I will toy with this and see how it goes. I really appreciate the help. I think I may have it from here. Thanks for showing me the way @sheeppOSU!!!! :) RE: Python script that calls jamfHelper binary - SheeppOSU - May-05-2019 no problem, that's why I am here RE: Python script that calls jamfHelper binary - kennethdean2010 - May-05-2019 Ok so the code below just keeps looping with 5 minutes still as an option, does not wait 5 mins either it just keeps looping what am I doing wrong with that counter? I am definately on the right track thanks to you SheeppOSU!!! :) The Jamf Helper Gui is displaying multiple times just not as intended. def display_prompt(): """Displays prompt to allow user to schedule update installation Args: None Returns: (int) defer_seconds: Number of seconds user wishes to defer policy OR None if an error occurs """ #counter to loop through display_prompt X amount of times. cmd = [JAMFHELPER, '-windowType', 'hud', '-title', GUI_WINDOW_TITLE, '-heading', GUI_HEADING, '-icon', GUI_ICON, '-description', GUI_MESSAGE, '-button1', GUI_BUTTON, '-showDelayOptions', ' '.join(GUI_DEFER_OPTIONS), '- '] error_values = ['2', '3', '239', '243', '250', '255'] # Instead of returning an error code to stderr, jamfHelper always returns 0 # and possibly returns an 'error value' to stdout. This makes it somewhat # spotty to check for some deferrment values including 0 for 'Start Now'. # The return value is an integer, so leading zeroes are dropped. Selecting # 'Start Now' should technically return '01'; instead, only '1' is returned # which matches the 'error value' for 'The Jamf Helper was unable to launch' # All we can do is make sure the subprocess doesn't raise an error, then # assume (yikes!) a return value of '1' equates to 'Start Now' try: proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate() # Check that the return value does not represent an 'error value' if not out in error_values: # Special case for 'Start Now' which returns '1' if out == '1': return 0 else: return int(out[:-1]) else: return None except: # Catch possible CalledProcessError and OSError print "An error occurred when displaying the user prompt." return None count = 0 while True: count += 1 display_prompt() if str(count) in GUI_DEFER_OPTIONS: for x in range(1, loopTimes + 1): display_prompt() RE: Python script that calls jamfHelper binary - SheeppOSU - May-05-2019 (May-05-2019, 05:49 AM)kennethdean2010 Wrote: it just keeps loopingThat's because there is no break. You need to break it eventually. Here's an example of how it is uses - import random loopTimes = random.randint(1, 10) while True: for x in range(1, loopTimes + 1): if x == loopTimes + 1: break #Breaks out of the while loopCould implement break after something reaches a specific value RE: Python script that calls jamfHelper binary - kennethdean2010 - May-05-2019 lol doh... of course. Thanks!!! RE: Python script that calls jamfHelper binary - kennethdean2010 - May-06-2019 (May-05-2019, 07:26 AM)SheeppOSU Wrote:(May-05-2019, 05:49 AM)kennethdean2010 Wrote: it just keeps loopingThat's because there is no break. You need to break it eventually. Here's an example of how it is uses -import random loopTimes = random.randint(1, 10) while True: for x in range(1, loopTimes + 1): if x == loopTimes + 1: break #Breaks out of the while loopCould implement break after something reaches a specific value Ok so how would place the counter into the code: def display_prompt(): """Displays prompt to allow user to schedule update installation Args: None Returns: (int) defer_seconds: Number of seconds user wishes to defer policy OR None if an error occurs """ #counter to loop through display_prompt X amount of times. cmd = [JAMFHELPER, '-windowType', 'hud', '-title', GUI_WINDOW_TITLE, '-heading', GUI_HEADING, '-icon', GUI_ICON, '-description', GUI_MESSAGE, '-button1', GUI_BUTTON, '-showDelayOptions', ' '.join(GUI_DEFER_OPTIONS), '- '] error_values = ['2', '3', '239', '243', '250', '255'] # Instead of returning an error code to stderr, jamfHelper always returns 0 # and possibly returns an 'error value' to stdout. This makes it somewhat # spotty to check for some deferrment values including 0 for 'Start Now'. # The return value is an integer, so leading zeroes are dropped. Selecting # 'Start Now' should technically return '01'; instead, only '1' is returned # which matches the 'error value' for 'The Jamf Helper was unable to launch' # All we can do is make sure the subprocess doesn't raise an error, then # assume (yikes!) a return value of '1' equates to 'Start Now' try: proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate() # Check that the return value does not represent an 'error value' if not out in error_values: # Special case for 'Start Now' which returns '1' if out == '1': return 0 else: return int(out[:-1]) else: return None except: # Catch possible CalledProcessError and OSError print "An error occurred when displaying the user prompt." count = 0 return None count = 0 while True: count += 1 display_prompt() if str(count) in GUI_DEFER_OPTIONS: for x in range(1, loopTimes + 1): def display_prompt() breakRight now I have it below the def display_prompt() function with a break :) however it just does the update after I select the 5 min deferral option. It does not re-prompt with 2 4 & 8 hours + the Start Now option. RE: Python script that calls jamfHelper binary - SheeppOSU - May-07-2019 I don't know if you pasted it wrong or something but - 1. You have def where it seems you are trying to call the function 2. Since the while loop is in the display_prompt, it is not called and it will keep playing over and over if called RE: Python script that calls jamfHelper binary - kennethdean2010 - May-07-2019 (May-07-2019, 01:43 AM)SheeppOSU Wrote: I don't know if you pasted it wrong or something but - 1. You have def where it seems you are trying to call the function 2. Since the while loop is in the display_prompt, it is not called and it will keep playing over and over if called Actually, I wasn't using the updated code you sent me. With loopTimes = random.randint(1, 10). I am adding that outside the function and trying it now. I added import random at the top of my script. Thanks again man!!! |