Advertisement
Guest User

Untitled

a guest
May 28th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. Script for dumping bluetooth pairings from OS X to a registry file, for Windows import.
  4. This will allow you to have your bluetooth devices paired with both operating systems at the same time.
  5.  
  6. In case of problems with Windows registry entries: pair your device with windows first, then with OS X,
  7. and do the dump and import.
  8. """
  9. __author__ = 'pawelszydlo@gmail.com'
  10.  
  11. import os
  12. import plistlib
  13. import re
  14. import sys
  15. import subprocess
  16.  
  17.  
  18. def _choose_one(options, what='one'):
  19. """Force user to choose an option if more than one available."""
  20. chosen = None
  21. if not options:
  22. return None
  23. elif len(options) > 1:
  24. print >> sys.stdout, 'Choose %s:' % what
  25. for number, line in enumerate(options):
  26. print >> sys.stdout, '%d. %s' % (number + 1, line)
  27. while chosen is None or chosen < 0 or chosen >= len(options):
  28. chosen = raw_input('Choose (%d - %d): ' % (number + 1, len(options)))
  29. try:
  30. chosen = int(chosen) - 1
  31. except:
  32. chosen = None
  33. else:
  34. chosen = 0
  35. return options[chosen]
  36.  
  37.  
  38. def _run_command(command):
  39. """Run a shell command."""
  40. p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  41. output = '\n'.join(p.stdout.readlines())
  42. retval = p.wait()
  43. return retval, output
  44.  
  45.  
  46. def _get_pairs(xml_data):
  47. """Extract host device id and pairings from plist xml."""
  48. plist = plistlib.readPlistFromString(xml_data)
  49. keys_root = plist.get('LinkKeys')
  50. if not keys_root:
  51. print >> sys.stderr, 'Key LinkKeys not found in blued.plist.'
  52. return None, []
  53. hosts = keys_root.keys()
  54. if not hosts:
  55. print >> sys.stderr, 'No bluetooth hosts found in blued.plist.'
  56. return None, []
  57. host = _choose_one(hosts, 'bluetooth host device')
  58. print >> sys.stdout, 'Using bluetooth host device %s...' % host
  59. pairs = []
  60. for device_id, device_key in keys_root.get(host, {}).items():
  61. device_key = device_key.data.encode('hex_codec')
  62. pairs.append([device_id, device_key])
  63. return host, pairs
  64.  
  65.  
  66. def _write_reg_file(host_id, pair):
  67. """Write the pairing into a Windows registry file."""
  68. host_id = host_id.replace('-', '')
  69. device_id = pair[0].replace('-', '')
  70. key = pair[1]
  71. # For windows, the key needs to be reversed and written as comma separated list of bytes
  72. key = ','.join(reversed([key[i:i + 2] for i in range(0, len(key), 2)]))
  73.  
  74. reg_file = open('bt_pair_%s.reg' % device_id, 'w')
  75. reg_file.write('Windows Registry Editor Version 5.00\r\n\r\n')
  76. reg_file.write('[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\BTHPORT\Parameters\Keys\%s]\r\n' % host_id)
  77. reg_file.write('"%s"=hex:%s\r\n' % (device_id, key))
  78. reg_file.close()
  79.  
  80.  
  81. if __name__ == '__main__':
  82. # This script should be run as root
  83. if os.geteuid() != 0:
  84. print >> sys.stderr, 'You must run this script as root. Try:\nsudo %s' % os.path.basename(__file__)
  85. sys.exit(1)
  86.  
  87. # Get the keys from blued.plist
  88. status, xml_data = _run_command('plutil -convert xml1 -o - /private/var/root/Library/Preferences/blued.plist')
  89. if status != 0:
  90. print >> sys.stderr, 'Cannot convert binary blued plist into xml.\n"%s"' % xml_data
  91. sys.exit(2)
  92. host_id, pairs = _get_pairs(xml_data)
  93. if not pairs:
  94. print >> sys.stderr, 'No pairings found for host device %s.' % host_id
  95. sys.exit(3)
  96.  
  97. # Choose which pair to dump
  98. chosen = _choose_one(pairs, 'pairing')
  99.  
  100. # Dump the selected pair to registry file
  101. print >> sys.stdout, 'Dumping pairing (%s) to registry file...' % ' = '.join(chosen)
  102. _write_reg_file(host_id, chosen)
  103.  
  104. print >> sys.stdout, 'Done.'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement