MCPcopy
hub / github.com/StevenBlack/hosts / flush_dns_cache

Function flush_dns_cache

updateHostsFile.py:1468–1556  ·  view source on GitHub ↗

Flush the DNS cache.

()

Source from the content-addressed store, hash-verified

1466
1467
1468def flush_dns_cache():
1469 """
1470 Flush the DNS cache.
1471 """
1472
1473 print("Flushing the DNS cache to utilize new hosts file...")
1474 print(
1475 "Flushing the DNS cache requires administrative privileges. You might need to enter your password."
1476 )
1477
1478 dns_cache_found = False
1479
1480 if platform.system() == "Darwin":
1481 if subprocess.call(SUDO + ["killall", "-HUP", "mDNSResponder"]):
1482 print_failure("Flushing the DNS cache failed.")
1483 elif os.name == "nt":
1484 print("Automatically flushing the DNS cache is not yet supported.")
1485 print(
1486 "Please copy and paste the command 'ipconfig /flushdns' in "
1487 "administrator command prompt after running this script."
1488 )
1489 else:
1490 nscd_prefixes = ["/etc", "/etc/rc.d"]
1491 nscd_msg = "Flushing the DNS cache by restarting nscd {result}"
1492
1493 for nscd_prefix in nscd_prefixes:
1494 nscd_cache = nscd_prefix + "/init.d/nscd"
1495
1496 if os.path.isfile(nscd_cache):
1497 dns_cache_found = True
1498
1499 if subprocess.call(SUDO + [nscd_cache, "restart"]):
1500 print_failure(nscd_msg.format(result="failed"))
1501 else:
1502 print_success(nscd_msg.format(result="succeeded"))
1503
1504 centos_file = "/etc/init.d/network"
1505 centos_msg = "Flushing the DNS cache by restarting network {result}"
1506
1507 if os.path.isfile(centos_file):
1508 if subprocess.call(SUDO + [centos_file, "restart"]):
1509 print_failure(centos_msg.format(result="failed"))
1510 else:
1511 print_success(centos_msg.format(result="succeeded"))
1512
1513 system_prefixes = ["/usr", ""]
1514 service_types = ["NetworkManager", "wicd", "dnsmasq", "networking"]
1515 restarted_services = []
1516
1517 for system_prefix in system_prefixes:
1518 systemctl = system_prefix + "/bin/systemctl"
1519 system_dir = system_prefix + "/lib/systemd/system"
1520
1521 for service_type in service_types:
1522 service = service_type + ".service"
1523 if service in restarted_services:
1524 continue
1525

Callers 8

test_flush_darwinMethod · 0.90
test_flush_windowsMethod · 0.90
test_flush_no_toolMethod · 0.90
test_flush_posixMethod · 0.90
test_flush_posix_failMethod · 0.90

Calls 3

print_successFunction · 0.85
path_join_robustFunction · 0.85
print_failureFunction · 0.70

Tested by 7

test_flush_darwinMethod · 0.72
test_flush_windowsMethod · 0.72
test_flush_no_toolMethod · 0.72
test_flush_posixMethod · 0.72
test_flush_posix_failMethod · 0.72