Skip to content

Instantly share code, notes, and snippets.

@timsutton
Created August 24, 2016 19:33
Show Gist options
  • Save timsutton/3781d7483b7cdf0358eb86cf28b49e50 to your computer and use it in GitHub Desktop.
Save timsutton/3781d7483b7cdf0358eb86cf28b49e50 to your computer and use it in GitHub Desktop.
Simple script to rename connected iOS devices according to a CSV file, using Apple Configurator 2's cfgutil command.
#!/usr/bin/python
#
# Simple script to batch-rename attached iOS devices according to UUID to name mappings
# in a CSV file.
#
# Usage: rename_devices.py <csvfile>
#
#
# The CSV file should be comma-separated and contain at least the 'udid' and 'name'
# fields. Such a CSV can be exported from Configurator. Any additional field will simply
# be ignored.
#
# "udid","name"
# "0a70436a6d09cea07a70c72cd99ee0441602b5d8","MyOrg 1"
# "0a70436a6d09cea619f7872cd99ee0441602b5e9","MyOrg 2"
import csv
import os
import plistlib
import subprocess
import sys
from pprint import pprint
CFGUTIL = '/Applications/Apple Configurator 2.app/Contents/MacOS/cfgutil'
def main():
if len(sys.argv) != 2:
sys.exit("This tool takes a single argument: the path to the CSV containing the names and UDIDs")
csv_file = sys.argv[1]
if not os.path.exists(csv_file):
sys.exit("Couldn't read CSV file at %s" % csv_file)
# names will be populated with string UDID keys mapping to string user-facing names for the iPads
names = {}
with open(csv_file, 'rb') as csv_fd:
reader = csv.DictReader(csv_fd, delimiter=',')
for row in reader:
names[row['udid']] = row['name']
proc = subprocess.Popen([CFGUTIL, '--format', 'plist', 'list'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = proc.communicate()
try:
ipad_devices = plistlib.readPlistFromString(out)['Output']
except:
sys.exit("Error retrieving devices list with cfgutil")
for ecid, data in ipad_devices.iteritems():
if data['UDID'] in names.keys():
name_to_set = names[data['UDID']]
if name_to_set == data['name']:
print "Device with UDID %s already has the desired name %s, continuing.." % (
data['UDID'], name_to_set)
continue
print "Found matching name for UDID %s: %s - attempting to rename" % (
data['UDID'], name_to_set)
cmd = [CFGUTIL, '--ecid', ecid, '--progress', '-v', 'rename', name_to_set]
print "Executing command: %s" % ' '.join(cmd)
subprocess.call(cmd)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment