A bin folder, holding helpfull scripts and commands
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

netst.py 1.6KB

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