Example 1: Tag systems from a list

You can learn the basics of writing Python scripts using this example and the ePolicy Orchestrator web API. After showing you the entire script, the individual parts of the script are described in detail.

This script assumes:

  • You have a text file, myfile.txt, with a list of managed systems with one system per line and the systems are listed by name or IP address
  • You want to apply a specific tag, in this example myTag, to every system in that list
  • The tag named myTag has already been created
Note: A more robust script would manage those assumptions, but that would complicate the example.

#Example 1
import mcafee
mc = mcafee.client('localhost','8443','ga','ga')

file = open('C:/path/to/myfile.txt', 'r')
for line in file:
	mc.system.applyTag(line.rstrip(‘\n’), 'myTag')

Examine the various script sections in detail

This line in the script imports the provided McAfee Python module (mcafee.py), kept in the same directory as your script.

import mcafee

The next line creates the connection to the McAfee ePO server by specifying the server name, connection port, user name, and password, in that order.

Note: This initialization function can take up to two more parameters that specify the protocol and the presentation of the output.

mc = mcafee.client('localhost','8443','ga','ga')

The full parameter list is:

mc = mcafee.client('yourservername','port','username','password', 'protocol', 'outputtype')

  • The protocol defaults to https on your McAfee ePO server.
  • The outputtype determines the format of output from commands as described in Web API Basics.

With those two lines, you've established a connection to your server.

This line creates your file handle in read-only mode.

file = open('C:/path/to/myfile.txt', 'r')

These lines iterate through the file, run the command system.applyTag to each system in the file, while stripping out the newline ('\n'):

 for line in file:
	mc.system.applyTag(line.rstrip(‘\n’), 'myTag')

After finishing the loop, each system in the file has the tag myTag applied.