Created
January 27, 2017 22:39
-
-
Save abeyer/92d5723003ac6ebf430aba672f32d1f7 to your computer and use it in GitHub Desktop.
CLI interactive shell for keepass 1 database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from cmd import Cmd | |
from getpass import getpass | |
import sys | |
from kppy.database import KPDBv1 | |
from kppy.exceptions import KPError | |
class KPPYShell(Cmd): | |
def __init__(self, filepath=None): | |
Cmd.__init__(self) | |
if filepath: | |
self.db = KPDBv1(filepath=filepath, read_only=True, password=getpass()) | |
self.db.load() | |
else: | |
self.db = KPDBv1(new=True) | |
self.title = self.db.filepath or '[memory]' | |
self.group = self.db.root_group | |
self.path = ':' | |
self.update_prompt() | |
def update_prompt(self): | |
self.prompt = '(KP) ' + self.title + self.path + '> ' | |
def push_path(self, group): | |
self.group = group | |
self.path = self.path + self.group.title + ':' | |
self.update_prompt() | |
def pop_path(self): | |
if self.group.parent: | |
self.group = self.group.parent | |
self.path = ':'.join(self.path.split(':')[:-2]) + ':' | |
self.update_prompt() | |
def do_unlock(self, args): | |
self.db.unlock(password=getpass()) | |
def do_lock(self, args): | |
self.db.lock() | |
def do_cd(self, args): | |
if args == '.': | |
return | |
elif args == '..': | |
self.pop_path() | |
elif args in [g.title for g in self.group.children]: | |
self.push_path([g for g in self.group.children if g.title == args][0]) | |
else: | |
print 'Group not found ' + args | |
def complete_cd(self, text, line, begin, end): | |
completions = [] | |
completions += [g.title for g in self.group.children if g.title.startswith(text)] | |
if self.group.parent: | |
completions += ['..'] | |
return completions | |
def do_ls(self, args): | |
for child in self.group.children: | |
print child.title + ':' | |
if not self.group.entries and not self.group.children: | |
print '[no entries]' | |
for ent in self.group.entries: | |
print ent.title | |
def do_cat(self, args): | |
if args in [e.title for e in self.group.entries]: | |
ent = [e for e in self.group.entries if e.title == args][0] | |
print ent.title, ent.username, ent.password | |
else: | |
print 'Entry not found ' + args | |
def complete_cat(self, text, line, begin, end): | |
return [e.title for e in self.group.entries if e.title.startswith(text)] | |
def do_EOF(self, args): | |
return True | |
if __name__ == "__main__": | |
f = sys.argv[1] if len(sys.argv) > 1 else None | |
KPPYShell(filepath=f).cmdloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment