Guest User

Untitled

a guest
Dec 10th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import os
  5. import sys
  6. from ConfigParser import ConfigParser
  7. from subprocess import Popen, PIPE, STDOUT
  8.  
  9.  
  10. # Place your own settings here
  11. filename = os.path.join(os.environ['HOME'], '.mypwfile.gpg')
  12. pipecmd = ('/usr/bin/xclip',)
  13. decryptcmd = ('/usr/bin/gpg', '-q', '--decrypt', 'FILE')
  14.  
  15.  
  16. def make_pwget(filename, pipecmd, decryptcmd):
  17. def pwget(lookupstr):
  18. parser = cfgparser_from_command(decryptcmd, filename)
  19. sections = [x for x in parser.sections()
  20. if x.lower().startswith(lookupstr.lower())]
  21. if sections:
  22. first = sections[0]
  23. credentials = [(k, v.strip("\"'"))
  24. for k, v in parser.items(first)
  25. if k in ('username', 'password')]
  26. if len(credentials) >= 2:
  27. print "Found match:", first
  28. print "Piping to command:", ' '.join(pipecmd)
  29. pipe_to_command(pipecmd, credentials)
  30. else:
  31. print "No username password pair found."
  32. else:
  33. print "No matches found."
  34.  
  35. return pwget
  36.  
  37.  
  38. def pipe_to_command(command, output=None, wait=True):
  39. for k, v in output:
  40. if wait:
  41. print "Press enter to pipe", k
  42. sys.stdin.read(1)
  43. p = Popen(command, stdin=PIPE)
  44. p.communicate(v)
  45.  
  46.  
  47. def cfgparser_from_command(command, filename=''):
  48. if filename:
  49. cmd = [x if x != 'FILE' else filename for x in command]
  50. else:
  51. cmd = command
  52. cfgparser = ConfigParser()
  53. process = Popen(cmd, stdout=PIPE, stderr=STDOUT, close_fds=True)
  54. cfgparser.readfp(process.stdout)
  55. return cfgparser
  56.  
  57.  
  58. if __name__ == '__main__':
  59. pwget = make_pwget(filename, pipecmd, decryptcmd)
  60. if len(sys.argv) > 1:
  61. arg = ' '.join(sys.argv[1:])
  62. pwget(arg)
  63. else:
  64. print """usage: %s ENTRY""" % os.path.basename(sys.argv[0])
Add Comment
Please, Sign In to add comment