This article describes one technique that can be used with BigFix to run a Python program on device managed by BigFix, i.e. those where the BigFix Agent is installed. BigFix can drive the execution of any scripting language provided that the interpreter is present on the device where the script must run. The interpreter could itself be managed via BigFix, e.g. its installation, but that part is beyond the scope of this short article.
The sample Python program is intentionally very easy to focus the description on how to embed it in a Fixlet and not to the intricacies of Python programming. The overall approach is to include the Python program in the body of a Fixlet, then invoke the Python interpreter to run it and report results of execution to the BigFix Server.
The operating system used here is Windows but the same results can be achieved on other operating system by adapting the part specific to Windows to the other OS.
The Python Program
The picture below shows the sample program used for demonstration.
It simply creates a file and writes a simple line to it. It however shows that indentation works and captures the exit code of the program.
Lines 6 to 18 check if an argument was passed in, in which case it’ll be the full pathname of the file to create. Otherwise the default name will be used which is set on line 3. For simplicity no check is done whether the pathname is a valid one, if the directory exists, etc. That part will not add much to the goal of this article.
Lines 20 to 22 are used to create the file and write a line of test to it and the last statement is to return an exit code. In a real program one would manage the different exist codes to indicate what error was encountered, if any.
Wrapping the Program in a Fixlet
With the sample Python program at hands let’s go through the steps needed to run it through BigFix on any managed device.
Step 1: Create a custom Fixlet
Open the WebUI, go to Apps, then Custom and click on the Create Custom Content button at the top right of the page. You are now in the page to create a Fixlet, so give it a name and a description, you should have something like the picture below:
Step 2: Define the Action to run
Move down to the page, leave Action Script selected and click the Action 1 tab. After that add two statements like in the picture below. The first line is to remove the file containing the program and the second to begin a section in BigFix to host the Python Program. See the picture below for what you should have, and remember to select it as the Default Action.
Put the content of your Python program below the last line above, then move to the end of the program you pasted in and add the following lines:
As a last step specify the success criteria for the Action, all lines of the Action script should succeed in our case.
Step 3: Identify the devices where the program can run
We want the program to run only on devices running Windows and we say so in the Relevance Tab. Click on the Relevance tab and express the need to only apply to devices where the name of the operating system contains “Win” and we don’t care case.
That’s really all that is needed!
As typical with BigFix the Fixlet must reside in a Site and the managed devices must be subscribed to that site. In this example the custom Site name is “A Site for Python” and the subscription is such that only devices with Windows OS are subscribed to it. Select it and finally click on the Save button.
Running the program and checking results
We are all set now to run the Python program. Let’s go back to the Custom App in the WebUI, filter by Site (“A Site for Python “ for our case) and select the Fixlet we just created. In the simple deployment used for this article there’s only one device, but there could be many and the flow will not change.
- Select Fixlet and Click Deploy
- Select Device
Move to Next on the Deploy Custom Content dialog and select the device where you want to run the program
- Run.
Click Next and then Deploy, and the program will run on the selected device.
- Check Result
The Status will go to Running and then Fixed and you’ll see something like the picture below:
Let’s check what happened on the device now.
Text file created in the destination directory with the expected content.
The Python program is still in the directory and will be overwritten on the next run. This was done on purpose to help troubleshooting any potential problem while developing the automation. In production environments the file will most probably be in a different directory and removed once its execution is complete.
This encapsulation technique can be used for other programming languages as well. One could encapsulate JavaScript if node.js is available or installed on the device. Also, Perl and variations of Shell scripting can be encapsulated using a similar approach as described in this wiki article.
Summing up
This short article focused on running a Python program on BigFix managed endpoints. All it takes is to add few lines around the program to embed it in a BigFix Action within a Fixlet. Although the example was kept simple to focus on the steps required to run it via BigFix, a more elaborate extension would be to take user input when the Action is submitted and pass it to the Python program. Also, once could make the python program more versatile by dealing with OS variations, checking if Python is installed on the endpoint, and installing it if necessary.
With BigFix, Developers have the freedom to choose whatever approach they like for automation, including Python, PowerShell, Perl, JavaScript or other scripting languages. Developers can leverage their knowledge of these programming languages within BigFix and even repurpose existing code.