Untitled
unknown
plain_text
5 months ago
8.7 kB
7
Indexable
import sys import os import sqlite3 import subprocess from datetime import datetime from PySide6.QtCore import Qt from PySide6.QtWidgets import QApplication, QHBoxLayout, QPushButton, QVBoxLayout, QWidget, QLabel, QListWidget, QListWidgetItem, QComboBox, QTextEdit, QMessageBox, QLineEdit, QInputDialog # Global variable to store the sudo password SUDO_PASSWORD = None # Function to run a bash script and capture its output def run_bash_script(script_path): global SUDO_PASSWORD try: subprocess.run( f"chmod +x {script_path}", shell=True, check=True ) # Check if the sudo password is already set if SUDO_PASSWORD is None: raise ValueError("Sudo password is not set.") # Run the bash script with the sudo password result = subprocess.run( f"echo {SUDO_PASSWORD} | sudo -S {script_path}", shell=True, check=True, capture_output=True, text=True, timeout=3000 ) return result.stdout, result.stderr, result.returncode except subprocess.CalledProcessError as e: return e.stdout, e.stderr, e.returncode except Exception as e: return str(e), "", 1 # Function to save the output to a SQLite database def save_to_db(script_name, stdout, stderr, last_executed, success): conn = sqlite3.connect('audit_results.db') cursor = conn.cursor() cursor.execute(''' CREATE TABLE IF NOT EXISTS audit_results ( id INTEGER PRIMARY KEY, script_name TEXT NOT NULL, stdout TEXT, stderr TEXT, last_executed DATETIME, success INTEGER, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP ) ''') cursor.execute(''' INSERT INTO audit_results (script_name, stdout, stderr, last_executed, success) VALUES (?, ?, ?, ?, ?) ''', (script_name, stdout, stderr, last_executed, success)) conn.commit() conn.close() # Function to prompt the user for the sudo password def prompt_for_sudo_password(): global SUDO_PASSWORD # Create a dialog to input the sudo password password_dialog = QInputDialog() password_dialog.setWindowTitle("Sudo Password") password_dialog.setLabelText("Enter your sudo password:") password_dialog.setTextEchoMode(QLineEdit.Password) ok_pressed = password_dialog.exec() if ok_pressed: SUDO_PASSWORD = password_dialog.textValue() else: QMessageBox.critical(None, "Error", "Sudo password is required to run scripts.") sys.exit(1) # Function to load default scripts into the list widget def load_default_scripts(directory, widget): if os.path.isdir(directory): files = [os.path.join(directory, file) for file in os.listdir(directory) if file.endswith('.sh')] for file in files: item = QListWidgetItem(file) item.setCheckState(Qt.CheckState.Unchecked) widget.addItem(item) else: QMessageBox.critical(widget, "Error", f"Directory {directory} does not exist.") # Main GUI class class AuditApp(QWidget): def __init__(self): super(AuditApp, self).__init__() self.initUI() self.current_working_directory = os.getcwd() self.default_script_directory = os.path.join(self.current_working_directory, "curr") self.remediation_script_directory = os.path.join(self.current_working_directory, "curr") load_default_scripts(self.default_script_directory, self.script_list_widget) # Prompt for the sudo password when the app starts prompt_for_sudo_password() def initUI(self): self.setWindowTitle("EasyAudit") self.setGeometry(100, 100, 600, 400) self.layout = QVBoxLayout() self.label = QLabel("EasyAudit", self) self.label.setStyleSheet("font-size: 16px; font-weight: bold;") self.layout.addWidget(self.label) self.script_list_widget = QListWidget(self) self.layout.addWidget(self.script_list_widget) self.button_layout = QHBoxLayout() self.select_all_btn = QPushButton("Select All", self) self.select_all_btn.clicked.connect(self.select_all_scripts) self.button_layout.addWidget(self.select_all_btn) self.clear_all_btn = QPushButton("Clear All", self) self.clear_all_btn.clicked.connect(self.clear_all_scripts) self.button_layout.addWidget(self.clear_all_btn) self.layout.addLayout(self.button_layout) self.run_btn = QPushButton("Run Scripts", self) self.run_btn.clicked.connect(self.run_selected_scripts) self.layout.addWidget(self.run_btn) self.output_dropdown = QComboBox(self) self.output_dropdown.currentIndexChanged.connect(self.update_output_display) self.layout.addWidget(self.output_dropdown) self.output_label = QLabel("Output Section:", self) self.output_label.setStyleSheet("font-size: 14px; font-weight: bold;") self.layout.addWidget(self.output_label) self.output_text = QTextEdit(self) self.layout.addWidget(self.output_text) self.exit_code_label = QLabel("Exit Code: ", self) self.exit_code_label.setStyleSheet("font-size: 14px; font-weight: bold; color: blue;") self.layout.addWidget(self.exit_code_label) self.setLayout(self.layout) def select_all_scripts(self): for i in range(self.script_list_widget.count()): self.script_list_widget.item(i).setCheckState(Qt.Checked) def clear_all_scripts(self): for i in range(self.script_list_widget.count()): self.script_list_widget.item(i).setCheckState(Qt.Unchecked) # Add this line in the `initUI` method to create a new QLabel for exit code # Update the `run_selected_scripts` method to include the exit code display def run_selected_scripts(self): selected_scripts = [self.script_list_widget.item(i).text() for i in range(self.script_list_widget.count()) if self.script_list_widget.item(i).checkState() == Qt.Checked] if selected_scripts: self.output_dropdown.clear() # Clear existing items for script_path in selected_scripts: stdout, stderr, returncode = run_bash_script(script_path) last_executed = datetime.now() success = (returncode == 0) save_to_db(script_path, stdout, stderr, last_executed, success) result_status = "Pass" if success else "Fail" dropdown_title = f"{os.path.basename(script_path)} ({result_status})" output_text = f"Script: {script_path}\nOutput:\n{stdout}\n\nError:\n{stderr}\n\nLast Executed: {last_executed}\n\n" # Update the exit code label self.exit_code_label.setText(f"Exit Code: {returncode}") # If script fails, run remediation script from remediation directory if not success: script_name = os.path.basename(script_path) remediation_script_path = os.path.join(self.remediation_script_directory, f"{script_name}_remed.sh") if os.path.isfile(remediation_script_path): stdout, stderr, returncode = run_bash_script(remediation_script_path) last_executed = datetime.now() success = (returncode == 0) save_to_db(remediation_script_path, stdout, stderr, last_executed, success) result_status = "Pass" if success else "Fail" dropdown_title = f"{os.path.basename(remediation_script_path)} ({result_status})" output_text += f"Remediation Script: {remediation_script_path}\nOutput:\n{stdout}\n\nError:\n{stderr}\n\nLast Executed: {last_executed}\n\n" # Update the exit code label for remediation script self.exit_code_label.setText(f"Exit Code: {returncode}") else: output_text += f"Remediation Script: {remediation_script_path} not found\n\n" self.output_dropdown.addItem(dropdown_title, output_text) QMessageBox.information(self, "Success", "Scripts executed and results saved.") else: QMessageBox.critical(self, "Error", "No scripts selected.") def update_output_display(self, index): output_text = self.output_dropdown.itemData(index) self.output_text.setPlainText(output_text) if __name__ == "__main__": app = QApplication(sys.argv) audit_app = AuditApp() audit_app.show() sys.exit(app.exec())
Editor is loading...
Leave a Comment