Skip to content

msmohit23/-Python-Based-Command-Terminal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python-Based Web Command Terminal

A fully functional, web-based command terminal application built with Python and the Django framework. This project provides a browser-based interface to execute common system commands securely on the server.

Terminal Preview Python Django License


Features ✨

  • Web-Based Interface: A clean, responsive terminal interface that runs directly in your browser with a modern Dracula-inspired dark theme.
  • Command Execution: Supports a whitelist of common commands for file and directory operations:
    • ls / dir: List directory contents.
    • cd: Change directory.
    • pwd: Print working directory.
    • mkdir: Create a new directory.
  • System Monitoring: Integrated with psutil to provide real-time system stats:
    • cpu: Displays current CPU usage.
    • mem: Displays current memory usage.
  • Dynamic Prompt: The command prompt dynamically updates to show the current working directory, providing an intuitive user experience.
  • Secure & Robust: Uses a command whitelist and Python's os module instead of direct shell execution to prevent security vulnerabilities.
  • Error Handling: Gracefully handles invalid or non-whitelisted commands with clear error messages.
  • Cross-Platform: Works on Windows, macOS, and Linux systems.
  • Session Management: Maintains working directory state across commands using Django sessions.

Technology Stack 🛠️

  • Backend: Python 3.8+, Django 5.2+
  • System Interaction: os, psutil libraries
  • Frontend: HTML5, CSS3, JavaScript (Fetch API)
  • Communication: REST API with JSON
  • Database: SQLite (default), easily configurable for other databases
  • Styling: Modern CSS with CSS Grid and custom properties

Getting Started 🚀

Follow these instructions to get a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

  • Python 3.8 or higher
  • pip (Python package installer)
  • Git (for cloning the repository)

Installation & Setup

  1. Clone the repository:

    git clone https://github.com/yourusername/python-based-command-terminal.git
    cd python-based-command-terminal
  2. Create a virtual environment (recommended):

    python -m venv venv
    
    # On Windows:
    venv\Scripts\activate
    
    # On macOS/Linux:
    source venv/bin/activate
  3. Install project dependencies:

    pip install django psutil
  4. Apply database migrations: (This sets up the initial database required by Django.)

    python manage.py migrate
  5. Run the development server:

    python manage.py runserver
  6. Open the application: Open your web browser and navigate to http://127.0.0.1:8000/. You should now see the web terminal interface, ready to accept commands.


Usage 📖

Available Commands

Command Description Example
ls or dir List directory contents ls
cd <directory> Change directory cd Documents
cd .. Go to parent directory cd ..
pwd Print working directory pwd
mkdir <name> Create a new directory mkdir new_folder
cpu Show CPU usage percentage cpu
mem Show memory usage percentage mem
clear Clear terminal screen clear
help Show available commands help

Example Session

user@server:/home/user$ ls
Documents  Downloads  Pictures  Videos
user@server:/home/user$ cd Documents
Changed directory to /home/user/Documents
user@server:/home/user/Documents$ mkdir my_project
Directory '/home/user/Documents/my_project' created successfully
user@server:/home/user/Documents$ cpu
CPU Usage: 15.2%
user@server:/home/user/Documents$ help
Available commands:
- dir/ls: List directory contents
- cd <directory>: Change directory
- cd ..: Go to parent directory
- pwd: Print working directory
- cpu: Show CPU usage
- mem: Show memory usage
- mkdir <directory_name>: Create a new directory
- clear: Clear terminal screen
- help: Show this help message

Project Structure 📁

python-based-command-terminal/
├── command_core/                 # Main Django app
│   ├── static/
│   │   └── command_core/
│   │       ├── script.js        # Frontend JavaScript
│   │       └── style.css        # Terminal styling
│   ├── templates/
│   │   └── command_core/
│   │       └── index.html       # Main template
│   ├── migrations/              # Database migrations
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py                 # Command execution logic
├── terminal_project/            # Django project settings
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
│   └── asgi.py
├── db.sqlite3                   # SQLite database
├── manage.py                    # Django management script
└── README.md                    # This file

Security Considerations 🔒

This terminal implementation prioritizes security by:

  • Command Whitelist: Only predefined commands are allowed to execute
  • No Shell Access: Commands are executed using Python's os module, not shell execution
  • Path Validation: Directory changes are validated before execution
  • Error Handling: Comprehensive error handling prevents information leakage
  • Session Management: Working directory state is managed securely through Django sessions

⚠️ Important: This is designed for development and educational purposes. For production use, consider additional security measures such as authentication, rate limiting, and more restrictive command permissions.


Development 🛠️

Adding New Commands

To add a new command to the terminal:

  1. Open command_core/views.py
  2. Add your command logic in the execute_command function
  3. Update the help text in the help command section
  4. Test your implementation

Customizing the UI

  • Styling: Modify command_core/static/command_core/style.css
  • JavaScript: Update command_core/static/command_core/script.js
  • Template: Edit command_core/templates/command_core/index.html

Running Tests

python manage.py test

Contributing 🤝

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Troubleshooting 🔧

Common Issues

Issue: ModuleNotFoundError: No module named 'django'

  • Solution: Make sure Django is installed: pip install django

Issue: psutil module not found

  • Solution: Install psutil: pip install psutil

Issue: Commands not working on Windows

  • Solution: The application uses Python's os module which is cross-platform compatible

Issue: Permission denied errors

  • Solution: Ensure the Django process has appropriate permissions for the directories you're trying to access

License 📄

This project is licensed under the MIT License - see the LICENSE file for details.

Copyright (c) 2024 Mohit Saxena

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Acknowledgments 🙏

  • Django team for the excellent web framework
  • psutil developers for the system monitoring library
  • The open-source community for inspiration and support

Contact 📧

Mohit Saxena


Changelog 📝

Version 1.0.0

  • Initial release
  • Basic command execution functionality
  • Web-based terminal interface
  • System monitoring capabilities
  • Cross-platform support

Made with ❤️ by Mohit Saxena

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published