dns_edit.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. """
  16. # This script will modify the /etc/resolv.conf
  17. # A new DNS is added as the first entry of "nameserver"
  18. # the domain is set to be Weave local domain, which is "weave.local"
  19. # set the Weave local domain as the first entry of "search"
  20. import sys
  21. local_Weave_DNS_IP = sys.argv[1]
  22. nameserver_file_name = "/etc/resolv.conf"
  23. lines = []
  24. with open(nameserver_file_name) as f_read:
  25. lines = f_read.readlines()
  26. add_nameserver = False
  27. with open(nameserver_file_name, "w") as f:
  28. for line in lines:
  29. if "search" in line:
  30. tokens = line.split()
  31. f.write("search weave.local")
  32. for token in tokens[1:]:
  33. f.write(" ")
  34. f.write(token)
  35. f.write("\n")
  36. elif "nameserver" in line:
  37. if add_nameserver == False:
  38. f.write("nameserver ")
  39. f.write(local_Weave_DNS_IP)
  40. f.write("\n")
  41. add_nameserver = True
  42. f.write(line)
  43. elif "domain" in line:
  44. f.write("domain weave.local\n")
  45. else:
  46. f.write(line)