Selaa lähdekoodia

netst added to my bin folder

master
Dirk Alders 9 kuukautta sitten
vanhempi
commit
4a5ea44daa
4 muutettua tiedostoa jossa 71 lisäystä ja 0 poistoa
  1. 2
    0
      .gitignore
  2. 6
    0
      netst
  3. 60
    0
      netst.src/netst.py
  4. 3
    0
      netst.src/requirements.txt

+ 2
- 0
.gitignore Näytä tiedosto

@@ -1,3 +1,5 @@
1
+*/venv
2
+
1 3
 # ---> Linux
2 4
 *~
3 5
 

+ 6
- 0
netst Näytä tiedosto

@@ -0,0 +1,6 @@
1
+#!/bin/bash
2
+#
3
+BASEDIR=$(dirname $0)
4
+
5
+$BASEDIR/netst.src/venv/bin/python $BASEDIR/netst.src/netst.py $*
6
+

+ 60
- 0
netst.src/netst.py Näytä tiedosto

@@ -0,0 +1,60 @@
1
+import dns.resolver
2
+import ifcfg 
3
+import json
4
+import socket
5
+import struct
6
+import sys
7
+import time
8
+
9
+def gateway(device):
10
+    with open("/proc/net/route") as fh:
11
+        # skip header
12
+        next(fh)
13
+        for line in fh:
14
+            routes = line.strip().split()
15
+            if routes[0] == device:
16
+                destination = socket.inet_ntoa(struct.pack("<L", int(routes[1], 16)))
17
+                if destination != "0.0.0.0":
18
+                    continue
19
+                return socket.inet_ntoa(struct.pack("<L", int(routes[2], 16)))
20
+
21
+
22
+try:
23
+    DEVICE = sys.argv[1]
24
+except IndexError:
25
+    print("You need to give the device name as first argument")
26
+    sys.exit()
27
+
28
+HRN = {     # Human Readable Name
29
+    'MAC': 'ether',
30
+    'MTU': 'mtu',
31
+    'IP': 'inet',
32
+    'Netmask': 'netmask',
33
+}
34
+
35
+try:
36
+    info = ifcfg.interfaces()[DEVICE]
37
+except KeyError:
38
+    print("Unknown device:", DEVICE)
39
+    sys.exit(1)
40
+try:
41
+    dns_resolver = dns.resolver.Resolver()
42
+except dns.resolver.NoResolverConfiguration:
43
+    dns_server = []
44
+else:
45
+    dns_server = dns_resolver.nameservers
46
+
47
+print('+' + 27 * '-' + '+')
48
+print("|%9s: %s" % ('Device', DEVICE) + (16 - len(DEVICE)) * ' ' + '|')
49
+print('+' + 27 * '-' + '+')
50
+for name in HRN:
51
+    print("%10s: %s" % (name, info[HRN[name]]))
52
+print("%10s: %s" % ('Gateway', gateway(DEVICE)))
53
+
54
+try:
55
+    print("%10s: %s" % ("Domain", socket.getfqdn().split('.', 1)[1]))
56
+except IndexError:
57
+    print("%10s: None" % "Domain")
58
+for i, dns in enumerate(dns_server):
59
+    print("%10s: %s" % ("DNS_%d" % (i + 1), dns))
60
+

+ 3
- 0
netst.src/requirements.txt Näytä tiedosto

@@ -0,0 +1,3 @@
1
+ifcfg
2
+dnspython
3
+

Loading…
Peruuta
Tallenna