This script monitors SSH connection attempts (both successful and failed) from /var/log/auth.log and sends notifications via ntfy or any other notification service. You can specify which types of connection attempts to monitor and receive notifications for.
- Monitors SSH logs (
/var/log/auth.logby default). - Sends notifications for:
- Successful SSH logins (High priority).
- Failed SSH logins (Low priority).
- SSH disconnections (Normal priority).
- Optionally sends a notification only for certain types of events, configurable via environment variables.
- Prevents duplicate notifications for the same event.
- Supports dynamic notification headers and token handling.
- Can be run continuously in the background, monitoring new log entries.
- Python 3.x
requestslibrary (for sending notifications)python-dotenvlibrary (for environment variable management)
-
Clone the repository or download the script.
git clone https://github.com/kinouzero/monitor-ssh.git cd monitor-ssh -
Install the required dependencies:
pip install -r requirements.txt
-
Create a
.envfile in the project directory. The file should contain the following environment variables:NTFY_URL=https://ntfy.sh/topic # URL of the notification service (default: https://ntfy.sh/topic) NTFY_TOKEN=your_token_here # Optional: Authentication token for the notification service MONITOR_EVENTS=all # Comma-separated list of events to monitor (choices: accepted, failed, disconnected, all) SSH_LOG_FILE=your/log/path # Optional: Path to your log (default: /var/log/auth.log)
You can customize the
MONITOR_EVENTSvariable to choose which events to monitor:accepted- Successful logins (default for "all")failed- Failed logins (default for "all")disconnected- Disconnections (default for "all")all- Monitors all types of events (default)
-
Run the script:
python main.py
You can run this SSH monitor inside a Docker container.
docker build -t ssh-monitor .docker run -d \
--name ssh-monitor \
--env NTFY_URL="https://ntfy.sh/topic" \
--env MONITOR_EVENTS="all" \
-v /var/log:/app/log \
ssh-monitorThis will run the monitor in the background, using environment variables to configure notifications.
If you prefer docker-compose, create a docker-compose.yml file:
version: '3.8'
services:
ssh-monitor:
image: ssh-monitor
build: .
restart: unless-stopped
environment:
- NTFY_URL=https://ntfy.sh/topic
- MONITOR_EVENTS=all
volumes:
- /var/log:/app/logdocker-compose up -ddocker-compose downThis setup ensures the container restarts automatically and keeps logs accessible.
You can configure the script to run as a service using systemd (on Linux systems):
-
Create a
monitor-ssh.servicefile in/etc/systemd/system/with the following content:[Unit] Description=SSH Connection Monitor After=network.target [Service] ExecStart=/usr/bin/python3 /path/to/monitor-ssh.py WorkingDirectory=/path/to/your/script Restart=always User=your-user Group=your-group [Install] WantedBy=multi-user.target
-
Reload
systemdand start the service:sudo systemctl daemon-reload sudo systemctl enable monitor-ssh.service sudo systemctl start monitor-ssh.service
The following environment variables can be configured in the .env file:
- NTFY_URL: The notification URL (default:
https://ntfy.sh/topic). - NTFY_TOKEN: The optional authentication token for the notification service.
- MONITOR_EVENTS: Comma-separated list of events to monitor (
accepted,failed,disconnected,all). Default isall. - SSH_LOG_FILE: Path to your log file (default script:
/var/log/auth.log, default docker:/app/log/auth.log).
NTFY_URL=https://ntfy.sh/topic
NTFY_TOKEN=your_token_here
MONITOR_EVENTS=accepted,failed
SSH_LOG_FILE=your/log/pathThe script logs its activities to /tmp/monitor-ssh.log by default.
This project is licensed under the MIT License - see the LICENSE file for details.