Run an nmap command and log both the command and its results Args: command: List of command arguments (should start with 'nmap') context: Additional context for logging **kwargs: Additional arguments to pass to subprocess.run
(self, command: List[str], context: str = "", **kwargs)
| 80 | self.logger.info(f"OPERATION: {operation}{details_str}") |
| 81 | |
| 82 | def run_nmap_command(self, command: List[str], context: str = "", **kwargs) -> subprocess.CompletedProcess: |
| 83 | """ |
| 84 | Run an nmap command and log both the command and its results |
| 85 | |
| 86 | Args: |
| 87 | command: List of command arguments (should start with 'nmap') |
| 88 | context: Additional context for logging |
| 89 | **kwargs: Additional arguments to pass to subprocess.run |
| 90 | |
| 91 | Returns: |
| 92 | subprocess.CompletedProcess object |
| 93 | """ |
| 94 | # Ensure we're dealing with an nmap command |
| 95 | if not command or command[0] != 'nmap': |
| 96 | raise ValueError("Command must start with 'nmap'") |
| 97 | |
| 98 | # Log the command |
| 99 | self.log_command(command, context) |
| 100 | |
| 101 | # Set default kwargs for subprocess.run |
| 102 | run_kwargs = { |
| 103 | 'capture_output': True, |
| 104 | 'text': True, |
| 105 | **kwargs |
| 106 | } |
| 107 | |
| 108 | try: |
| 109 | # Execute the command |
| 110 | result = subprocess.run(command, **run_kwargs) |
| 111 | |
| 112 | # Log the result |
| 113 | self.log_result(result.stdout, result.stderr, result.returncode, context) |
| 114 | |
| 115 | return result |
| 116 | |
| 117 | except Exception as e: |
| 118 | # Log the exception |
| 119 | with self.lock: |
| 120 | context_str = f" ({context})" if context else "" |
| 121 | self.logger.error(f"EXCEPTION{context_str}: {str(e)}") |
| 122 | raise |
| 123 | |
| 124 | # Global instance for use throughout Ragnar |
| 125 | nmap_logger = NmapLogger() |
no test coverage detected