193 lines
5.7 KiB
Python
Executable File
193 lines
5.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Bitwarden 密码管理器示例
|
|
"""
|
|
|
|
import subprocess
|
|
import json
|
|
import os
|
|
|
|
# Bitwarden CLI 路径
|
|
BW_CMD = "/usr/local/bin/bw"
|
|
|
|
def bw_login(email, password):
|
|
"""登录 Bitwarden"""
|
|
proc = subprocess.Popen(
|
|
[BW_CMD, "login", email, password],
|
|
stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
text=True
|
|
)
|
|
# 输入密码并等待完成
|
|
stdout, stderr = proc.communicate()
|
|
if proc.returncode == 0:
|
|
print("✅ 登录成功")
|
|
return True
|
|
else:
|
|
print(f"❌ 登录失败: {stderr}")
|
|
return False
|
|
|
|
def bw_unlock(password):
|
|
"""解锁保险库"""
|
|
proc = subprocess.Popen(
|
|
[BW_CMD, "unlock", "--password", password, "--raw"],
|
|
stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
text=True
|
|
)
|
|
stdout, stderr = proc.communicate()
|
|
if proc.returncode == 0:
|
|
session_key = stdout.strip()
|
|
print("✅ 解锁成功")
|
|
return session_key
|
|
else:
|
|
print(f"❌ 解锁失败: {stderr}")
|
|
return None
|
|
|
|
def bw_sync(session):
|
|
"""同步数据"""
|
|
result = subprocess.run(
|
|
[BW_CMD, "sync", "--session", session],
|
|
capture_output=True, text=True
|
|
)
|
|
if result.returncode == 0:
|
|
print("✅ 同步成功")
|
|
return True
|
|
else:
|
|
print(f"❌ 同步失败: {result.stderr}")
|
|
return False
|
|
|
|
def bw_list_items(session):
|
|
"""列出所有项目"""
|
|
result = subprocess.run(
|
|
[BW_CMD, "list", "items", "--session", session],
|
|
capture_output=True, text=True
|
|
)
|
|
if result.returncode == 0:
|
|
try:
|
|
items = json.loads(result.stdout)
|
|
print(f"📋 共 {len(items)} 个项目")
|
|
return items
|
|
except json.JSONDecodeError:
|
|
print("❌ 解析失败")
|
|
return []
|
|
else:
|
|
print(f"❌ 获取失败: {result.stderr}")
|
|
return []
|
|
|
|
def bw_search(session, query):
|
|
"""搜索项目"""
|
|
result = subprocess.run(
|
|
[BW_CMD, "list", "items", "--search", query, "--session", session],
|
|
capture_output=True, text=True
|
|
)
|
|
if result.returncode == 0:
|
|
try:
|
|
items = json.loads(result.stdout)
|
|
print(f"🔍 找到 {len(items)} 个匹配项")
|
|
return items
|
|
except json.JSONDecodeError:
|
|
return []
|
|
else:
|
|
print(f"❌ 搜索失败: {result.stderr}")
|
|
return []
|
|
|
|
def bw_get_password(name, session):
|
|
"""获取密码"""
|
|
result = subprocess.run(
|
|
[BW_CMD, "get", "password", name, "--session", session],
|
|
capture_output=True, text=True
|
|
)
|
|
if result.returncode == 0:
|
|
return result.stdout.strip()
|
|
else:
|
|
print(f"❌ 获取失败: {result.stderr}")
|
|
return None
|
|
|
|
def bw_get_totp(name, session):
|
|
"""获取 TOTP 验证码"""
|
|
result = subprocess.run(
|
|
[BW_CMD, "get", "totp", name, "--session", session],
|
|
capture_output=True, text=True
|
|
)
|
|
if result.returncode == 0:
|
|
return result.stdout.strip()
|
|
else:
|
|
print(f"❌ 获取失败: {result.stderr}")
|
|
return None
|
|
|
|
def main():
|
|
import sys
|
|
|
|
print(f"\n{'='*60}")
|
|
print("🔐 Bitwarden 密码管理器")
|
|
print(f"{'='*60}\n")
|
|
|
|
# 检查是否已登录
|
|
result = subprocess.run(
|
|
[BW_CMD, "status"],
|
|
capture_output=True, text=True
|
|
)
|
|
status = json.loads(result.stdout) if result.stdout else {}
|
|
print(f"当前状态: {status.get('status', 'unknown')}")
|
|
|
|
if len(sys.argv) > 1:
|
|
action = sys.argv[1]
|
|
|
|
if action == "list":
|
|
# 列出所有项目
|
|
password = os.getenv("BW_PASSWORD", "")
|
|
if password:
|
|
session = bw_unlock(password)
|
|
if session:
|
|
items = bw_list_items(session)
|
|
for item in items[:10]:
|
|
print(f" - {item.get('name', 'Unknown')}")
|
|
else:
|
|
print("请设置环境变量 BW_PASSWORD")
|
|
|
|
elif action == "search":
|
|
# 搜索项目
|
|
query = sys.argv[2] if len(sys.argv) > 2 else ""
|
|
password = os.getenv("BW_PASSWORD", "")
|
|
if password:
|
|
session = bw_unlock(password)
|
|
if session:
|
|
items = bw_search(session, query)
|
|
for item in items[:5]:
|
|
print(f" - {item.get('name', 'Unknown')}")
|
|
else:
|
|
print("请设置环境变量 BW_PASSWORD")
|
|
|
|
elif action == "get":
|
|
# 获取密码
|
|
name = sys.argv[2] if len(sys.argv) > 2 else ""
|
|
password = os.getenv("BW_PASSWORD", "")
|
|
if password and name:
|
|
session = bw_unlock(password)
|
|
if session:
|
|
pwd = bw_get_password(name, session)
|
|
if pwd:
|
|
print(f"密码: {pwd}")
|
|
else:
|
|
print("用法: python bitwarden.py get '账号名称'")
|
|
|
|
else:
|
|
print("用法:")
|
|
print(" python bitwarden.py list - 列出所有项目")
|
|
print(" python bitwarden.py search - 搜索项目")
|
|
print(" python bitwarden.py get name - 获取密码")
|
|
|
|
else:
|
|
print("用法:")
|
|
print(" python bitwarden.py list - 列出所有项目")
|
|
print(" python bitwarden.py search query - 搜索项目")
|
|
print(" python bitwarden.py get name - 获取密码")
|
|
print("\n提示:")
|
|
print(" 先设置环境变量: export BW_PASSWORD='你的主密码'")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|