Skip to content

Instantly share code, notes, and snippets.

@rjohnsondev
Last active October 1, 2022 03:42
Show Gist options
  • Save rjohnsondev/db50066a8296e80e7019b3feb30b1cb4 to your computer and use it in GitHub Desktop.
Save rjohnsondev/db50066a8296e80e7019b3feb30b1cb4 to your computer and use it in GitHub Desktop.
lastpass cli wrapper for quick and dirty lookup of entries
#!/usr/bin/env python
import sys
import subprocess
import re
import base64
import os
if __name__ == "__main__":
words = sys.argv[1:]
all_entries = subprocess.check_output(['lpass', 'ls']).split(b"\n")
# output : [{name: "wework printing ", id: "3058962232726551787", rank: 1}]
output: [{}]
output = []
for entry in all_entries:
entry = entry.decode("utf-8")
count = 0
for word in words:
p = r'\b{}\b'.format(re.escape(word))
count += sum(1 for _ in re.finditer(p, entry, flags=re.IGNORECASE))
if count > 0:
bits = entry.split("[id: ")
if len(bits) < 2:
continue
entry_id = bits[1][:-1]
output.append({
"name": entry,
"id": entry_id,
"rank": (count, -len(entry))
})
output.sort(key=lambda e: e["rank"])
output.reverse()
for entry in output:
print(entry["name"])
print("*** ------ ***")
for x in range(min(len(output), 1)):
entry_id = output[x]["id"]
out = subprocess.check_output(['lpass', 'show', '--color=always', entry_id])
passwd_search = re.search(br'\x1b\[33mPassword\x1b\[0m: ([^\n]+)\n', out, re.MULTILINE)
if passwd_search:
passwd = passwd_search.group(1)
out = out.replace(passwd, b'********')
data_enc = base64.b64encode(passwd)
buf = b"\033]52;c;" + data_enc + b"\a"
sys.stdout.buffer.write(buf)
sys.stdout.buffer.write(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment