Introduce logger.py module.

This commit is contained in:
Sebastian Lenzlinger
2024-05-07 19:55:58 +02:00
parent 577ac9e5cf
commit 7b27ee7fa9
6 changed files with 73 additions and 38 deletions

View File

@@ -8,23 +8,23 @@ from iottb.utils.device_metadata_utils import *
def setup_init_device_root_parser(subparsers):
parser = subparsers.add_parser("add-device", aliases=["add-device-root", "add"])
parser.add_argument("--root", type=pathlib.Path, default=pathlib.Path.cwd())
parser.add_argument("--root_dir", type=pathlib.Path, default=pathlib.Path.cwd())
group = parser.add_mutually_exclusive_group()
group.add_argument("--guided", action="store_true", help="Guided setup", default=False)
group.add_argument("name", action="store", type=str, help="name of device")
group.add_argument("--name", action="store", type=str, help="name of device")
parser.set_defaults(func=handle_add)
def handle_add(args):
print("Entered add-device-root")
metadata = None
if args.guided:
metadata = guided_setup(args.root_dir)
else:
device_name = args.name
args.root_dir.mkdir(parents=True, exist_ok=True)
args.root_dir.chdir()
metadata = DeviceMetadata(device_name)
metadata = DeviceMetadata(device_name, args.root_dir)
file_path = args.root_dir / DEVICE_METADATA_FILE
response = input(f"Confirm device metadata: {metadata.model_dump()} [y/N]")
@@ -50,10 +50,10 @@ def guided_setup(device_root) -> DeviceMetadata:
device_name = input("Please enter name of device: ")
if device_name == "" or device_name is None:
print("Name cannot be empty")
response = input(f"Confirm device name: {device_name} [y/N]")
response = input(f"Confirm device name: {device_name} [y/N] ")
assert response.lower() in definitions.AFFIRMATIVE_USER_RESPONSE.add(""), f"{response.upper()} not supported"
assert device_name != ""
assert device_name is not None
return DeviceMetadata(device_name)
return DeviceMetadata(device_name, device_root)

View File

@@ -12,8 +12,8 @@ def setup_capture_parser(subparsers):
# metadata args
parser.add_argument("-a", "--ip-address", help="IP address of the device to sniff", dest="device_ip")
# tcpdump args
parser.add_argument("device-root", help="Root folder for device to sniff", dest="device_folder",
type=Path, required=True, default=Path.cwd())
parser.add_argument("device_root", help="Root folder for device to sniff",
type=Path, default=Path.cwd())
parser.add_argument("-s", "--safe", help="Ensure correct device root folder before sniffing", action="store_true")
parser.add_argument("--app", help="Application name to sniff", dest="app_name", default=None)
@@ -73,16 +73,16 @@ def run_tcpdump(cmd):
try:
p = subprocess.run(cmd, capture_output=True, text=True, check=True)
if p.returncode != 0:
print(p.stderr)
print(f"Error running tcpdump {p.stderr}")
else:
print(p.stdout)
print(f"tcpdump run successfully\n: {p.stdout}")
except KeyboardInterrupt:
pass
def handle_capture(args):
assert args.device_root is not None, f"Device root directory is required"
assert dir_contains_device_metadata(args.device_root)
assert dir_contains_device_metadata(args.device_root), f"Device metadata file '{args.device_root}' does not exist"
# get device metadata
if args.safe and not dir_contains_device_metadata(args.device_root):
print(f"Supplied folder contains no device metadata. "