Parcourir la source

Exception handling for wrong device

master
Dirk Alders il y a 9 mois
Parent
révision
b44cfef41d
1 fichiers modifiés avec 12 ajouts et 5 suppressions
  1. 12
    5
      netst.src/netst.py

+ 12
- 5
netst.src/netst.py Voir le fichier

@@ -6,6 +6,13 @@ import struct
6 6
 import sys
7 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 16
 def gateway(device):
10 17
     with open("/proc/net/route") as fh:
11 18
         # skip header
@@ -22,8 +29,8 @@ def gateway(device):
22 29
 try:
23 30
     DEVICE = sys.argv[1]
24 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 35
 HRN = {     # Human Readable Name
29 36
     'MAC': 'ether',
@@ -33,10 +40,10 @@ HRN = {     # Human Readable Name
33 40
 }
34 41
 
35 42
 try:
36
-    info = ifcfg.interfaces()[DEVICE]
43
+    info = interfaces[DEVICE]
37 44
 except KeyError:
38
-    print("Unknown device:", DEVICE)
39
-    sys.exit(1)
45
+    print("Unknown device:", DEVICE, "\n")
46
+    exit_device()
40 47
 try:
41 48
     dns_resolver = dns.resolver.Resolver()
42 49
 except dns.resolver.NoResolverConfiguration:

Chargement…
Annuler
Enregistrer