Skip to content

Instantly share code, notes, and snippets.

@cmacrae
Created December 30, 2014 23:23
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 cmacrae/96dbeb4840d93cd608c0 to your computer and use it in GitHub Desktop.
Save cmacrae/96dbeb4840d93cd608c0 to your computer and use it in GitHub Desktop.
A more portable version of the offinleimap Python script for use with OS X's keychain
#! /usr/bin/env python
import getpass, re, subprocess
from os.path import expanduser, join
def get_keychain_pass(account=None, server=None):
params = {
'security': '/usr/bin/security',
'command': 'find-internet-password',
'user': getpass.getuser(),
'account': account,
'server': server,
'keychain': join(expanduser('~'), 'library/Keychains/login.keychain'),
}
command = "sudo -u %(user)s %(security)s -v %(command)s -g -a %(account)s -s %(server)s %(keychain)s" % params
output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
outtext = [l for l in output.splitlines()
if l.startswith('password: ')][0]
return re.match(r'password: "(.*)"', outtext).group(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment