From b44cfef41dc8ec7362ce4f8344fc359099ccc323 Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Sat, 23 Mar 2024 14:13:53 +0100 Subject: [PATCH] Exception handling for wrong device --- netst.src/netst.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/netst.src/netst.py b/netst.src/netst.py index 6d2c923..9d201bd 100644 --- a/netst.src/netst.py +++ b/netst.src/netst.py @@ -6,6 +6,13 @@ import struct import sys import time +interfaces = ifcfg.interfaces() + +def exit_device(): + print("Possible devices:", ", ".join(interfaces.keys())) + sys.exit(1) + + def gateway(device): with open("/proc/net/route") as fh: # skip header @@ -22,8 +29,8 @@ def gateway(device): try: DEVICE = sys.argv[1] except IndexError: - print("You need to give the device name as first argument") - sys.exit() + print("You need to give the device name as first argument!\n") + exit_device() HRN = { # Human Readable Name 'MAC': 'ether', @@ -33,10 +40,10 @@ HRN = { # Human Readable Name } try: - info = ifcfg.interfaces()[DEVICE] + info = interfaces[DEVICE] except KeyError: - print("Unknown device:", DEVICE) - sys.exit(1) + print("Unknown device:", DEVICE, "\n") + exit_device() try: dns_resolver = dns.resolver.Resolver() except dns.resolver.NoResolverConfiguration: