Skip to content

Commit 7e856fd

Browse files
Add -c argument to target_shell (#873)
(DIS-150)
1 parent d0eba16 commit 7e856fd

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

dissect/target/tools/shell.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,29 +1410,38 @@ def build_pipe_stdout(pipe_parts: list[str]) -> Iterator[TextIO]:
14101410
yield pipe_stdin
14111411

14121412

1413-
def open_shell(targets: list[str | pathlib.Path], python: bool, registry: bool) -> None:
1413+
def open_shell(targets: list[str | pathlib.Path], python: bool, registry: bool, commands: list[str] | None) -> None:
14141414
"""Helper method for starting a regular, Python or registry shell for one or multiple targets."""
14151415
targets = list(Target.open_all(targets))
14161416

14171417
if python:
1418-
python_shell(targets)
1418+
python_shell(targets, commands=commands)
14191419
else:
14201420
cli_cls = RegistryCli if registry else TargetCli
1421-
target_shell(targets, cli_cls=cli_cls)
1421+
target_shell(targets, cli_cls=cli_cls, commands=commands)
14221422

14231423

1424-
def target_shell(targets: list[Target], cli_cls: type[TargetCmd]) -> None:
1424+
def target_shell(targets: list[Target], cli_cls: type[TargetCmd], commands: list[str] | None) -> None:
14251425
"""Helper method for starting a :class:`TargetCli` or :class:`TargetHubCli` for one or multiple targets."""
14261426
if cli := create_cli(targets, cli_cls):
1427+
if commands is not None:
1428+
for command in commands:
1429+
cli.onecmd(command)
1430+
return
14271431
run_cli(cli)
14281432

14291433

1430-
def python_shell(targets: list[Target]) -> None:
1434+
def python_shell(targets: list[Target], commands: list[str] | None) -> None:
14311435
"""Helper method for starting a (I)Python shell with multiple targets."""
14321436
banner = "Loaded targets in 'targets' variable. First target is in 't'."
14331437
ns = {"targets": targets, "t": targets[0]}
14341438

14351439
try:
1440+
if commands is not None:
1441+
for command in commands:
1442+
eval(command, ns)
1443+
return
1444+
14361445
import IPython
14371446

14381447
IPython.embed(header=banner, user_ns=ns, colors="linux")
@@ -1512,7 +1521,7 @@ def main() -> None:
15121521
default=None,
15131522
help="select a specific loader (i.e. vmx, raw)",
15141523
)
1515-
1524+
parser.add_argument("-c", "--commands", action="store", nargs="*", help="commands to execute")
15161525
configure_generic_arguments(parser)
15171526
args, rest = parser.parse_known_args()
15181527
args.targets = args_to_uri(args.targets, args.loader, rest) if args.loader else args.targets
@@ -1537,7 +1546,7 @@ def main() -> None:
15371546
)
15381547

15391548
try:
1540-
open_shell(args.targets, args.python, args.registry)
1549+
open_shell(args.targets, args.python, args.registry, args.commands)
15411550
except TargetError as e:
15421551
log.error(e)
15431552
log.debug("", exc_info=e)

tests/tools/test_shell.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import argparse
24
import pathlib
35
import platform
@@ -252,10 +254,10 @@ def _map_function(path: Path) -> str:
252254

253255

254256
def run_target_shell(
255-
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture, target_path: str, stdin: str
257+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture, argv: str | list, stdin: str
256258
) -> tuple[bytes, bytes]:
257259
with monkeypatch.context() as m:
258-
m.setattr("sys.argv", ["target-shell", target_path])
260+
m.setattr("sys.argv", ["target-shell"] + (argv if isinstance(argv, list) else [argv])),
259261
m.setattr("sys.stdin", StringIO(stdin))
260262
m.setenv("NO_COLOR", "1")
261263
target_shell()
@@ -302,6 +304,13 @@ def test_shell_cmd_alias(monkeypatch: pytest.MonkeyPatch, capsys: pytest.Capture
302304
assert ls_la_out == ll_out
303305

304306

307+
def test_shell_cli_command(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture) -> None:
308+
target_path = absolute_path("_data/tools/info/image.tar")
309+
dir_out, _ = run_target_shell(monkeypatch, capsys, target_path, "dir")
310+
ls_out, _ = run_target_shell(monkeypatch, capsys, [target_path, "-c", "dir"], "")
311+
assert dir_out == "ubuntu:/$ " + ls_out + "ubuntu:/$ \n"
312+
313+
305314
def test_shell_cmd_alias_runtime(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture) -> None:
306315
"""test if alias commands call their parent attribute correctly."""
307316
target_path = absolute_path("_data/tools/info/image.tar")

0 commit comments

Comments
 (0)