Skip to content

Instantly share code, notes, and snippets.

@rjohnsondev
Last active June 16, 2025 06:35
Show Gist options
  • Select an option

  • Save rjohnsondev/4dd6d21c02e81a2e2674bce13382e499 to your computer and use it in GitHub Desktop.

Select an option

Save rjohnsondev/4dd6d21c02e81a2e2674bce13382e499 to your computer and use it in GitHub Desktop.
Little python wrapper for Bitwarden rbw client: perform a substring search on matches to stdin, display full details for top entry, send password straight to clipboard instead of displaying it.
#!/usr/bin/env python
import sys
import subprocess
import re
import base64
if __name__ == "__main__":
words = sys.argv[1:]
all_entries = subprocess.check_output(["rbw", "ls", "--fields=id,name"]).split(b"\n")
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("\t")
if len(bits) < 2:
continue
entry_id = bits[0]
# entry_id = bits[1][:-1]
output.append({"name": bits[1], "id": entry_id, "rank": (count, -len(entry))})
output.sort(key=lambda e: e["rank"])
output.reverse()
for entry in output:
# sys.stdout.buffer.write(entry["name"])
name = (
b"\x1b[90m".decode("unicode-escape")
+ entry["id"]
+ b"\x1b[0m\t".decode("unicode-escape")
+ entry["name"]
)
print(name)
print("*** ------ ***")
for x in output[:1]:
entry_id = output[0]["id"]
name = output[0]["name"].encode("utf-8")
sys.stdout.buffer.write(b"\x1b[95m" + name + b"\x1b[0m\t")
sys.stdout.buffer.write(
b"\x1b[90m["
+ entry_id.encode("utf-8")
+ b"]\x1b[0m\n"
)
result = subprocess.run(
["rbw", "get", entry_id, "--full"],
capture_output=True,
text=True,
)
# print("out" + out)
if "had no password" in result.stderr:
print("No password for entry.")
else:
# first line is the passsword...
out = result.stdout
ix = out.find("\n")
passwd = out[0:ix].encode("utf-8")
rest = out[ix + 1 :].encode("utf-8")
rest = rest.replace(b"Username:", b"\x1b[94mUsername:\x1b[0m")
rest = rest.replace(b"URI:", b"\x1b[33mURI:\x1b[0m")
sys.stdout.buffer.write(rest)
data_enc = base64.b64encode(passwd)
buf = b"\033]52;c;" + data_enc + b"\a"
sys.stdout.buffer.write(buf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment