Telegram bot to manage and run commands on SSH servers.
- Servers: Add, edit, delete; IP/hostname and credentials validated; passwords stored encrypted.
- Connection: One active connection per user; connect from list or direct; idle timeout.
- Commands: Run commands on the connected server; interactive support; dangerous commands blocked; length limits.
- Presets: Save and run frequent commands.
- Access: Admin-only by default; optional public mode; data per user.
- Python 3.9 or higher
- PostgreSQL
- A Telegram bot (from @BotFather)
- Clone or download the project
cd telegram-ssh-bot- Create virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies
pip install -r requirements.txt- Setup PostgreSQL database
# Create database
createdb telegram_ssh_bot
# Or using psql
psql -U postgres
CREATE DATABASE telegram_ssh_bot;- Configure environment variables
Copy .env.example and rename to .env:
cp .env.example .envThen edit .env file:
# Telegram Bot Configuration
TELEGRAM_TOKEN=your_bot_token_here
# Admin Configuration (comma-separated user IDs)
ADMIN_IDS=123456789,987654321
# Database Configuration
DATABASE_URL=postgresql://user:password@localhost:5432/telegram_ssh_bot
# Security Settings
MASTER_ENCRYPTION_KEY=your_master_key_here_32_chars_minimum
# Bot Settings
COMMAND_TIMEOUT=300
CONNECTION_TIMEOUT=1800
MAX_COMMAND_LENGTH=1000
RATE_LIMIT_PER_MINUTE=30
# Optional: thread pool size (default 15; increase on powerful servers)
# THREAD_POOL_MAX_WORKERS=15Important Notes:
TELEGRAM_TOKEN: Get bot token from @BotFatherADMIN_IDS: Numeric admin IDs separated by comma (get from @userinfobot)MASTER_ENCRYPTION_KEY: A random string at least 32 characters for encryptionDATABASE_URL: PostgreSQL connection address
- Run the bot
python bot.py- Find the bot in Telegram and send
/start - Select options from the main menu
- From main menu, select "🖥️ Server Management"
- Select "➕ Add Server"
- Enter server information in order:
- Server name
- IP address or Hostname
- Port (default: 22)
- Username
- Password
- From "🖥️ Server Management" menu, select "🔌 Connect"
- Select desired server from list
- After successful connection, you can execute commands
- From main menu, select "⚡ Execute Command"
- Enter the command
- Command output will be displayed
- From main menu, select "📝 Preset Commands"
- You can add frequently used commands
- For quick execution, use preset commands list
Tuning (slow bot or low RAM): see PERFORMANCE.md. You can set THREAD_POOL_MAX_WORKERS in .env (default 15).
To enable bot for all users:
- Admin should use main menu (or
/admincommand) - Select "🌐 Public Mode"
- Bot will be active for all users
Passwords are encrypted (AES-256-GCM). Commands are validated and dangerous ones (e.g. rm -rf /, mkfs, dd if=) are blocked. Input is sanitized; rate limit and timeouts apply.
telegram-ssh-bot/
├── bot.py # Main entry point
├── config/
│ ├── __init__.py
│ └── settings.py # Settings management
├── database/
│ ├── __init__.py
│ ├── models.py # Database models
│ └── connection.py # Connection management
├── security/
│ ├── __init__.py
│ ├── encryption.py # Encryption
│ └── validator.py # Validation
├── ssh/
│ ├── __init__.py
│ ├── manager.py # SSH management
│ └── executor.py # Command execution
├── handlers/
│ ├── __init__.py
│ ├── server_handlers.py # Server management
│ ├── command_handlers.py # Command execution
│ ├── preset_handlers.py # Preset commands
│ └── admin_handlers.py # Admin management
├── utils/
│ ├── __init__.py
│ ├── keyboards.py # Keyboards
│ └── messages.py # Messages
├── requirements.txt
├── .env.example
├── .gitignore
└── README.md
In .env file:
COMMAND_TIMEOUT=300 # Command execution time (seconds)
CONNECTION_TIMEOUT=1800 # Idle connection time (seconds)MAX_COMMAND_LENGTH=1000 # Maximum command length
RATE_LIMIT_PER_MINUTE=30 # Requests per minute- Check that PostgreSQL is running
- Verify connection address and credentials in
.env - Ensure database is created
- Verify IP and port are correct
- Check username and password
- Ensure SSH server is accessible
- Check that bot token is correct
- Check logs
- Ensure bot is running
MIT.