Source

pythonwise / find-pass.py

#!/usr/bin/env python

from subprocess import check_output, STDOUT, CalledProcessError
from sys import platform
import re

# password: "XXXXX"
find_passwd = re.compile('password: "([^"]+)"').search
#     "acct"<blob>="miki.tebeka@gmail.com"
find_user = re.compile('"acct"<blob>="([^"]+)"').search


def find_key(fn, out):
    match = fn(out)
    return match and match.group(1)


def main(argv=None):
    import sys
    from argparse import ArgumentParser

    argv = argv or sys.argv

    parser = ArgumentParser(description='')
    parser.add_argument('domain')
    args = parser.parse_args(argv[1:])

    if platform != 'darwin':
        raise SystemExit('error: {} works only on OSX'.format(parser.prog))

    domain = args.domain

    cmd = [
        'security',
        'find-internet-password',
        '-g',
        '-s', domain,
    ]
    try:
        out = check_output(cmd, stderr=STDOUT)
    except CalledProcessError:
        raise SystemExit('error: not password for {}'.format(domain))

    user = find_key(find_user, out)
    if not user:
        raise SystemExit('error: cannot find user for {}'.format(domain))
    print(user)

    passwd = find_key(find_passwd, out)
    if not passwd:
        raise SystemExit('error: cannot find password for {}'.format(domain))
    print(passwd)


if __name__ == '__main__':
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.