def main()

in cortado/rtas/hosts_file_modify.py [0:0]


def main():
    hosts_files = {
        OSType.WINDOWS: "C:\\Windows\\system32\\drivers\\etc\\hosts",
        OSType.LINUX: "/etc/hosts",
        OSType.MACOS: "/private/etc/hosts",
    }
    current_os = _common.get_current_os()
    hosts_file = hosts_files[current_os]

    backup = Path(hosts_file + "_backup").resolve()
    log.info("Backing up original 'hosts' file.")
    _common.copy_file(hosts_file, backup)

    # add randomness for diffs for FIM module
    randomness = "".join(random.sample(ascii_letters, 10))
    entry = [
        "",
        "# RTA hosts_modify was here",
        "# 8.8.8.8 https://www.{random}.google.com".format(random=randomness),
    ]
    with open(hosts_file, "a") as f:
        _ = f.write("\n".join(entry))

    log.info("Updated hosts file")
    with open(hosts_file, "r") as f:
        log.info(f.read())

    time.sleep(2)

    # cleanup
    log.info("Restoring hosts from backup copy.")
    _common.copy_file(backup, hosts_file)
    os.remove(backup)