Browse Source

Exception handling for wrong device

master
Dirk Alders 7 months ago
parent
commit
b44cfef41d
1 changed files with 12 additions and 5 deletions
  1. 12
    5
      netst.src/netst.py

+ 12
- 5
netst.src/netst.py View File

6
 import sys
6
 import sys
7
 import time
7
 import time
8
 
8
 
9
+interfaces = ifcfg.interfaces()
10
+
11
+def exit_device():
12
+    print("Possible devices:", ", ".join(interfaces.keys()))
13
+    sys.exit(1)
14
+
15
+
9
 def gateway(device):
16
 def gateway(device):
10
     with open("/proc/net/route") as fh:
17
     with open("/proc/net/route") as fh:
11
         # skip header
18
         # skip header
22
 try:
29
 try:
23
     DEVICE = sys.argv[1]
30
     DEVICE = sys.argv[1]
24
 except IndexError:
31
 except IndexError:
25
-    print("You need to give the device name as first argument")
26
-    sys.exit()
32
+    print("You need to give the device name as first argument!\n")
33
+    exit_device()
27
 
34
 
28
 HRN = {     # Human Readable Name
35
 HRN = {     # Human Readable Name
29
     'MAC': 'ether',
36
     'MAC': 'ether',
33
 }
40
 }
34
 
41
 
35
 try:
42
 try:
36
-    info = ifcfg.interfaces()[DEVICE]
43
+    info = interfaces[DEVICE]
37
 except KeyError:
44
 except KeyError:
38
-    print("Unknown device:", DEVICE)
39
-    sys.exit(1)
45
+    print("Unknown device:", DEVICE, "\n")
46
+    exit_device()
40
 try:
47
 try:
41
     dns_resolver = dns.resolver.Resolver()
48
     dns_resolver = dns.resolver.Resolver()
42
 except dns.resolver.NoResolverConfiguration:
49
 except dns.resolver.NoResolverConfiguration:

Loading…
Cancel
Save