DEV Community

Cover image for AI-in-Shell: Secure Linux Management via Telegram and Gemini AI (Technical Deep Dive)
Lag Lagendary
Lag Lagendary

Posted on

AI-in-Shell: Secure Linux Management via Telegram and Gemini AI (Technical Deep Dive)

I'm pleased to introduce my new project, AI-in-Shell(ADMIN BOT): Telegram Remote Agent for Linux. This tool isn't just a bot; it's a local, always-running agent that provides a secure bridge between your Telegram chat and your Linux machine.

Tomorrow, after I'm off, I plan to translate the documentation into English and publish it on GIT to reach even more users! But if you're interested, use the browser's built-in Russian -> English automatic translators.

The main idea: gain full control over a server or home PC, perform administrative tasks, and even use generative AI without the need for an SSH connection.

🚀 Key Features of the Agent

  1. Secure Remote Command Execution

The tg_local_shell.py agent allows you to execute any Bash commands (ls, df, cat, and others) directly on your system.

Safety First: The bot is strictly tied to a single ALLOWED_USER_ID. All other commands from unauthorized users are ignored, protecting against unauthorized access.

  1. Gemini API Integration

I've added a set of aliases (alias.txt) and scripts (gemini_query.sh) that allow you to use Google Gemini directly in the command line and then run these functions through Telegram.

geminih: Securely generate code in a sandbox for testing.

geminir: Run generated code.

SGS: Save a script.

  1. Passwordless sudo for automation (UU alias)

For routine tasks, such as system updates, I've configured secure execution of sudo apt ... without prompting for a password. This is implemented through a fine-grained configuration of /etc/sudoers using NOPASSWD, applied only to the specific /usr/bin/apt command. This improves convenience without sacrificing overall system security.

  1. Always-on operation via systemd

To ensure reliability and continuous availability, the agent is configured as a user-based systemd service (tg-remote-shell.service.template).

Reliability: The service automatically starts at system startup and restarts in the event of a failure.

Management: Full control via standard commands: systemctl --user start/stop/restart/status tg-remote-shell.service.

🛠️ Architecture and Installation

The project consists of several key files:

tg_local_shell.py: The main Python script that listens to Telegram and executes commands via subprocess.

gemini_query.sh: A Bash wrapper script for calling the Gemini API.

tg-remote-shell.service.template: A systemd template for background work.

Setup overview:

Cloning the repository and setting up venv.

Adding GEMINI_API_KEY to ~/.bashrc and ensuring gemini_query.sh is executable.

Configuring TELEGRAM_BOT_TOKEN and ALLOWED_USER_ID in tg_local_shell.py.

Allowing NOPASSWD for /usr/bin/apt in visudo.

Configuring and starting the service via systemctl --user enable/start tg-remote-shell.service.

Full step-by-step instructions are available in the README.

🔗 Conclusion

AI-in-Shell is a tool for those who value the convenience of remote management and AI capabilities, but aren't willing to sacrifice security. This proves that it's possible to securely combine your favorite messenger, the power of Linux, and generative models.

I welcome your feedback and suggestions for improvements!

Project code on GitHub:
https://github.com/LAG-Lagendary/AI_in-shell

Python #Linux #TelegramBot #GeminiAPI #Bash #Systemd #RemoteAdministration #DevOps #Security

Top comments (3)

Collapse
 
carl231 profile image
carl

I like how you kept the “remote control” idea practical but still locked down with a single allowed Telegram user and scoped sudo permissions

Collapse
 
lag_lagendary profile image
Lag Lagendary

Thank you so much for the great feedback and support. I'm new here and I'm very happy when I get a connection.

Collapse
 
carl231 profile image
carl • Edited

@lag_lagendary You’re very welcome