#!/usr/bin/env python

import dbus
import subprocess
import sys
import os

# Replace this with the bluetooth address of the Bluetooth Network Access Point
dev_bdaddr = '12_34_56_AB_CD_EF'

dhcp_clients = [
  ['/sbin/dhclient', '-v', '-d'],
  ['/sbin/udhcpc', '-f', '-i'],
]

def main():
  bus = dbus.SystemBus()
  bluez_proxy = bus.get_object('org.bluez', '/')
  bluez_manager = dbus.Interface(bluez_proxy, 'org.bluez.Manager')
  adapter = bluez_manager.DefaultAdapter()
  # adapter_proxy = bus.get_object('org.bluez', adapter)
  dev_proxy = bus.get_object('org.bluez', '%s/dev_%s' % (adapter, dev_bdaddr))
  adapter_network = dbus.Interface(dev_proxy, 'org.bluez.Network')
  # adapter_introspect = dbus.Interface(dev_proxy, 'org.freedesktop.DBus.Introspectable')
  # print adapter_introspect.Introspect()
  # print adapter_network.Disconnect()
  net_interface = adapter_network.Connect('NAP') # 'GN' / 'NAP' ?
  print '%s created' % net_interface

  dhcp = None
  try:
    for dhcp_client in dhcp_clients:
      if os.path.exists(dhcp_client[0]):
        dhcp = subprocess.Popen(dhcp_client + [net_interface], stdout=sys.stdout)
    if dhcp is None:
      print 'Unable to locate DHCP Client'

    raw_input('Press enter to close connection\n')

  finally:
    if dhcp is not None:
      dhcp.kill()
      dhcp.wait()

if __name__ == '__main__':
  main()

@@ -71,28 +71,38 @@
  os%0A
-%0A# Replace this with
+import optparse%0A%0A# Either pass
  the
@@ -131,170 +131,1753 @@
 the 
-Bluetooth N
+n
 etwork 
-A
+a
 ccess 
-P
+p
 oint
-%0Adev_bdaddr = '12_34_56_AB_CD_EF'%0A%0Adhcp_clients = %5B%0A  %5B'/sbin/dhclient', '-v', '-d'%5D,%0A  %5B'/sbin/udhcpc', '-f', '-i'%5D,%0A%5D%0A%0Adef main():
+ to connect to%0A# with the -b flag, or store it in the config file in the form:%0A# bdaddr = 00:11:22:AA:BB:CC%0Adef_config_file = '~/.blue-tether'%0A%0Adhcp_clients = %5B%0A  %5B'/sbin/dhclient', '-v', '-d'%5D,%0A  %5B'/sbin/udhcpc', '-f', '-i'%5D,%0A%5D%0A%0Adef process_config_file(opts):%0A  file = os.path.expanduser(opts.config)%0A  if not os.path.isfile(file):%0A    return%0A  with open(file, 'r') as f:%0A    for line in f:%0A      line = line.split('#', 1)%5B0%5D.strip()%0A      if line == '':%0A        continue%0A      try:%0A        (opt, val) = map(str.strip, line.split('=', 1))%0A      except ValueError:%0A        raise optparse.OptionValueError('Badly formatted line in condfig file: %25s' %25 line)%0A      try:%0A        if getattr(opts, opt) == None:%0A          setattr(opts, opt, val)%0A      except AttributeError:%0A        raise optparse.OptionValueError('Unknown option in config file: %25s' %25 opt)%0A%0Adef get_config():%0A  def check_bdaddr():%0A    import re%0A    if re.match('(%5B0-9a-fA-F%5D%7B2%7D(:(?=.)%7C$))%7B6%7D$', opts.bdaddr) is None:%0A      raise optparse.OptionValueError('bdaddr in wrong format')%0A%0A  parser = optparse.OptionParser()%0A  parser.add_option('-b', '--bdaddr',%0A    help='Bluetooth address of the network access point to connect to')%0A  # Coming soon:%0A  # parser.add_option('-r', '--reconnect', type='int', metavar='SECONDS',%0A  #   help='Attempt to reconnect every SECONDS if the network goes down')%0A  parser.add_option('-c', '--config', default=def_config_file,%0A    help='Process this config file (%25default)')%0A%0A  (opts, args) = parser.parse_args()%0A%0A  if len(args):%0A    parser.error('Too many arguments')%0A%0A  try:%0A    process_config_file(opts)%0A    check_bdaddr()%0A  except optparse.OptionValueError, e:%0A    parser.error(e)%0A%0A  return opts%0A%0Adef main():%0A  opts = get_config()%0A
 %0A  b
@@ -2184,18 +2184,37 @@
 er, 
-dev_
+opts.
 bdaddr
+.replace(':', '_')
 ))%0A 
@@ -2961,8 +2961,35 @@
  main()%0A
+%0A# vim:expandtab:ts=2:sw=2%0A

Add option parsing + config file