A bin folder, holding helpfull scripts and commands
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

netst.py 1.5KB

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