I started this project to tackle a personal issue. At home, I run a mini server with a bunch of self-hosted services. But sometimes, the power goes out, and since I don’t have a UPS, the server shuts down, and I can’t do anything about it when I’m away.
So I thought—why not use other devices, like old Android phones, to keep an eye on the server and wake it up when the power comes back? That’s how this project was born, and it works pretty well now! It even sends me updates on Telegram when something happens.
- Device Monitoring: Keeps tabs on network devices.
- Power Handling: Detects power return and wakes devices up.
- Telegram Alerts: Sends updates straight to your phone.
- Easy Setup: Simple steps to get started.
- The bot runs on devices in your network (like an old phone).
- If the power goes out, it waits.
- When power is back, it sends a wake-on-LAN signal to turn the server on.
- You get a Telegram message with the node status.
General Advice for Nodes: To ensure stability, it's highly recommended to set up static IP addresses for your nodes via your home router. This way, if anything goes wrong, they will always reconnect using the same IP addresses once everything is back to normal.
The bot uses Google Spreadsheets to store data online, outside the home network, in case of a power failure. Here's how to set it up:
-
Google Cloud Setup:
- Create a project in Google Cloud Platform.
- Generate credentials to let nodes write to the spreadsheet.
- Save the credentials file as
google_credentials.json
in the project folder.
-
Spreadsheet Configuration: The spreadsheet contains several key columns used by the nodes to adjust their behavior:
- NODE_ID: Sequential numbers from 1 to N, identifying each node.
- NODE_LAN_IP: Identifies the node on the network, allowing other nodes to ping it.
- NODE_MAC_ADDRESS: Stores the MAC address of the network interface for nodes that support Wake-on-LAN.
- CHECK: Determines if the node should be contacted for pings and Wake-on-LAN. This is useful for devices like my personal tablet, which I take on trips and disable via a Telegram command.
- WOL: Indicates if the node supports Wake-on-LAN.
The spreadsheet must be uploaded to Google Drive, and sharing permissions should be set to allow anyone with the link to edit, ensuring that all nodes can access and update it.
The initial configuration might seem tricky, but I've included Telegram bot commands to simplify adjustments later.
-
Node Data Writing:
- Each node logs its status in the spreadsheet.
- To avoid Google API limits, nodes write at different intervals.
This method works for now, but I plan to move away from Google to something more flexible.
- Install Python (make sure it’s added to the PATH).
- Clone the project:
git clone https://github.com/Taoshan98/network-node-manager-bot.git
- Move into the project directory:
cd network-node-manager-bot
- Set up a virtual environment:
python3 -m venv .venv source .venv/bin/activate # Android/Linux/macOS .venv\Scripts\activate # Windows
- Install dependencies:
pip install -r requirements.txt
- Place the
google_credentials.json
file in the correct directory. - On Android, macOS, and Linux, run the following command to make the start script executable:
Note: On macOS and Linux, use
chmod +x start.sh
sudo
to run the command.
- Set up a Telegram bot via BotFather.
- Save your new Telegram Token for next steps.
The bot comes with several commands to help manage nodes:
/reboot - Reboots the node.
/enable - Enables the contacted node to be verified.
/disable - Disables the contacted node to prevent it from being verified.
/enable_nodes - Enables the nodes sent as arguments from another node.
/disable_nodes - Disables the nodes sent as arguments from another node.
/check - Checks the operation of the node.
When creating the bot in BotFather, you can copy and paste this list to have all commands readily available in the chat. I personally added the nodes to a group to send the same command to all bots and get all responses in a single chat.
- Configure the .env file with the following:
TELEGRAM_TOKEN=your-telegram-token TELEGRAM_MAIN_GROUP=chat-id-or-group-id NODE_ID=node-id NODE_HAS_BATTERY=node-battery-for-laptop-or-smartphones integer 1-0 SPREADSHEET_URL=google-spreadsheet-url-like https://docs.google.com/spreadsheets/d/1 MAIN_SHEET_ID=main-sheet-id-usually-0 NODE_SHEET_ID=node-sheet-id NODE_DEBUG=debug-mode integer 1-0
Follow the same steps but with some extra setup:
Pro Tip: Typing commands on a phone can be tricky. Use SSH from your PC instead! Change the password with passwd
,
run sshd
, and follow this guide.
- Make sure your device runs Android >= 6.0.
- Install Termux from F-Droid (don’t use the Play Store version as it's outdated). Download from here.
- Get Termux-API from F-Droid to access phone features (like battery status).
- Open Termux and execute:
pkg update && pkg upgrade termux-setup-storage pkg install cronie termux-api python clang cmake make libffi-dev openssl-dev libxml2 libxslt ninja autoconf automake build-essential libtool patchelf
Pro Tip: On Android I recommend to install pip packages using the -v option, because it can happen that the device is not very performant so it looks like it froze.
Just run command like this:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt -v
chmod +x start.sh
./start.sh
To keep the bot running smoothly, set up a cron job:
* * * * * cd /path/to/network-node-manager-bot && python3 nnm_check.py
On Windows, use Task Scheduler to automate running the bot:
- Open Task Scheduler and create a new task.
- Set it to run at startup or at regular intervals.
- Choose "Start a program" and set the path to
python.exe
. - Add arguments to run the bot, e.g.,
C:\path\to\network-node-manager-bot\nnm_check.py
. - Save and activate the task.
This runs the script every minute, ensuring everything stays monitored.
- Better logging and analytics.
- Support for more messaging platforms.
- A self-hosted data storage alternative to Google.