On my last post I described how I get my asterisk box to know the caller name from a csv data file. The thing is, my address book keeps changing on my iphone. People change their phone numbers, I meet new people (can you believe it? I don’t let it happen too often though)… I wanted to be able to sync it automatically to my asterisk. This synchronisation also doubles up as a backup for my address book.
This method works on jailbroken iphone version 3. You’d need to setup and enable ssh access to your iphone. There are many resources on how to do it, like this and this.
The process is fairly simple. You can schedule certain tasks or jobs on the iphone. On older version there used to be a cron daemon, but from version 3 – this is all managed by launchd (the Launch Daemon). First lets create a script that syncs the address book file (AddressBook.sqlitedb) to a remote host. We’ll call it address_sync.sh
#!/bin/sh rsync -e ssh /private/var/mobile/Library/AddressBook/AddressBook.sqlitedb firstname.lastname@example.org:/home/iphone/
I’m using a dedicated user called iphone on the remote server, and the script uses rsync over ssh – it will only use a fraction of the bandwidth to transfer the differences between the files (if any). You can use scp instead if you wish.
Trying to run the script, it would prompt us for a password. We don’t want to use a password because it must be run automatically, so we should set up ssh-key access.
This is a two-part process. First part, on the iphone itself. ssh to your iphone as root and use these commands:
user1s-iPhone:~ root# cd ~/.ssh/ user1s-iPhone:~/.ssh root# ssh-keygen -b 1024 -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/var/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /var/root/.ssh/id_rsa. Your public key has been saved in /var/root/.ssh/id_rsa.pub.
Note I used an empty passphrase. Now you’d need to store the contents of id_rsa.pub file (use scp/winscp or simply copy&paste from the terminal).
Go to the remote server we want to allow our iphone key-based access to and add the public key to the authorized_keys file (replace with the contents of your id_rsa.pub file)
root@server:~# cd /home/iphone/.ssh root@server:/home/iphone/.ssh# echo "[contents of the id_rsa.pub]" >>authorized_keys
Test the connection from your iphone:
user1s-iPhone:~/.ssh root# ssh remote.server The authenticity of host 'remote.server (184.108.40.206)' can't be established. RSA key fingerprint is 0c:aa:24:51:2b:61:0f:3e:f2:55:11:ab:a2:e4:61:21. Are you sure you want to continue connecting (yes/no)? yes
All set. Try running the address_sync.sh and check that it copied the file. It should not prompt for password. Put this script under /usr/local/bin
The next part is to schedule this copy to run automatically. In my case, I have decided to run it daily at 1pm. I’ve followed the excellent post about iphone 3 scheduling here and created another file called com.test.syncjob.plist:
<?xml version="1.0" encoding="UTF-8"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>test.syncjob</string> <key>OnDemand</key> <true/> <key>Program</key> <string>/usr/local/bin/address_sync.sh</string> <key>StartCalendarInterval</key> <dict> <key>Hour</key> <integer>13</integer> <key>Minute</key> <integer>0</integer> </dict> </dict> </plist>
Copy this file to /Library/LaunchDaemons on your iphone, and run this command on your iphone:
user1s-iPhone:~ root# launchctl load /Library/LaunchDaemons/com.test.syncjob.plist
All done. Now every day at 1pm, my iphone automatically syncs my address book to my server. That’s great for backups. How do I convert the AddressBook.sqlitedb file to csv for my asterisk box?
I created a cron job on the server that does it automatically. It’s comprised of a shell script and some python code:
#!/bin/sh # converts the sqlite database to csv sqlite3 -csv /home/iphone/AddressBook.sqlitedb "SELECT First, Last, ABMultiValue.value FROM ABPerson, ABMultiValue WHERE ROWID=record_id;" >/home/iphone/AddressBook.csv # converts the csv to a more asterisk-friendly format using a python script (see below) # also removes any email addresses. I only care about phone numbers /home/iphone/convert-csv.py /home/iphone/AddressBook.csv |grep -v @ >/home/iphone/myPhoneBook.csv
Note that I could easily pipe data instead of saving it into files, but I wanted to have a csv file anyway.
#!/usr/bin/env python import csv from sys import argv if len(argv) <= 1: print "usage: %s <filename>" % argv exit() fd = file(argv) for lines in csv.reader(fd): full_name = "%s %s" % (lines.strip(), lines.strip()) print "\"%s\",%s" % (full_name.strip(), lines.replace(" ",""))