# Conflicts: # CHANGELOG.md # README.md # napalm_windows/windows.py # tools/harvest.py
94 lines
2.9 KiB
Python
94 lines
2.9 KiB
Python
"""Record what the driver's PowerShell scripts emit on a real Windows host.
|
|
|
|
python tools/harvest.py <host> <user> <label> [--port 5985] [--insecure]
|
|
|
|
Asks for the password, runs every getter script over one WinRM connection and
|
|
writes the raw JSON to tools/harvest-out/<label>/<fixture>.json — the same file
|
|
names as tests/fixtures/synthetic/. harvest-out/ is gitignored: scrub serials,
|
|
MAC and IP addresses, host names, account names and SIDs before copying anything into
|
|
tests/.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import getpass
|
|
import pathlib
|
|
|
|
from napalm_windows import (
|
|
WindowsDriver,
|
|
host_status,
|
|
login_accounts,
|
|
login_sessions,
|
|
os_users,
|
|
packages,
|
|
processes,
|
|
scheduled_tasks,
|
|
storage,
|
|
system_identity,
|
|
time_sync,
|
|
)
|
|
from napalm_windows import windows as mod
|
|
|
|
SCRIPTS = {
|
|
"facts": mod._PS_FACTS,
|
|
"interfaces": mod._PS_INTERFACES,
|
|
"interfaces_ip": mod._PS_INTERFACES_IP,
|
|
"arp": mod._PS_ARP,
|
|
"routes": mod._PS_ROUTES,
|
|
"services": mod._PS_SERVICES,
|
|
"listening": mod._PS_LISTENING,
|
|
"scheduled_tasks": scheduled_tasks._PS_SCHEDULED_TASKS,
|
|
"time_sync": time_sync._PS_TIME_SYNC,
|
|
"disks": storage._PS_DISKS,
|
|
"storage_pools": storage._PS_STORAGE_POOLS,
|
|
"volumes": storage._PS_VOLUMES,
|
|
"system_identity": system_identity._PS_SYSTEM_IDENTITY,
|
|
"processes": processes._PS_PROCESSES,
|
|
"os_users": os_users._PS_OS_USERS,
|
|
"packages": packages._PS_PACKAGES,
|
|
"host_status": host_status._PS_HOST_STATUS,
|
|
"login_accounts": login_accounts._PS_LOGIN_ACCOUNTS,
|
|
"login_sessions": login_sessions._PS_LOGIN_SESSIONS,
|
|
}
|
|
|
|
OUT = pathlib.Path(__file__).parent / "harvest-out"
|
|
|
|
|
|
def main() -> None:
|
|
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
|
ap.add_argument("host")
|
|
ap.add_argument("user")
|
|
ap.add_argument("label", help="subdirectory name, e.g. server2022 or win11")
|
|
ap.add_argument("--port", type=int, default=5986)
|
|
ap.add_argument("--insecure", action="store_true", help="skip certificate validation")
|
|
args = ap.parse_args()
|
|
|
|
driver = WindowsDriver(
|
|
args.host,
|
|
args.user,
|
|
getpass.getpass(f"Password for {args.user}@{args.host}: "),
|
|
optional_args={"port": args.port, "ssl_verify": not args.insecure},
|
|
)
|
|
target = OUT / args.label
|
|
target.mkdir(parents=True, exist_ok=True)
|
|
|
|
driver.open()
|
|
try:
|
|
for name, script in SCRIPTS.items():
|
|
try:
|
|
raw = driver._run_raw(script)
|
|
except Exception as exc: # record the failure, keep harvesting
|
|
raw = ""
|
|
print(f" {name}: FAILED — {exc}")
|
|
else:
|
|
print(f" {name}: {len(raw)} bytes")
|
|
(target / f"{name}.json").write_text(raw + "\n")
|
|
finally:
|
|
driver.close()
|
|
print(f"written to {target}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|