Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import os
- import sys
- from ConfigParser import ConfigParser
- from subprocess import Popen, PIPE, STDOUT
- # Place your own settings here
- filename = os.path.join(os.environ['HOME'], '.mypwfile.gpg')
- pipecmd = ('/usr/bin/xclip',)
- decryptcmd = ('/usr/bin/gpg', '-q', '--decrypt', 'FILE')
- def make_pwget(filename, pipecmd, decryptcmd):
- def pwget(lookupstr):
- parser = cfgparser_from_command(decryptcmd, filename)
- sections = [x for x in parser.sections()
- if x.lower().startswith(lookupstr.lower())]
- if sections:
- first = sections[0]
- credentials = [(k, v.strip("\"'"))
- for k, v in parser.items(first)
- if k in ('username', 'password')]
- if len(credentials) >= 2:
- print "Found match:", first
- print "Piping to command:", ' '.join(pipecmd)
- pipe_to_command(pipecmd, credentials)
- else:
- print "No username password pair found."
- else:
- print "No matches found."
- return pwget
- def pipe_to_command(command, output=None, wait=True):
- for k, v in output:
- if wait:
- print "Press enter to pipe", k
- sys.stdin.read(1)
- p = Popen(command, stdin=PIPE)
- p.communicate(v)
- def cfgparser_from_command(command, filename=''):
- if filename:
- cmd = [x if x != 'FILE' else filename for x in command]
- else:
- cmd = command
- cfgparser = ConfigParser()
- process = Popen(cmd, stdout=PIPE, stderr=STDOUT, close_fds=True)
- cfgparser.readfp(process.stdout)
- return cfgparser
- if __name__ == '__main__':
- pwget = make_pwget(filename, pipecmd, decryptcmd)
- if len(sys.argv) > 1:
- arg = ' '.join(sys.argv[1:])
- pwget(arg)
- else:
- print """usage: %s ENTRY""" % os.path.basename(sys.argv[0])
Add Comment
Please, Sign In to add comment