Example 4: Import computers from external sources

You can use this script to take a comma-delimited file containing system information, and import those systems into a specified McAfee ePO server System Tree group.

This script assumes that there are these two source files:

  • myfile.txt — Contains the System Tree group where the systems are added
  • systemsToAdd.txt — Contains one system per line with comma-delimited properties in this order: MAC address, IP address, system name, and domain name

#Example 4
import mcafee, sys
mc = mcafee.client('localhost','8443','ga','ga','https','json')

file = open('C:/myfile.txt', 'r')
for line in file:
  #determine the ID of the group to add it to
  groups = mc.system.findGroups(line.rstrip('\n'))
  groupId = -1
  for group in groups:
    if group['groupPath'] == 'My Organization\\' + line.rstrip('\n'):
    groupId = group['groupId']
                                
    if groupId == -1:
      error = 'Error finding the specified group.'
      sys.exit(error)

#now that we have the group id, pull in the systems from file
sourceId = "12"
sourceType = "CLI"
file = open('C:/systemsToAdd.txt', 'r')
for line in file:
  sysProps = line.rstrip('\n').split(',')
  # Contains line break at "\"
  systemId = mc.detectedsystem.add(sourceId,sourceType,sysProps[0],sysProps[1],dnsName= \
  sysProps[2],do main=sysProps[3])
  mc.detectedsystem.addToTree(str(systemId),str(groupId))

This example first determine where to add the systems using the system.findGroups() command searches for the group by name and, using that name, obtains the group ID.

The systems are added to the group with the detectedsystem.add command. This command has these optional parameters:

detectedsystem.add sourceID sourceType MAC IPAddress [IPSubnet][IPSubnetMask][dnsName] [OSPlatform] [OSFamily] [OSVersion]
[domain] [netbiosName][netbiosComment] [users] [agentGUID] [detectedTime] [externalID]

You can add optional parameters in order (using any values for parameters you aren't providing). These optional parameters are, for example:

mc.detectedsystem.add(sourceId, sourceType,sysProps[0], sysProps[1], "0.0.0.0", "0.0.0.0", sysProps[2], '', '', '', sysProps[3])

You can also assign parameters by name with the dnsName and domainName parameters in this script.

Note: The parameters sourceID and sourceType are arbitrary values defined when you add the systems. These parameters are stored in the database to record what source detected, or added, any given system.

The return value of this command is the ID of the newly added detected system. You use this ID as input to this command () which adds detected systems to the System Tree.

detectedsystem.addToTree UIDs branchNodeID [allowDuplicates] [dirSort]

By default, the system is not added if it's a duplicate and it is not automatically sorted, but you can override these defaults if you want. This script accepts the defaults and, using the newly obtained detected system ID and the group ID, moves this system into the System Tree.