Monitor shell command execution :param command: Command to run :type command: `str` :param plugin_info: Plugin context info :type plugin_info: `dict` :return: Scrubbed output from the command :rtype: `str`
(self, session, command, plugin_info)
| 162 | return proc |
| 163 | |
| 164 | def shell_exec_monitor(self, session, command, plugin_info): |
| 165 | """Monitor shell command execution |
| 166 | |
| 167 | :param command: Command to run |
| 168 | :type command: `str` |
| 169 | :param plugin_info: Plugin context info |
| 170 | :type plugin_info: `dict` |
| 171 | :return: Scrubbed output from the command |
| 172 | :rtype: `str` |
| 173 | """ |
| 174 | cmd_info = self.start_cmd(command, command) |
| 175 | target, can_run = self.can_run_cmd(session=session, command=cmd_info) |
| 176 | if not can_run: |
| 177 | message = "The command was already run for target: {!s}".format(target) |
| 178 | return message |
| 179 | logging.info("") |
| 180 | logging.info("Executing :\n\n%s\n\n", command) |
| 181 | logging.info( |
| 182 | "------> Execution Start Date/Time: %s", |
| 183 | self.timer.get_start_date_time_as_str("Command"), |
| 184 | ) |
| 185 | logging.info("") |
| 186 | output = "" |
| 187 | cancelled = False |
| 188 | proc = None |
| 189 | try: |
| 190 | proc = self.create_subprocess(command) |
| 191 | while True: |
| 192 | line = proc.stdout.readline() |
| 193 | if not line: |
| 194 | break |
| 195 | logging.info( |
| 196 | line.decode("utf-8").strip() |
| 197 | ) # Show progress on the screen too! |
| 198 | output += line.decode( |
| 199 | "utf-8" |
| 200 | ) # Save as much output as possible before a tool crashes! :) |
| 201 | except KeyboardInterrupt: |
| 202 | os.killpg(proc.pid, signal.SIGINT) |
| 203 | out, err = proc.communicate() |
| 204 | logging.warn(out.decode("utf-8")) |
| 205 | output += out.decode("utf-8") |
| 206 | try: |
| 207 | os.killpg( |
| 208 | os.getpgid(proc.pid), signal.SIGTERM |
| 209 | ) # Plugin KIA (Killed in Action) |
| 210 | except OSError: |
| 211 | pass # Plugin RIP (Rested In Peace) |
| 212 | cancelled = True |
| 213 | output += user_abort("Command", output) # Identify as Command Level abort |
| 214 | finally: |
| 215 | try: |
| 216 | self.finish_cmd( |
| 217 | session=session, |
| 218 | cmd_info=cmd_info, |
| 219 | was_cancelled=cancelled, |
| 220 | plugin_info=plugin_info, |
| 221 | ) |
no test coverage detected