# __Useful Scripts

# Delete unused subnets

```bash
curl -O https://wissen.woitzel.eu/unused_subnets.py && python3 unused_subnets.py -k
```

#### What it does

Connects to one Prism Central, lists every subnet (with its cluster) that no VM NIC points to and optionally deletes the ones you mark. Python 3 only, no extra modules.

#### Usage

```bash
python3 unused_subnets.py [--host HOST] [--user USER] [-k] [--csv FILE] [--list-only]

# ask for host, user and password
python3 unused_subnets.py

# everything on the command line
python3 unused_subnets.py --host pc.example.local --user admin -k

# read-only: list and save as CSV, never offer to delete
python3 unused_subnets.py --host pc.example.local --user admin -k --list-only --csv unused.csv
```

#### Options

<table id="bkmrk-optiondescription---"><thead><tr><th>Option</th><th>Description</th></tr></thead><tbody><tr><td>`--host HOST`</td><td>Prism Central (host/IP); asked for if missing</td></tr><tr><td>`--user USER`</td><td>User; else `NTNX_USER`, else asked for</td></tr><tr><td>`-k`, `--insecure`</td><td>Do not verify the TLS certificate (self-signed)</td></tr><tr><td>`--csv FILE`</td><td>Also save the list as CSV</td></tr><tr><td>`--list-only`</td><td>Only list, never offer to delete</td></tr><tr><td>`-h`, `--help`</td><td>Show the built-in help</td></tr></tbody></table>

#### Password

Prompted (no echo). Never passed as argument, never stored. Alternatively set `NTNX_PASSWORD` (and `NTNX_USER`).

#### Mark and delete

After the list the script asks *Mark subnets for deletion? \[y/N\]*. Then:

<table id="bkmrk-keyaction-up-%2F-downm"><thead><tr><th>Key</th><th>Action</th></tr></thead><tbody><tr><td>Up / Down</td><td>Move</td></tr><tr><td>Space</td><td>Mark / unmark and move on</td></tr><tr><td>`a`</td><td>Mark all / none</td></tr><tr><td>Enter</td><td>Continue with the marked subnets</td></tr><tr><td>`q`</td><td>Quit without deleting</td></tr></tbody></table>

The marked subnets are shown again and need a final *Delete these N subnet(s) now? \[y/N\]* (default: no). Right before deleting the VMs are read again; a subnet that got a VM in the meantime is skipped. Subnets are deleted one by one, each with OK / FAIL.

#### Notes

- A VM counts whether it is powered on or off. Subnets are matched by UUID, so identical names on different clusters are not mixed up.
- Other consumers of a subnet (Files, Karbon, templates) are not checked. Prism may refuse such a delete - it shows up as FAIL.
- Without a terminal (pipe) or with `--list-only` the script only lists; nothing is deleted.

# Recovery Plan network mapping

```bash
curl -O https://wissen.woitzel.eu/rp_netmap.py && python3 rp_netmap.py
```

#### What it does

Compares the subnets of one or more Prism Centrals by name, creates missing VLANs on both sides and writes the network mapping into a Recovery Plan. Python 3 only, no extra modules. The wizard asks for confirmation before every write; `--apply` is the explicit write switch of the command-line mode.

#### Usage

```bash
# guided wizard (no arguments = wizard)
python3 rp_netmap.py

# report only (read-only): compare ALL subnets of source and target, list the plans
python3 rp_netmap.py --source pc-a.example.local --target pc-b.example.local -k

# show a plan as JSON (check network_mapping_list / cluster references)
python3 rp_netmap.py --source pc-a... --target pc-b... -k --plan "RP-Prod" --dump

# dry run, then write the mapping of ALL matching subnets into the plan
python3 rp_netmap.py --source pc-a... --target pc-b... -k --plan "RP-Prod"
python3 rp_netmap.py --source pc-a... --target pc-b... -k --plan "RP-Prod" --apply
```

#### Wizard flow

1. Number of Prism Centrals (1 is enough if the plans run between clusters of one PC), TLS check, then host, user and password of every PC. A previous run offers *Use the saved values?*.
2. Pick the Recovery Plan (all plans of all PCs, with their clusters).
3. The target Prism Central is read from the plan (its availability zone, or the clusters it names; same PC for plans inside one Prism Central). Only if the plan does not tell it, you are asked.
4. Pick the source and the target cluster. The clusters named in the plan are preselected.
5. Only the subnets of these two clusters are compared: mapped, not found in target, only in target, ambiguous, same name with different VLAN ID.
6. *Create the missing VLANs on both sides?* - summary, then a second confirmation, then the VLANs are created.
7. If every VLAN of the source cluster has a counterpart, the missing entries are offered for the plan's network mapping and written after a confirmation.

Menus: Up / Down, Enter to select, a digit jumps to the entry, Ctrl+C aborts.

#### Options

<table id="bkmrk-optiondescription--i"><thead><tr><th>Option</th><th>Description</th></tr></thead><tbody><tr><td>`-i`, `--interactive`</td><td>Wizard (default when no arguments are given)</td></tr><tr><td>`--source HOST`, `--target HOST`</td><td>Source / target Prism Central for report mode</td></tr><tr><td>`--source-user`, `--target-user`</td><td>Users; else `NTNX_SOURCE_USER` / `NTNX_TARGET_USER` / `NTNX_USER`, else asked for</td></tr><tr><td>`-k`, `--insecure`</td><td>Do not verify the TLS certificate (self-signed)</td></tr><tr><td>`--ignore-case`</td><td>Ignore upper/lower case when matching names</td></tr><tr><td>`--rename REGEX=>REPL`</td><td>Rewrite source names before comparing, e.g. `'^DC1-=>DC2-'` (repeatable)</td></tr><tr><td>`--include REGEX`, `--exclude REGEX`</td><td>Only / not subnets whose name matches</td></tr><tr><td>`--plan NAME`</td><td>Recovery Plan (on the source PC) for `--dump` / dry run / `--apply`</td></tr><tr><td>`--dump`</td><td>Print the plan as JSON (read-only)</td></tr><tr><td>`--apply`</td><td>Write the mapping of all matching subnets into `--plan`</td></tr><tr><td>`--csv FILE`</td><td>Save the subnets without counterpart as CSV</td></tr><tr><td>`-v`, `-vv`</td><td>More output on stderr (info / debug incl. API calls)</td></tr><tr><td>`--log-file FILE`</td><td>Log the run with timestamps to a file</td></tr><tr><td>`--state-file FILE`, `--no-save`</td><td>Other file for the saved inputs / do not load or save them</td></tr><tr><td>`-h`, `--help`</td><td>Show the built-in help</td></tr></tbody></table>

#### Passwords and saved inputs

Passwords are prompted (no echo), never passed as argument and never stored. Alternatively: `NTNX_PASSWORD` (all PCs), `NTNX_SOURCE_PASSWORD` / `NTNX_TARGET_PASSWORD` (first two PCs). Hosts, users and the TLS setting are saved to `./rp_netmap_last_run.json` (chmod 600) in the directory the script is started from.

#### Notes

- Subnets are matched by name. `--rename` only applies to the source side. A name with several hits in the target is "ambiguous" and is never mapped.
- New VLANs are plain VLAN subnets (name + VLAN ID, default virtual switch, no IP address management). The same VLAN ID may exist several times per cluster; only an already used name blocks the creation.
- Existing mapping entries of a plan are kept; only missing pairs are added. An entry that points to a different target network is reported, not changed.
- Plans inside one Prism Central (both sides the same availability zone): the entries are written source first, target second. Check the result in Prism after the first write.
- Report mode compares all subnets and needs exactly one source and one target PC; the wizard handles any number.