def main(): # Default log-level is "INFO" logging.getLogger().setLevel(logging.INFO) # Build the victim object pwncat.victim = Victim() # Arguments to `pwncat` are considered arguments to `connect` # We use the `prog_name` argument to make the help for "connect" # display "pwncat" in the usage. This is just a visual fix, and # isn't used anywhere else. pwncat.victim.command_parser.dispatch_line( shlex.join(["connect"] + sys.argv[1:]), prog_name="pwncat" ) # Only continue if we successfully connected if not pwncat.victim.connected: exit(0) # Setup the selector to wait for data asynchronously from both streams selector = selectors.DefaultSelector() selector.register(sys.stdin, selectors.EVENT_READ, None) selector.register(pwncat.victim.client, selectors.EVENT_READ, "read") # Initialize our state done = False try: # This loop is only used to funnel data between the local # and remote hosts when in raw mode. During the `pwncat` # prompt, the main loop is handled by the CommandParser # class `run` method. while not done: for k, _ in selector.select(): if k.fileobj is sys.stdin: data = sys.stdin.buffer.read(1) pwncat.victim.process_input(data) else: data = pwncat.victim.recv() if data is None or len(data) == 0: done = True break sys.stdout.buffer.write(data) sys.stdout.flush() except ConnectionResetError: pwncat.victim.restore_local_term() console.log("[yellow]warning[/yellow]: connection reset by remote host") except SystemExit: console.log("closing connection") finally: # Restore the shell pwncat.victim.restore_local_term() try: # Make sure everything was committed pwncat.victim.session.commit() except InvalidRequestError: pass console.log("local terminal restored")
def send_sox_command(self): cmd = self.get_sox_command() logging.debug(shlex.join(cmd)) self.sox_process = self.speaker_process = Popen( cmd, stdout=DEVNULL, stderr=STDOUT, )
def instances_exec_all(cfg: Config, remote_cmd: Sequence[str]): """Execute REMOTE_CMD on all the instances.""" escaped = shlex.join(remote_cmd) if not are_you_sure(f'exec command {escaped} in all instances', cfg): return print("Running '{}' on all instances".format(escaped)) exec_remote_all(pick_instances(cfg), remote_cmd)
def _create_keyhandler_cmd_entry(self): entry = Gtk.Entry() entry.set_size_request(200, -1) entry.set_text(shlex.join(prefs['keyhandler cmd'])) entry.connect('activate', self._entry_keyhandler_cmd_cb) entry.connect('focus_out_event', self._entry_keyhandler_cmd_cb) entry.set_tooltip_text(_('Command line of key handler.')) return entry
def run_psql_as_postgres( config_file: configparser.RawConfigParser, sql_query: str, ) -> None: dbname = get_config(config_file, "postgresql", "database_name", "zulip") subcmd = shlex.join( ["psql", "-v", "ON_ERROR_STOP=1", "-d", dbname, "-c", sql_query]) subprocess.check_call(["su", "postgres", "-c", subcmd])
def run_cli_runner(cls, media_path, *runner_args: str) -> Dict[str, Any]: file_name = os.path.basename(media_path) container_path = os.path.join('/root', file_name) cp_command = ('docker', 'cp', media_path, f'{cls._container_id}:{container_path}') print('Copying media into container with command: ', shlex.join(cp_command)) subprocess.run(cp_command, check=True) exec_command = [ 'docker', 'exec', '-i', cls._container_id, 'runner', container_path, *runner_args ] print('Running job with command: ', shlex.join(exec_command)) with subprocess.Popen(exec_command, stdout=subprocess.PIPE, text=True) as proc: return json.load(proc.stdout)
def prepare_command(self, command): # type: (t.List[str]) -> t.List[str] """Return the given command, if any, with privilege escalation.""" become = ['sudo', '-in'] if command: become.extend(['sh', '-c', shlex.join(command)]) return become
def subprocess_run(cmd): """Runs a command through subprocess.run, with a few tweaks. Raises an Exception if exit code != 0.""" print(shlex.join(cmd)) return subprocess.run(cmd, capture_output=True, text=True, env=os.environ.copy(), check=True)
def resize_window(self, args: List[str]) -> None: s = resize_window('resize_window', shlex.join(args))[1] spec: ResizeSpec = s[0], s[1] t = self.tabs[-1] if t.windows: t.windows[-1].resize_spec = spec else: t.pending_resize_spec = spec
def prepare_command(self, command: list[str]) -> list[str]: """Return the given command, if any, with privilege escalation.""" become = ['su', '-l', 'root'] if command: become.extend(['-c', shlex.join(command)]) return become
def load_native(config: Mapping[str, Any]) -> None: runtime_exe_path = build_runtime(config, "exe") data_path = pathify(config["data"], root_dir, cache_path, True, True) demo_data_path = pathify(config["demo_data"], root_dir, cache_path, True, True) enable_offload_flag = config["enable_offload"] enable_alignment_flag = config["enable_alignment"] realsense_cam_string = config["realsense_cam"] plugin_paths = threading_map( lambda plugin_config: build_one_plugin(config, plugin_config), [plugin_config for plugin_group in config["plugin_groups"] for plugin_config in plugin_group["plugin_group"]], desc="Building plugins", ) actual_cmd_str = config["action"].get("command", "$cmd") illixr_cmd_list = [str(runtime_exe_path), *map(str, plugin_paths)] env_override = dict( ILLIXR_DATA=str(data_path), ILLIXR_DEMO_DATA=str(demo_data_path), ILLIXR_OFFLOAD_ENABLE=str(enable_offload_flag), ILLIXR_ALIGNMENT_ENABLE=str(enable_alignment_flag), ILLIXR_ENABLE_VERBOSE_ERRORS=str(config["enable_verbose_errors"]), ILLIXR_RUN_DURATION=str(config["action"].get("ILLIXR_RUN_DURATION", 60)), ILLIXR_ENABLE_PRE_SLEEP=str(config["enable_pre_sleep"]), KIMERA_ROOT=config["action"]["kimera_path"], AUDIO_ROOT=config["action"]["audio_path"], REALSENSE_CAM=str(realsense_cam_string), ) env_list = [f"{shlex.quote(var)}={shlex.quote(val)}" for var, val in env_override.items()] actual_cmd_list = list( flatten1( replace_all( unflatten(shlex.split(actual_cmd_str)), { ("$env_cmd",): [ "env", "-C", Path(".").resolve(), *env_list, *illixr_cmd_list, ], ("$cmd",): illixr_cmd_list, ("$quoted_cmd",): [shlex.quote(shlex.join(illixr_cmd_list))], ("$env",): env_list, }, ) ) ) log_stdout_str = config["action"].get("log_stdout", None) log_stdout_ctx = cast( ContextManager[Optional[BinaryIO]], (open(log_stdout_str, "wb") if (log_stdout_str is not None) else noop_context(None)), ) with log_stdout_ctx as log_stdout: subprocess_run( actual_cmd_list, env_override=env_override, stdout=log_stdout, check=True, )
def sendVolumeCommand(self): cmd = self.getVolumeCommand(self.volume) logging.debug(shlex.join(cmd)) return Popen( cmd, stdout=DEVNULL, # stdout=STDOUT, # stderr=STDOUT, )
def find_executable(exe): command = shlex.split(exe) executable = shutil.which(command[0]) if executable: command[0] = executable try: # Python 3.8 return shlex.join(command) except AttributeError: return ' '.join(shlex.quote(arg) for arg in command)
def get_image_entrypoint(self, docker_image: str, pull: bool = True) -> str: """Get the entry point for the given image :param docker_image: Docker image to inspect :param pull: Whether to pull if image is not present :return: Image entrypoint """ LOG.debug("Getting the entrypoint for image: %s", docker_image) entrypoint_list = self.inspect_image(docker_image, pull)["Config"]["Entrypoint"] or [] return shlex.join(entrypoint_list)
def main(): apa = argparse.ArgumentParser() apa.add_argument("command", nargs=1, help="command to execute") # subset of arguments accepted by miniooni apa.add_argument("-n", "--no-collector", action="count", help="don't submit measurement") apa.add_argument("-o", "--reportfile", help="specify report file to use") apa.add_argument("-i", "--input", help="input for nettests taking an input") apa.add_argument("--home", help="override home directory") apa.add_argument("nettest", nargs=1, help="nettest to run") out = apa.parse_args() command, nettest = out.command[0], out.nettest[0] if "miniooni" not in command and "measurement_kit" not in command: raise RuntimeError("unrecognized tool") args = [] args.append(command) if "miniooni" in command: args.extend(["--yes"]) # make sure we have informed consent if "measurement_kit" in command: args.extend([ "--ca-bundle-path", file_must_exist("{}/.miniooni/assets/ca-bundle.pem".format( out.home)), ]) args.extend([ "--geoip-country-path", file_must_exist("{}/.miniooni/assets/country.mmdb".format( out.home)), ]) args.extend([ "--geoip-asn-path", file_must_exist("{}/.miniooni/assets/asn.mmdb".format(out.home)), ]) if out.home and "miniooni" in command: args.extend(["--home", out.home]) # home applies to miniooni only if out.input: if "miniooni" in command: args.extend(["-i", out.input]) # input is -i for miniooni if out.no_collector: args.append("-n") if out.reportfile: args.extend(["-o", out.reportfile]) args.append(nettest) if out.input and "measurement_kit" in command: if nettest == "web_connectivity": args.extend(["-u", out.input]) # MK's Web Connectivity uses -u for input sys.stderr.write("minioonilike.py: {}\n".format(shlex.join(args))) common.execute(args)
def routine(mod: Moderator) -> None: # Got to stay safe cmd = shlex.join(["./verified", mod.name, *argv[1:]]).split() result = subprocess.run(cmd, capture_output=True, text=True) if result.returncode != EXIT_SUCCESS: error_and_die(result.stderr) mod.examined = int(result.stdout.split()[1])
def run_cmd(msys2_root: _PathLike, args, **kwargs): executable = os.path.join(msys2_root, 'usr', 'bin', 'bash.exe') env = kwargs.pop("env", os.environ.copy()) env["CHERE_INVOKING"] = "1" env["MSYSTEM"] = "MSYS" env["MSYS2_PATH_TYPE"] = "minimal" check_call([executable, '-lc'] + [shlex.join([str(a) for a in args])], env=env, **kwargs)
def join_cmd(cmd: Iterable[str]) -> str: """Join a list of command line arguments into a single string. This is intended for logging purposes only. It does not provide any safety guarantees. """ if sys.version_info >= (3, 8, 0): return shlex.join(cmd) return " ".join(cmd)
def run(self, cmd, has_pty=True, wait=True) -> bytes: """ Run a command in the context of the remote host and return the output. This is run synchrounously. :param cmd: The command to run. Either a string or an argv list. :param has_pty: Whether a pty was spawned """ if isinstance(cmd, list): cmd = shlex.join(cmd) EOL = b"\r" if has_pty else b"\n" # Read until there's no more data in the queue # This works by waiting for our known prompt self.recvuntil(b"(remote) ") try: self.recvuntil(b"$ ", socket.MSG_DONTWAIT) self.recvuntil(b"# ", socket.MSG_DONTWAIT) except BlockingIOError: pass # Surround the output with known delimeters # self.client.send(b"echo _OUTPUT_DELIM_START_\r") self.client.send(cmd.encode("utf-8") + EOL) # self.client.send(b"echo -e '" + DELIM_ESCAPED + b"'\r") # Initialize response buffer response = b"" peek_len = 4096 # Look for the next prompt in the output and leave it in the buffer if wait: while True: data = self.client.recv(peek_len, socket.MSG_PEEK) if b"(remote) " in data: response = data.split(b"(remote) ")[0] self.client.recv(len(response)) break if len(data) == peek_len: peek_len += 4096 # The echoed input command is currently in the output if has_pty: response = b"".join(response.split(b"\r\n")[1:]) else: response = b"".join(response.split(b"\n")[1:]) # Bash sends these escape sequences for some reason, and it f***s up # the output while b"\x1b_" in response: response = response.split(b"\x1b_") before = response[0] after = b"\x1b_".join(response[1:]) response = before + b"\x1b\\".join(after.split(b"\x1b\\")[1]) return response
def setUpClass(cls): full_image_name = cls._get_full_image_name() command = ('docker', 'run', '--rm', '-d', full_image_name, '-d') print('Starting test container with command: ', shlex.join(command)) proc = subprocess.run(command, stdout=subprocess.PIPE, text=True, check=True) cls._container_id = proc.stdout.strip()
def _run_process(self, command, *arguments, **configuration): expected_rc = int(configuration.pop('expected_rc', 0)) token = configuration.pop('token', 'process') merged_output = is_truthy(configuration.pop('merged_output', True)) input = configuration.pop('input', None) if input and not isinstance(input, bytes): input = input.encode() tty = is_truthy(configuration.pop('tty', False)) redirection = configuration.pop('redirection', None) # For compatibility with Process.run_process() timeout = configuration.pop('timeout', None) on_timeout = configuration.pop('on_timeout', 'terminate') if redirection and not tty: raise ValueError('Cannot use "redirection" without "tty"') with ExitStack() as stack: if merged_output: stdout = stack.enter_context(_Attachment(token + '-output.txt')).path stderr = 'STDOUT' else: stdout = stack.enter_context(_Attachment(token + '-stdout.txt')).path stderr = stack.enter_context(_Attachment(token + '-stderr.txt')).path if tty: joined = shlex.join((command, ) + arguments) if redirection: joined += ' ' + redirection command = 'script' arguments = list() arguments += ['--return'] arguments += ['--quiet'] arguments += ['--echo', 'never', '--log-out', '/dev/null'] arguments += ['--command', joined] process = Process() handle = process.start_process(command, *arguments, **configuration, stdout=str(stdout), stderr=str(stderr)) if input: process_object = process.get_process_object(handle) process_object.stdin.write(input) process_object.stdin.close() result = process.wait_for_process(handle, timeout, on_timeout) if result.rc != expected_rc: raise AssertionError( 'Process exited with unexpected code {}'.format(result.rc)) return result
def testJoin(self): for split_command, command in [ (['a ', 'b'], "'a ' b"), (['a', ' b'], "a ' b'"), (['a', ' ', 'b'], "a ' ' b"), (['"a', 'b"'], '\'"a\' \'b"\''), ]: with self.subTest(command=command): joined = shlex.join(split_command) self.assertEqual(joined, command)
def handle_process_err(cmd, err): if type(cmd) == list: cmd = shlex.join(cmd) output = err.stderr.decode("utf-8") print( f"{bcolors.FAIL}ERROR{bcolors.ENDC} while running\n\n\t{cmd}\n\n{output}", file=sys.stderr, ) sys.exit(err.returncode)
def run_qvm_template(self, *args): try: stdout, stderr = self.loop.run_until_complete( self.updatevm.run_for_stdio(shlex.join(args))) except subprocess.CalledProcessError as e: if e.returncode == 127: self.skipTest('Package qubes-core-admin-client ' '(including qvm-template) not installed') return e.returncode, e.stdout, e.stderr return 0, stdout, stderr
def sendTestSpeakerCommand(self): cmd = self.getTestSpeakerCommand() logging.debug(shlex.join(cmd)) self.testSpeakerProc = Popen( cmd, stdout=DEVNULL, # stdout=DEVNULL, # stdout=STDOUT, # stderr=STDOUT, )
def check_output_msys(msys2_root: str, args: Sequence[str], **kwargs: Any): executable = os.path.join(msys2_root, 'usr', 'bin', 'bash.exe') env = kwargs.pop("env", os.environ.copy()) env["CHERE_INVOKING"] = "1" env["MSYSTEM"] = "MSYS" env["MSYS2_PATH_TYPE"] = "minimal" return subprocess.check_output([executable, '-lce'] + [shlex.join([str(a) for a in args])], env=env, **kwargs)
def run(args): cmd = shlex.join(['uvicorn', 'creepy:app'] + list(args)) while True: start = time.time() return_code = os.system(cmd) if return_code != 0: sys.exit(return_code) duration = time.time() - start if duration < 1: time.sleep(1 - duration)
def main() -> int: """Runs a program specified by command-line arguments.""" args = argument_parser().parse_args() if not args.command or args.command[0] != '--': return 1 env = os.environ.copy() # Command starts after the "--". command = args.command[1:] if args.args_file is not None: empty = True for line in args.args_file: empty = False command.append(line.strip()) if args.skip_empty_args and empty: return 0 if args.env_file is not None: for line in args.env_file: apply_env_var(line, env) # Apply command-line overrides at a higher priority than the env file. for string in args.env: apply_env_var(string, env) if args.capture_output: output_args = {'stdout': subprocess.PIPE, 'stderr': subprocess.STDOUT} else: output_args = {} process = subprocess.run(command, env=env, **output_args) # type: ignore if process.returncode != 0 and args.capture_output: _LOG.error('') _LOG.error('Command failed with exit code %d in GN build.', process.returncode) _LOG.error('') _LOG.error('Build target:') _LOG.error('') _LOG.error(' %s', args.target) _LOG.error('') _LOG.error('Full command:') _LOG.error('') _LOG.error(' %s', shlex.join(command)) _LOG.error('') _LOG.error('Process output:') print(flush=True) sys.stdout.buffer.write(process.stdout) print(flush=True) _LOG.error('') return process.returncode
def _build_replay_cmd(self) -> str: args = [ sys.executable, sys.argv[0], "replay", self.test_log, ] if sys.argv[1:]: args.append("--") args.extend(sys.argv[1:]) return shlex.join(args)
def build_tmux_command(args): """Build the tmux line.""" hashcat_cmd = build_hashcat_command(args) print_msg('Hashcat command line: {}'.format(hashcat_cmd)) cmd = [ "tmux", "new-session", "-d", hashcat_cmd + ";/bin/bash -i", ] return shlex.join(cmd)