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.
- 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.
- 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
Follow these instructions to get a copy of the project up and running on your local machine for development and testing purposes.
- Python 3.8 or higher
- pip (Python package installer)
- Git (for cloning the repository)
-
Clone the repository:
git clone https://github.com/yourusername/python-based-command-terminal.git cd python-based-command-terminal
-
Create a virtual environment (recommended):
python -m venv venv # On Windows: venv\Scripts\activate # On macOS/Linux: source venv/bin/activate
-
Install project dependencies:
pip install django psutil
-
Apply database migrations: (This sets up the initial database required by Django.)
python manage.py migrate
-
Run the development server:
python manage.py runserver
-
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.
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 |
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
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
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
To add a new command to the terminal:
- Open
command_core/views.py
- Add your command logic in the
execute_command
function - Update the help text in the
help
command section - Test your implementation
- 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
python manage.py test
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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
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
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.
- Django team for the excellent web framework
- psutil developers for the system monitoring library
- The open-source community for inspiration and support
Mohit Saxena
- GitHub: @mohitsaxena
- Email: [ms2304saxena@gmail.com]
- Initial release
- Basic command execution functionality
- Web-based terminal interface
- System monitoring capabilities
- Cross-platform support
Made with ❤️ by Mohit Saxena