2024-11-29 09:29:35 +01:00
> [!NOTE]
> *If you experience issues or have suggestions, submit an issue! https://github.com/DaanSelen/meshbook/issues I'll respond ASAP!*
2024-11-27 16:41:34 +01:00
# Meshbook
2025-01-09 10:26:15 +01:00
A way to programmatically manage MeshCentral-managed machines, inspired by applications like [Ansible ](https://github.com/ansible/ansible ).<br>
What problem does it solve? Well, what I wanted to be able to do is to automate system updates through [MeshCentral ](https://github.com/ylianst/meshcentral ). And some machines are behind unmanaged or 3rd party managed firewalls.<br>
2024-11-27 19:24:15 +01:00
And many people will be comfortable with YAML configurations! It's almost like JSON, but different!<br>
# Quick-start:
The quickest way to start is to grab a template from the templates folder in this repository.<br>
2025-01-09 10:26:06 +01:00
Make sure to correctly pass the MeshCentral websocket API as `wss://<MeshCentral-Host>` .<br>
2025-02-11 16:27:30 +01:00
And make sure to fill in the credentails of an account which has `Remote Commands` , `Details` and `Agent Console` permissions on the targeted devices or groups.<br>
2024-11-29 09:27:38 +01:00
2025-02-11 16:27:30 +01:00
> I did this through a "Service account" with rights on the device group.
2024-11-29 09:27:38 +01:00
2024-11-27 20:20:22 +01:00
Then make a yaml with a target and some commands! See below examples as a guideline. And do not forget to look at the bottom's notice.<br>
To install, follow the following commands:<br>
2024-11-29 09:27:38 +01:00
### Linux setup:
2025-02-13 13:44:23 +01:00
```bash
2024-11-28 09:49:29 +01:00
git clone https://github.com/daanselen/meshbook
2024-11-27 20:28:52 +01:00
cd ./meshbook
2024-11-27 20:20:22 +01:00
python3 -m venv ./venv
source ./venv/bin/activate
2025-01-09 10:26:15 +01:00
pip3 install -r ./requirements.txt
2025-02-13 13:44:23 +01:00
cp ./templates/meshcentral.conf.template ./meshcentral.conf
2024-11-27 20:20:22 +01:00
```
2024-11-29 09:27:38 +01:00
2025-02-13 13:44:23 +01:00
### Windows setup (PowerShell, not cmd):
2024-11-29 09:27:38 +01:00
```shell
git clone https://github.com/daanselen/meshbook
cd ./meshbook
2025-02-13 13:44:23 +01:00
python -m venv ./venv # or python3 when done through the Microsoft Store.
2024-11-29 09:27:38 +01:00
.\venv\Scripts\activate # Make sure to check the terminal prefix.
2025-01-09 10:26:15 +01:00
pip3 install -r ./requirements.txt
2025-02-13 13:44:23 +01:00
cp .\templates\meshcentral.conf.template .\meshcentral.conf
2024-11-29 09:27:38 +01:00
```
2025-02-13 13:44:23 +01:00
Now copy the configuration template from ./templates and fill it in with the correct details (remove .template from the file) this is shown in the last step of the setup(s).<br>
The url should start with `wss://` .<br>
You can check pre-made examples in the examples directory, make sure the values are set to your situation.<br>
2024-11-28 09:53:54 +01:00
After this you can use meshbook, for example:
2024-11-29 09:27:38 +01:00
### Linux run:
2025-02-13 13:44:23 +01:00
```bash
2025-01-09 10:26:15 +01:00
python3 .\meshbook.py -pb .\examples\echo.yaml
2024-11-29 09:27:38 +01:00
```
### Windows run:
2024-11-27 20:20:22 +01:00
```shell
2025-02-13 13:44:23 +01:00
.\venv\Scripts\python.exe .\meshbook.py -pb .\examples\echo_example.yaml
2024-11-27 20:20:22 +01:00
```
2024-11-27 16:41:34 +01:00
2024-11-29 09:27:38 +01:00
### How to check if everything is okay?
2024-11-29 09:29:35 +01:00
The python virtual environment can get messed up, therefore...<br>
2024-11-29 09:27:38 +01:00
To check if everything is in working order, make sure that the lists from the following commands are aligned:
```
python3 -m pip list
pip3 list
```
If not, perhaps you are using the wrong executable, the wrong environment and so on...
# How to create a configuration?
This paragraph explains how the program interprets certain information.
### Targeting:
2025-01-09 10:26:15 +01:00
MeshCentral has `meshes` or `groups` , in this program they are called `group(s)` . Because of the way I designed this.<br>
2024-11-29 09:27:38 +01:00
So to target for example a mesh/group in MeshCentral called: "Nerthus" do:
> If your group has multiple words, then you need to use `"` to group the words.
```yaml
---
name: example configuration
2025-01-09 10:26:15 +01:00
group: "Nerthus"
2025-02-27 21:47:55 +01:00
#target_os: "Linux" # <--- according to os_categories.json.
powershell: True # <--- this can be important for Windows clients.
2024-11-29 09:27:38 +01:00
variables:
- name: var1
value: "This is the first variable"
tasks:
- name: echo the first variable!
command: 'echo "{{ var1 }}"'
```
2025-01-09 10:26:15 +01:00
It is also possible to target a single device, as seen in: [here ](./examples/apt_update_example.yaml ).<br>
2024-11-29 09:27:38 +01:00
### Variables:
2025-01-09 10:26:15 +01:00
Variables are done by replacing the placeholders just before the runtime (the Python program does this, not you).<br>
2024-11-29 09:27:38 +01:00
So if you have var1 declared, then the value of that declaration is placed wherever it finds {{ var1 }}.<br>
2025-01-09 10:26:15 +01:00
This is done to imitate popular methods. See below [from the example ](./examples/variable_usage_example.yaml ).<br>
2024-11-29 09:27:38 +01:00
### Tasks:
The tasks you want to run should be contained under the `tasks:` with two fields, `name` and `command` .<br>
The name field is for the user of meshbook, to clarify what the following command does in a summary.<br>
The command field actually gets executed on the end-point.<br>
2025-02-27 21:47:55 +01:00
### Windows Client Extra-information:
If you want to launch commands at Windows machines, make sure you have your `os_categories.conf` up-to-date with the correct reported Windows versions.<br>
And then make sure to create compatible commands, see: [windows examples ](./examples/windows )<br>
Related is the yaml option: `powershell: True` .
2025-02-13 12:29:06 +01:00
### Granual Operating System filtering:
2025-01-31 23:29:02 +01:00
2025-02-13 12:29:06 +01:00
I have made the program so it can have a filter with the Operating systems. If you have a mixed group, please read:
[This explanation ](./docs/operating_system_filtering.md )
2025-01-31 23:29:02 +01:00
2025-03-27 11:20:51 +01:00
### Tag filtering:
Filtering on MeshCentral tags is also possible with `target_tag` inside the meshbook. This string is case-sensitive, lower- and uppercase must match.<br>
This is done because its human made and therefor needs to be keps well administrated.
2024-11-27 16:41:34 +01:00
# Example:
2024-11-29 09:27:38 +01:00
For the example, I used the following yaml file (you can find more in [this directory ](./examples/ )):
2024-11-27 19:24:15 +01:00
2025-01-09 10:26:06 +01:00
The below group: `Dev` has three devices, of which one is offline, Meshbook checks if the device is reachable.<br>
2024-11-27 19:24:15 +01:00
You can expand the command chain as follows:<br>
```yaml
---
2025-01-09 10:26:06 +01:00
name: Echo a string to the terminal through the meshbook example.
group: "Dev"
2025-02-13 13:44:23 +01:00
#target_os: "Linux" # <--- according to os_categories.json
2024-11-28 09:49:29 +01:00
variables:
2025-01-09 10:26:06 +01:00
- name: file
value: "/etc/os-release"
2024-11-27 19:24:15 +01:00
tasks:
2025-01-09 10:26:06 +01:00
- name: Echo!
command: "echo $(cat {{ file }})"
2024-11-27 19:24:15 +01:00
```
2025-01-09 10:26:15 +01:00
The following response it received when executing the first yaml of the above files (without the `-s` parameters, which just outputs the below JSON).
2024-11-27 19:24:15 +01:00
2024-11-27 16:41:34 +01:00
```shell
2025-02-13 13:44:23 +01:00
~/meshbook$ python3 meshbook.py -pb examples/echo_example.yaml
----------------------------------------
Playbook: examples/echo_example.yaml
Operating System Categorisation file: ./os_categories.json
Congiguration file: ./meshcentral.conf
Target group: Development
Grace: True
Silent: False
2025-01-09 10:26:06 +01:00
----------------------------------------
Trying to load the MeshCentral account credential file...
Trying to load the Playbook yaml file and compile it into something workable...
2025-02-13 13:44:23 +01:00
Trying to load the Operating System categorisation JSON file...
2025-01-09 10:26:06 +01:00
Connecting to MeshCentral and establish a session using variables from previous credential file.
Generating group list with nodes and reference the targets from that.
----------------------------------------
2025-02-13 13:44:23 +01:00
Executing playbook on the target(s): Development.
Initiating grace-period...
1...
2...
3...
----------------------------------------
2025-01-09 10:26:06 +01:00
1. Running: Echo!
----------------------------------------
2025-02-13 13:44:23 +01:00
{"Task 1": "ALL THE DATA"} # Not sharing due to PID
2024-11-27 16:41:34 +01:00
```
2025-01-09 10:26:06 +01:00
The above without `-s` is quite verbose. use `--help` to read about parameters and getting a minimal response for example.
2024-11-27 19:24:15 +01:00
# Important Notice:
If you want to use this, make sure to use `NON-BLOCKING` commands. MeshCentral does not work if you send it commands that wait.<br>
2025-01-09 10:26:15 +01:00
A couple examples of `BLOCKING COMMANDS` which will never get back to the main MeshCentral server, and Meshbook will quit after the timeout but the agent will not come back:
2024-11-27 19:24:15 +01:00
```shell
apt upgrade # without -y.
2024-11-27 16:41:34 +01:00
2024-11-27 19:24:15 +01:00
sleep infinity
2024-11-27 16:41:34 +01:00
2024-11-27 19:24:15 +01:00
ping 1.1.1.1 # without a -c flag (because it pings forever).
2024-11-27 20:28:52 +01:00
```