-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
919 additions
and
0 deletions.
There are no files selected for viewing
Binary file added
BIN
+29.5 KB
Enumerating a System over the Network/Programming and Algorithms 2 CW2.docx
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| Virtual Machine Controller Tool | ||
| Overview | ||
| The Virtual Machine Controller Tool is a Python-based application designed to manage and send commands to multiple virtual machines (VMs) through a graphical user interface (GUI). It allows users to select between different VMs, choose commands based on the operating system of the selected VM, and execute these commands remotely. The tool supports basic network commands for Linux and Windows VMs and provides real-time output display within the GUI. | ||
|
|
||
| Features | ||
| VM Configuration Management: Configure and select VMs to manage directly from the GUI. | ||
| OS-Specific Commands: Dynamically updates available commands based on the selected VM's operating system. | ||
| Command Execution: Execute commands on the chosen VM or all VMs simultaneously and display the output. | ||
| Output Management: Save command execution outputs to text files for further analysis or documentation purposes. | ||
| Components | ||
| config_handler.py: Manages configurations for VMs and defines available commands based on the operating system. | ||
| utilities.py: Contains utility functions, including a method to save command outputs to a file. | ||
| controller_app.py: The main application logic, handling the GUI and integrating other components. | ||
| main.py: The entry point of the application, responsible for initializing and running the GUI. | ||
| Setup | ||
| Ensure Python 3.x is installed on your system. | ||
| Clone the repository or download the source code. | ||
| (Optional) Set up a virtual environment: | ||
| bash | ||
| Copy code | ||
| python -m venv venv | ||
| source venv/bin/activate # On Windows use `venv\Scripts\activate` | ||
| (Optional) Install dependencies: | ||
| Copy code | ||
| pip install -r requirements.txt | ||
| Running the Application | ||
| To run the Virtual Machine Controller Tool, navigate to the project directory and execute: | ||
|
|
||
| css | ||
| Copy code | ||
| python main.py | ||
| Testing | ||
| Unit tests have been implemented to ensure the reliability of the tool's core functionalities. These tests cover: | ||
|
|
||
| ConfigHandler Functionality: Tests configuration loading for VMs and command sets, ensuring correct retrieval of configurations based on VM name and OS type. | ||
| Command Execution and Output Saving: Verifies that commands are executed correctly on the selected VMs, and the outputs are accurately saved to files. | ||
| Test Cases and Outcomes | ||
| test_get_vm_config_exists: Ensures VM configurations are correctly retrieved. Outcome: Passed. | ||
| test_get_vm_config_not_exists: Checks the tool's response to requests for non-existent VM configurations. Outcome: Passed. | ||
| test_save_command_output_content: Confirms that command outputs are correctly saved to files and contain the expected content. Outcome: Passed. | ||
| Additional tests for error handling and edge cases have been conducted to simulate various scenarios, including invalid commands and connection failures. Outcome: All tests passed, ensuring robust error handling and graceful degradation. |
Binary file added
BIN
+1.93 KB
Enumerating a System over the Network/__pycache__/config_handler.cpython-312.pyc
Binary file not shown.
Binary file added
BIN
+7.73 KB
Enumerating a System over the Network/__pycache__/controller_app.cpython-312.pyc
Binary file not shown.
Binary file added
BIN
+724 Bytes
Enumerating a System over the Network/__pycache__/utilities.cpython-312.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import socket | ||
| import subprocess | ||
|
|
||
| class CommandExecutor: | ||
| """ | ||
| Class to execute commands and return their output. | ||
| """ | ||
| @staticmethod | ||
| def execute(command): | ||
| """ | ||
| Execute a command and return its output. | ||
|
|
||
| Parameters: | ||
| command (str): The command to execute. | ||
|
|
||
| Returns: | ||
| str: The output of the executed command. | ||
| """ | ||
| try: | ||
| output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT) | ||
| return output.decode() | ||
| except subprocess.CalledProcessError as e: | ||
| return e.output.decode() | ||
|
|
||
| class Server: | ||
| """ | ||
| Class to handle server functionality. | ||
| """ | ||
| def __init__(self, host, port): | ||
| """ | ||
| Initialize the server. | ||
|
|
||
| Parameters: | ||
| host (str): The host IP on which the server will listen. | ||
| port (int): The port number on which the server will listen. | ||
| """ | ||
| self.host = host | ||
| self.port = port | ||
| self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
|
|
||
| def start(self): | ||
| """ | ||
| Start the server and listen for incoming connections. | ||
| """ | ||
| try: | ||
| self.server_socket.bind((self.host, self.port)) | ||
| self.server_socket.listen(1) # Limit to 1 pending connection | ||
| print("Server is listening on {}:{}".format(self.host, self.port)) | ||
|
|
||
| while True: | ||
| client_socket, client_address = self.server_socket.accept() | ||
| print("Connection established with:", client_address) | ||
|
|
||
| while True: | ||
| command = client_socket.recv(1024).decode().strip() | ||
| print("Received command:", command) | ||
|
|
||
| if command == 'exit': | ||
| break | ||
| else: | ||
| output = CommandExecutor.execute(command) | ||
| client_socket.sendall(output.encode()) | ||
|
|
||
| except Exception as e: | ||
| print("Error:", e) | ||
| finally: | ||
| client_socket.close() | ||
| self.server_socket.close() | ||
|
|
||
| def main(): | ||
| """ | ||
| Main function to start the server. | ||
| """ | ||
| host = '0.0.0.0' # Listen on all available network interfaces | ||
| port = 12345 # Use a custom port (you can change it as needed) | ||
| server = Server(host, port) | ||
| server.start() | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import socket | ||
| import subprocess | ||
| import os | ||
|
|
||
| class CommandExecutor: | ||
| """ | ||
| Class to execute commands and return their output. | ||
| """ | ||
| @staticmethod | ||
| def execute(command): | ||
| """ | ||
| Execute a command and return its output. | ||
|
|
||
| Parameters: | ||
| command (str): The command to execute. | ||
|
|
||
| Returns: | ||
| str: The output of the executed command. | ||
| """ | ||
| try: | ||
| if os.name == 'nt': | ||
| # For Windows, ensure cmd is used to execute the command | ||
| output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, text=True, executable='/bin/sh') | ||
| else: | ||
| # For Unix-like systems | ||
| output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, text=True) | ||
| return output | ||
| except subprocess.CalledProcessError as e: | ||
| return e.output | ||
|
|
||
| class Server: | ||
| """ | ||
| Class to handle server functionality. | ||
| """ | ||
| def __init__(self, host, port): | ||
| """ | ||
| Initialize the server. | ||
|
|
||
| Parameters: | ||
| host (str): The host IP on which the server will listen. | ||
| port (int): The port number on which the server will listen. | ||
| """ | ||
| self.host = host | ||
| self.port = port | ||
| self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
|
|
||
| def start(self): | ||
| """ | ||
| Start the server and listen for incoming connections. | ||
| """ | ||
| try: | ||
| self.server_socket.bind((self.host, self.port)) | ||
| self.server_socket.listen(1) | ||
| print(f"Server listening on {self.host}:{self.port}") | ||
|
|
||
| while True: | ||
| client_socket, address = self.server_socket.accept() | ||
| print(f"Connection from {address} has been established.") | ||
|
|
||
| while True: | ||
| command = client_socket.recv(1024).decode().strip() | ||
| if not command or command.lower() == 'exit': | ||
| break # Exit the loop if command is empty or 'exit' | ||
| print(f"Executing command: {command}") | ||
| output = CommandExecutor.execute(command) | ||
| client_socket.sendall(output.encode()) | ||
|
|
||
| finally: | ||
| self.server_socket.close() | ||
|
|
||
| def main(): | ||
| """ | ||
| Main function to start the server. | ||
| """ | ||
| host = '0.0.0.0' # Listen on all available network interfaces | ||
| port = 12345 # Port number should match the one used in the controller.py | ||
| server = Server(host, port) | ||
| server.start() | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import socket | ||
| import subprocess | ||
|
|
||
| class CommandExecutor: | ||
| """ | ||
| Class to execute commands and return their output. | ||
| """ | ||
| @staticmethod | ||
| def execute(command): | ||
| """ | ||
| Execute a command and return its output. | ||
|
|
||
| Parameters: | ||
| command (str): The command to execute. | ||
|
|
||
| Returns: | ||
| str: The output of the executed command. | ||
| """ | ||
| try: | ||
| output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT) | ||
| return output.decode() | ||
| except subprocess.CalledProcessError as e: | ||
| return e.output.decode() | ||
|
|
||
| class Server: | ||
| """ | ||
| Class to handle server functionality. | ||
| """ | ||
| def __init__(self, host, port): | ||
| """ | ||
| Initialize the server. | ||
|
|
||
| Parameters: | ||
| host (str): The host IP on which the server will listen. | ||
| port (int): The port number on which the server will listen. | ||
| """ | ||
| self.host = host | ||
| self.port = port | ||
| self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
|
|
||
| def start(self): | ||
| """ | ||
| Start the server and listen for incoming connections. | ||
| """ | ||
| try: | ||
| self.server_socket.bind((self.host, self.port)) | ||
| self.server_socket.listen(1) | ||
| print(f"Server is listening on {self.host}:{self.port}") | ||
|
|
||
| while True: | ||
| client_socket, client_address = self.server_socket.accept() | ||
| print(f"Connection established with: {client_address}") | ||
|
|
||
| while True: | ||
| command = client_socket.recv(1024).decode().strip() | ||
| if not command: | ||
| break # Connection closed by the client | ||
| print(f"Received command: {command}") | ||
| if command.lower() == 'exit': | ||
| break | ||
| output = CommandExecutor.execute(command) | ||
| client_socket.sendall(output.encode()) | ||
| except Exception as e: | ||
| print(f"Server error: {e}") | ||
| finally: | ||
| self.server_socket.close() | ||
|
|
||
| def main(): | ||
| """ | ||
| Main function to start the server. | ||
| """ | ||
| host = '0.0.0.0' # Listen on all network interfaces | ||
| port = 12345 # The port on which to listen | ||
| server = Server(host, port) | ||
| server.start() | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| class ConfigHandler: | ||
| """Handles configurations for virtual machines and command sets.""" | ||
| def __init__(self): | ||
| self.vm_configs = { | ||
| 'vm1': {'ip': '192.168.0.37', 'port': 12345}, | ||
| 'vm2': {'ip': '192.168.31.137', 'port': 12345}, | ||
| 'vm3': {'ip': '192.168.0.196', 'port': 12345}, | ||
| } | ||
| self.commands = { | ||
| 'Linux': { | ||
| 'Get Users (cat /etc/passwd)': 'cat /etc/passwd', | ||
| 'Finding open Ports (nmap localhost)': 'nmap localhost', | ||
| 'List running processes (ps aux)': 'ps aux', | ||
| 'System information (uname -a)': 'uname -a', | ||
| 'Network information (ifconfig)': 'ifconfig', | ||
| 'Disk usage (df -h)': 'df -h', | ||
| }, | ||
| 'Windows': { | ||
| 'Display network info (ipconfig)': 'ipconfig', | ||
| 'Display network stats (netstat)': 'netstat', | ||
| 'List of processes (tasklist)': 'tasklist', | ||
| 'System information (systeminfo)': 'systeminfo', | ||
| 'List user accounts (net user)': 'net user', | ||
| 'List open ports (netstat -a -n)': 'netstat -a -n', | ||
| 'Display computer name (hostname)': 'hostname', | ||
| }, | ||
| } | ||
|
|
||
| def get_vm_config(self, vm_name): | ||
| """Retrieves the configuration for a specified VM.""" | ||
| return self.vm_configs.get(vm_name, {}) | ||
|
|
||
| def get_commands(self, os_type): | ||
| """Retrieves the command set for the specified OS type.""" | ||
| return self.commands.get(os_type, {}) |
Oops, something went wrong.