105 lines
2.8 KiB
Python
105 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
分布式算力矩阵 · MongoDB 索引优化脚本
|
||
|
||
对 KR 库下 4 个与项目相关的集合补齐/创建推荐索引,便于查询与扫描/破解性能。
|
||
与文档一致:02_账号密码管理/references/MongoDB_分布式算力矩阵_数据库结构说明.md
|
||
"""
|
||
import pymongo
|
||
|
||
MONGO_URI = "mongodb://admin:admin123@localhost:27017/?authSource=admin"
|
||
DB_NAME = "KR"
|
||
|
||
|
||
def ensure_indexes(client: pymongo.MongoClient):
|
||
db = client[DB_NAME]
|
||
|
||
# 1. 分布式矩阵IP
|
||
coll = db["分布式矩阵IP"]
|
||
indexes = [
|
||
[("ip", 1)],
|
||
[("ip_public", 1)],
|
||
[("source_db", 1)],
|
||
[("source_col", 1)],
|
||
[("RFM_total", 1)],
|
||
[("value_level", 1)],
|
||
[("username", 1)],
|
||
[("email", 1)],
|
||
[("region", 1)],
|
||
[("province", 1)],
|
||
[("city", 1)],
|
||
[("phone", 1)],
|
||
[("qq", 1)],
|
||
[("ip", 1), ("source_col", 1)],
|
||
]
|
||
for keys in indexes:
|
||
coll.create_index(keys)
|
||
print(" [OK] 分布式矩阵IP")
|
||
|
||
# 2. 分布式矩阵IP_已扫描
|
||
coll = db["分布式矩阵IP_已扫描"]
|
||
indexes = [
|
||
[("ip", 1)],
|
||
[("ssh_open", 1)],
|
||
[("rdp_open", 1)],
|
||
[("vnc_open", 1)],
|
||
[("telnet_open", 1)],
|
||
[("baota_open", 1)],
|
||
[("ssh_difficulty", 1)],
|
||
[("deploy_score", -1)],
|
||
[("deploy_ready", 1)],
|
||
[("os_guess", 1)],
|
||
[("user_count", -1)],
|
||
[("port_count", -1)],
|
||
[("source_col", 1)],
|
||
[("ssh_open", 1), ("ssh_difficulty", 1)],
|
||
[("deploy_ready", 1), ("deploy_score", -1)],
|
||
[("ssh_open", 1), ("deploy_score", -1)],
|
||
]
|
||
for keys in indexes:
|
||
coll.create_index(keys)
|
||
print(" [OK] 分布式矩阵IP_已扫描")
|
||
|
||
# 3. 分布式矩阵IP_已验证
|
||
coll = db["分布式矩阵IP_已验证"]
|
||
indexes = [
|
||
[("ip", 1)],
|
||
[("ssh_open", 1)],
|
||
[("deploy_score", -1)],
|
||
[("is_honeypot", 1)],
|
||
[("connection_quality", 1)],
|
||
[("source_col", 1)],
|
||
]
|
||
for keys in indexes:
|
||
coll.create_index(keys)
|
||
print(" [OK] 分布式矩阵IP_已验证")
|
||
|
||
# 4. 分布式矩阵IP_已登录
|
||
coll = db["分布式矩阵IP_已登录"]
|
||
indexes = [
|
||
[("ip", 1)],
|
||
[("username", 1)],
|
||
]
|
||
for keys in indexes:
|
||
coll.create_index(keys)
|
||
print(" [OK] 分布式矩阵IP_已登录")
|
||
|
||
|
||
def main():
|
||
print("分布式算力矩阵 · MongoDB 索引优化")
|
||
print("连接:", MONGO_URI)
|
||
client = pymongo.MongoClient(MONGO_URI)
|
||
try:
|
||
client.admin.command("ping")
|
||
except Exception as e:
|
||
print("连接失败:", e)
|
||
return
|
||
print("目标库:", DB_NAME)
|
||
ensure_indexes(client)
|
||
print("完成.")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|