A powerful Telegram bot that provides SSH terminal access to remote servers through multiple interfaces - connect to your servers securely via Telegram.
-
SSH Mode (
/ssh) - SSH connection to remote servers- Connect with
/ssh <host> [port] [username] - SSH key and password authentication support
- Full PTY emulation for interactive programs
- Connect with
-
TUI Mode (
/tui start) - Enhanced terminal with inline keyboard- Visual terminal display with ANSI color support
- Inline keyboard with arrow keys, Ctrl combinations, and special keys
- Mobile-optimized 60x24 terminal size
- Persistent message that stays at bottom of chat
-
Web App Mode (
/webapp) - Full terminal in Telegram Mini App- Complete xterm.js terminal emulator
- Real-time WebSocket communication
- Full keyboard and mouse support
- Hide/show virtual keyboard button for maximized screen space
- User authorization by Telegram ID whitelist
- SSH key-based authentication support
- Uses existing SSH keys from
~/.ssh/ - Secure password handling
- Session isolation per user
- Python 3.8+
- Linux/Unix system (for PTY support)
- Telegram Bot Token from @BotFather
- Clone the repository:
git clone https://github.com/yourusername/tg-terminal-bot.git
cd tg-terminal-bot- Create virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
# Bot dependencies
pip install python-telegram-bot pexpect pyte nest-asyncio
# Web app dependencies (if using webapp mode)
cd webapp
pip install -r requirements.txt
cd ..- Configure the bot:
# Edit main.py and add your Telegram user ID to AUTHORIZED_USER_IDS
AUTHORIZED_USER_IDS = {
123456789, # Replace with your Telegram user ID
}- Set environment variables:
export TELEGRAM_TOKEN="your_bot_token_here"
# For web app mode (optional)
export WEBAPP_URL="https://your-domain.com" # Or use ngrok for testingpython main.pycd webapp
python app.pyFor testing, you can use ngrok to expose the web app:
ngrok http 8080
# Then set WEBAPP_URL to the ngrok URL- Connect to a server:
/ssh example.com
/ssh server.local 2222 admin
/ssh 192.168.1.100
- Run commands on remote server:
ls -la
pwd
echo "Hello from remote!"
- Disconnect from server:
/disconnect
- Check connection status:
/status
- Start TUI mode:
/tui start
-
Use the inline keyboard for:
- Arrow keys navigation
- Ctrl combinations (Ctrl+C, Ctrl+D, etc.)
- Special keys (Tab, Escape, Enter, Backspace)
- Function keys and more
-
Switch keyboard layouts:
/key nav # Navigation keys
/key ctrl # Control combinations
/key special # Special characters
/key fn # Function keys
- Launch the web app:
/webapp
-
Click "π₯οΈ Open Terminal Web App"
-
Features:
- Full terminal emulation
- Copy/paste support
- Touch-friendly interface
- Toggle keyboard visibility with floating button
tg-tui/
βββ main.py # Main bot logic
βββ webapp/
β βββ app.py # FastAPI WebSocket server
β βββ requirements.txt # Web app dependencies
β βββ templates/
β βββ terminal.html # xterm.js frontend
βββ README.md # This file
-
Bot Server (
main.py):- Handles Telegram messages and SSH commands
- Manages SSH connections using
pexpect - Handles SSH authentication (key and password)
- Renders terminal output using
pytefor TUI mode - Provides inline keyboards for terminal control
-
Web App Server (
webapp/app.py):- FastAPI server with WebSocket support
- Creates PTY for each user session
- Bidirectional communication between browser and terminal
- Proper PTY handling with master/slave FD management
-
Web Frontend (
terminal.html):- xterm.js for terminal rendering
- WebSocket client for real-time communication
- Telegram Web App SDK integration
- Mobile-optimized UI with virtual keyboard
- Always restrict access using
AUTHORIZED_USER_IDS - Never share your bot token publicly
- Be cautious with SSH credentials
- Use SSH keys when possible instead of passwords
- Monitor bot usage through logs
- Consider restricting SSH access to specific hosts
- New Commands: Add handlers in
main.py:
app.add_handler(CommandHandler("yourcommand", your_handler))-
Keyboard Layouts: Extend
get_terminal_keyboard()inmain.py -
Web App Enhancements: Modify
webapp/templates/terminal.html
- Enable debug logging:
logging.basicConfig(level=logging.DEBUG)- Monitor WebSocket connections:
tail -f webapp/app.log # If logging is configured-
"Unauthorized" message:
- Add your Telegram ID to
AUTHORIZED_USER_IDS - Get your ID from @userinfobot
- Add your Telegram ID to
-
Terminal not responding in web app:
- Check WebSocket connection in browser console
- Ensure webapp server is running
- Verify WEBAPP_URL is correctly set
-
Special keys not working:
- Use TUI mode with inline keyboard
- Or use web app for full keyboard support
-
Line wrapping issues:
- TUI mode uses 60-column width for mobile
- Adjust terminal size with
stty cols 60 rows 24
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
MIT License - See LICENSE file for details
- python-telegram-bot - Telegram bot framework
- xterm.js - Terminal emulator for the web
- pexpect - PTY control library
- pyte - Terminal emulator library
- FastAPI - Modern web framework
For issues, questions, or suggestions:
- Open an issue on GitHub
- Contact via Telegram (if provided)