Exemplo n.º 1
0
def remove(address_to_remove=None,
           names_to_remove=None,
           remove_from_path=None):
    """Remove entries from a hosts file

    :param address_to_remove: An ipv4 or ipv6 address to remove
    :param names_to_remove: A list of names to remove
    :param remove_from_path: The path of the hosts file to remove entries from
    :return: A dict containing the result and user message to output
    """
    hosts = Hosts(path=remove_from_path)
    if address_to_remove or names_to_remove:
        num_before = hosts.count()
        hosts.remove_all_matching(address=address_to_remove,
                                  name=names_to_remove)
        hosts.write()
        difference = num_before - hosts.count()
        if difference:
            if difference > 1:
                str_entry = 'entries'
            else:
                str_entry = 'entry'
            return {
                'result': 'success',
                'message': 'Removed {0} {1}'.format(difference, str_entry)
            }
        else:
            return {'result': 'failed', 'message': 'No matching entries found'}
Exemplo n.º 2
0
def updateHostsConfigFile(newEntriesList):
    """ write the latest IP address for the hosts to the system config file """
    my_hosts = Hosts()
    print("Locate the hosts config from : ", my_hosts.determine_hosts_path())
    print("Host config entries number : ", my_hosts.count())

    #step 1, remove all the entries with the same name
    for entry in newEntriesList:
        my_hosts.remove_all_matching(name=entry['Host'])

    #step 2, add the entry from the new entry list
    for entry in newEntriesList:
        new_entry = HostsEntry(entry_type='ipv4',
                               address=entry['Ip'],
                               names=[entry['Host']])
        ret = my_hosts.add(
            [new_entry],
            allow_address_duplication=True,
        )
        #print(f"Add ipv4 entry for:  {new_entry}\n\tOperation result : {ret}\n")

    #step 3, write the host file
    result = my_hosts.write()
    if (result is not None):
        print("Done! new host file saved! result : \n")
        print('\n'.join(f'{k} : {v}' for k, v in sorted(result.items())))
    else:
        print("Error! update host file failed! \n")
Exemplo n.º 3
0
def checkHostsLatency(hostname_keyword=None):
    """ Read host and IP from system hosts config file, Ping the specific host IP to check the average latency in ms """
    my_hosts = Hosts()
    print(my_hosts.determine_hosts_path())
    print("Host config entries : ", my_hosts.count())

    entry = 0

    for host in my_hosts.entries:
        if (host.entry_type == "ipv4"):
            if (hostname_keyword is None):  # by default print all entries
                print(host)
            else:  # print entries with keyword
                hostString = "".join(host.names)
                hostString = hostString.strip().lower()
                if (hostname_keyword.strip().lower() in hostString):
                    ipaddr = host.address
                    latency = ping(ipaddr, size=1000, verbose=False).rtt_avg_ms
                    print(entry, f"  Latency {latency:<7.2f}ms ", host)
                    entry += 1

    if (entry == 0):
        print(hostname_keyword, " Not found in the host file!\n")