Skip to content

Instantly share code, notes, and snippets.

@jfinstrom
Created July 10, 2014 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfinstrom/57a49991320acdc7dcb9 to your computer and use it in GitHub Desktop.
Save jfinstrom/57a49991320acdc7dcb9 to your computer and use it in GitHub Desktop.
Dump Asterisk Phonebook to json as an interchange format
#!/usr/bin/env python
import subprocess
import json
callerid = {}
speeddial = {}
p = subprocess.Popen(["asterisk", "-rx", "database show"], stdout=subprocess.PIPE)
out = p.communicate()
out = out[0].splitlines()
for line in out:
if line.startswith('/cidname'):
key,value = line.split(':')
key = key.strip()
key = key.split('/')[2]
value = value.strip()
callerid[key] = value
if line.startswith('/sysspeeddials'):
key,value = line.split(':')
key = key.strip()
key = key.split('/')[2]
value = value.strip()
speeddial[value] = key
output = []
for number,name in callerid.items():
output.append({"entry" : {"full name" : name, "phone":number, "speeddial" : speeddial.get(number)}})
jsonout = json.dumps(output)
print jsonout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment