示例#1
0
    def set_hostname():
        macaddress = NetInfo.get_default_mac().replace(":", "")
        default_hostname = "pi-" + macaddress
        init_hostname = socket.gethostname()
        logger.debug("get hostname = %s" % init_hostname)
        # self.hostname = inithostname
        if init_hostname != default_hostname:
            logger.debug("set hostname = %s" % init_hostname)
            # 重设主机名
            args = "hostname  %s " % default_hostname
            subprocess.Popen(args, shell=True,
                             stdout=subprocess.PIPE).communicate()

            # 修改/etc/hostname文件
            f = open('/etc/hostname', 'w')
            f.write(default_hostname)
            f.close()

            # 修改/etc/hosts
            if 'linux' in sys.platform or 'darwin' in sys.platform:
                filename = '/etc/hosts'
            else:
                filename = 'c:\windows\system32\drivers\etc\hosts'

            hosts = Hosts(path=filename)
            # 移除旧域名
            hosts.remove_all_matching(name=init_hostname)
            new_entry = HostsEntry(entry_type='ipv4',
                                   address='127.0.0.1',
                                   names=[default_hostname])
            hosts.add([new_entry])
            hosts.write()
示例#2
0
def main():
    hostentries = []

    if os.environ['USER'] == "root":
        user = os.environ['SUDO_USER']
    else:
        user = os.environ['USER']

    with open("/Users/{}/.vagrant.d/data/machine-index/index".format(user), "r") as rawindex:
        currentvagrants = json.load(rawindex)

    for machine in currentvagrants['machines']:
        currentvagrants['machines'][machine]['machineprovider'] = currentvagrants['machines'][machine]['extra_data']['box']['provider']
        with open("{local_data_path}/machines/{name}/{machineprovider}/vm".format(**currentvagrants['machines'][machine]), "r") as vmraw:
            vmdetails = yaml.load(vmraw)
            name = currentvagrants['machines'][machine]['name']
            hostentries.append(HostsEntry(entry_type="ipv4", address=vmdetails['host'], names=["{}.vagrant.skytap.com".format(name), name, "vagrant"]))
            print("Updating host entry: {} with address: {}".format(name, vmdetails['host']))


    hosts = Hosts("/private/etc/hosts")
    hosts.remove_all_matching(name="vagrant")

    hosts.add(hostentries)

    hosts.write()
示例#3
0
    def prepare_hosts(self):
        host = self.host_name
        if host:
            if self.machine_name:
                ip = self.machine.ip(machine=self.machine_name)
            else:
                ip = '127.0.0.1'
            self.logger.debug('Prepare hosts: {name} with {ip}'.format(name=host, ip=ip))
            hosts = Hosts()
            for entry in hosts.entries:
                if entry.address == ip:
                    if host not in entry.names:
                        entry.names.append(host)
                        entry.names = list(set(entry.names))
            if not hosts.exists(names=[host]):
                entry = HostsEntry(entry_type='ipv4', address=ip, names=[host])
                hosts.add(entries=[entry])

            try:
                # make backup
                hosts_path = Hosts.determine_hosts_path()
                hosts_backup_path = hosts_path + '.' + datetime.datetime.today().strftime('%Y%m%d')
                shutil.copy(hosts_path, hosts_backup_path)
            except BaseException:
                pass

            try:
                hosts.write()
            except BaseException:
                self.logger.debug('Unable to write host file, ignored.')
示例#4
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'}
示例#5
0
def rebuild_hosts(path, android=False):
    new_hosts = Hosts()
    #new_hosts.add(entry_type = 'comment', comment = ">> created by hosts-adblock-plus <<")
    add_default_entry(hosts, native=False)
    if not android:
        add_default_entry(hosts, native=True)
    new_hosts.write(path)
示例#6
0
 def _write_hosts_file(self, overrides):
     hosts_file = Hosts(path='/etc/hosts')
     hosts_file.add([
         HostsEntry(entry_type='ipv4',
                    address=override.ip_address,
                    names=[override.hostname]) for override in overrides
     ])
     hosts_file.write()
示例#7
0
 def set_hosts(address, names, type='ipv4'):
     '''add item to system hosts file'''
     from python_hosts import Hosts, HostsEntry
     hosts = Hosts()
     if isinstance(names, str):
         names = [names]
     new_entry = HostsEntry(entry_type=type, address=address, names=names)
     hosts.add([new_entry])
     hosts.write()
示例#8
0
def modify_etc_hosts(data):
    private_ip = data['private_ip']
    hostname = socket.gethostname()

    hosts = Hosts()
    new_entry = HostsEntry(entry_type='ipv4',
                           address=private_ip,
                           names=[hostname])
    hosts.add([new_entry])
    hosts.write()
示例#9
0
def modify_etc_hosts(data):
    private_ip = data['private_ip']
    hostname = socket.gethostname()

    hosts = Hosts()
    new_entry = HostsEntry(entry_type='ipv4',
                           address=private_ip,
                           names=[hostname])
    hosts.add([new_entry])
    hosts.write()
示例#10
0
 def add_host():
     """ Add hosts entry for the server IP """
     hosts = Hosts()
     hosts.add([
         HostsEntry(
             entry_type='ipv4',
             address=GCEMetadata.IP,
             names=GCEMetadata.HOSTS,
         ),
     ])
     hosts.write()
示例#11
0
def update_all_hosts(hosts_list):
    hosts = Hosts('/home/xin/hosts')
    entries = []
    for row in hosts_list:
        entry = HostsEntry(entry_type=row.get('ip_type', 'ipv4'),
                           address=row["ip"],
                           names=[row['hostname']])
        entries.append(entry)

    ret = hosts.add(entries, force=True, merge_names=True)
    hosts.write()
    return ret
示例#12
0
def rebuild_hosts(path, android=False):
    #new_hosts = Hosts() #would read all /etc/hosts entries, which we do not want
    new_hosts = Hosts(
        path)  #will except -catched- on populate as this path is yet invalid
    #new_hosts.add(entry_type = 'comment', comment = ">> created by hosts-adblock-plus <<")
    add_default_entry(new_hosts, native=False)
    if not android:
        add_default_entry(new_hosts, native=True)
    try:
        new_hosts.write(path)
    except UnableToWriteHosts:
        write_error_log("ERROR: could not rebuild/write " + path)
示例#13
0
def main():
    hosts = Hosts(path='/etc/hosts')

    layout = json.load(open(LAYOUT_JSON))
    for name, config in layout.get('groups', {}).items():
        for host, ip in config.get('ips', {}).items():
            host = host.lower()

            hosts.remove_all_matching(name=host)

            new_entry = HostsEntry(entry_type='ipv4', address=ip, names=[host])
            hosts.add([new_entry])
            hosts.write()
def update_hosts_file(address,hostname,profile):
    if profile is not None:
        copyfile("/etc/hosts", "hosts")
        etchostname = profile.replace(" ", "_") + ("-" + hostname if hostname else "")
        print(f"Updating hostname as: {etchostname} with {address}")

        hosts = Hosts(path='hosts')
        hosts.remove_all_matching(name=etchostname)
        new_entry = HostsEntry(entry_type='ipv4', address=address, names=[etchostname])
        hosts.add([new_entry])
        hosts.write()
        copyfile("hosts", "/etc/hosts")

        print(f"Updated Host name for hostsfile is {etchostname}")
示例#15
0
def update_hosts_file(system_config: Config,
                      warning_callback=lambda msg: None):
    """Update the hosts-file for the current project,
    if any is loaded and updating is enabled in configuration.

    The hosts file is written, if it was changed.
    If it can't be written, warning messages are send to the lambda that outputs
    how to manually change the host file.

    :param warning_callback: Callback that receives strings representing warning messages to output to users.
    :param system_config: System configuration
    """

    if system_config["update_hosts_file"]:
        if "project" in system_config:
            hosts = Hosts()
            new_entries = []
            changes = False

            base_url = system_config["proxy"]["url"]
            if not hosts.exists(names=[base_url]):
                changes = True
                new_entries.append(
                    HostsEntry(entry_type='ipv4',
                               address='127.0.0.1',
                               names=[base_url]))

            if "services" in system_config["project"]["app"]:
                for service in system_config["project"]["app"][
                        "services"].values():
                    domain = service.domain()
                    if not hosts.exists(names=[domain]):
                        changes = True
                        new_entries.append(
                            HostsEntry(entry_type='ipv4',
                                       address='127.0.0.1',
                                       names=[domain]))
            hosts.add(new_entries)
            if changes:
                try:
                    hosts.write()
                except UnableToWriteHosts:
                    entries = "\n".join(
                        [f"{e.address}\t{e.names[0]}" for e in new_entries])
                    warning_callback(
                        f"Could not update the hosts-file ({hosts.hosts_path}) to configure proxy server routing.\n"
                        f"> Give your user permission to edit this file, to remove this warning.\n"
                        f"> If you wish to manually add the entries instead, "
                        f"add the following entries to {hosts.hosts_path}:\n{entries}\n"
                    )
示例#16
0
def update_hosts_file(address,hostname,profile):
    if profile is not None:
        copyfile("/etc/hosts", "hosts")
        etchostname = profile.replace(" ", "_") + ("-" + hostname if hostname else "")
        print(f"Updating hostname as: {etchostname} with {address}")

        hosts = Hosts(path='hosts')
        hosts.remove_all_matching(name=etchostname)
        new_entry = HostsEntry(entry_type='ipv4', address=address, names=[etchostname])
        hosts.add([new_entry])
        hosts.write()
        copyfile("hosts", "/etc/hosts")

        print(f"Updated Host name for hostsfile is {etchostname}")
示例#17
0
def test_write_will_create_path_if_missing():
    """
    Test that the hosts file declared when constructing a Hosts instance will
    be created if it doesn't exist
    """
    now = datetime.datetime.now()
    timestamp = now.strftime('%Y%m%d%H%M%S')
    hosts_path = '/tmp/testwrite.{0}'.format(timestamp)
    hosts = Hosts(path=hosts_path)
    entry = HostsEntry.str_to_hostentry('1.2.3.4 example.com example.org')
    hosts.add(entries=[entry])
    hosts.write()
    hosts2 = Hosts(path=hosts_path)
    os.remove(hosts_path)
    assert hosts2.exists(address='1.2.3.4')
示例#18
0
def test_exception_raised_when_unable_to_write_hosts(tmpdir):
    """ Test that the correct exception is raised when a hosts file
    is not writeable.
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("127.0.0.1\tlocalhost\n")
    hosts = Hosts(path=hosts_file.strpath)
    mode = int('0440', 8)
    # if sys.version_info[0] == 3:
    #    mode = 0o440
    os.chmod(hosts_file.strpath, mode)
    new_entry = HostsEntry(entry_type='ipv4', address='123.123.123.123', names=['test.example.com'])
    hosts.add(entries=[new_entry])
    with pytest.raises(exception.UnableToWriteHosts):
        hosts.write()
示例#19
0
 def main(self):
     i = 0
     while self.isrunning:
         hosts = Hosts(path='C:\Windows\System32\drivers\etc\hosts')
         r = requests.get('http://dev.nsyncdata.net:9000/hosts')
         r = r.json()
         for item in r['Remove']:
             hosts.remove_all_matching(name=item['URL'])
         for item in r['Add']:
             new = HostsEntry(entry_type='ipv4',
                              address=item['Address'],
                              names=[item['URL']])
             hosts.add([new], force=True, allow_address_duplication=True)
         hosts.write()
         time.sleep(30)
示例#20
0
    def create(self, vals):
        # Inherit create_portal.plan create method for generating template
        # name from server name and plan name.
        # Give warning if template is already exist.
        saas_server_obj = self.env['saas_portal.server']
        portal_db_obj = self.env['saas_portal.database']
        config = self.env['ir.config_parameter']
        saas_config = self.env['saas_portal.config.settings'].search(
            [], limit=1, order="id desc")
        alies_name = saas_config.base_saas_domain
        if alies_name == False:
            base_saas_rec = config.search(
                [('key', '=', 'saas_portal.base_saas_domain')], limit=1)
            alies_name = base_saas_rec.value
        server_id = vals.get('server_id' or False)
        server_rec = saas_server_obj.browse(server_id)
        server_name = server_rec.name
        default_plan_name = vals.get('db_name')
        new_server_name = self.concate_string(server_name, alies_name)
        new_plan_name = self.concate_string(default_plan_name, alies_name)
        template_name = "template." + new_server_name + '.' + new_plan_name + \
                        '.' + alies_name

        exsiting_templates = portal_db_obj.search(
            [('name', '=', template_name), ('state', '=', 'template'),
             ('server_id', '=', server_id)],
            limit=1)

        if exsiting_templates:
            raise Warning(
                _('Template already exists. Please give Different '
                  'Name of your Plan.'))
        else:
            # Host entry is done for the ip address and database name
            hosts = Hosts(path='/etc/hosts')
            new_entry = HostsEntry(entry_type='ipv4',
                                   address='127.0.0.1',
                                   names=[template_name])
            hosts.add([new_entry])
            hosts.write()
            portal_db_rec = portal_db_obj.create({
                'name': template_name,
                'server_id': server_id
            })
        vals.update({
            'template_id': portal_db_rec.id,
        })
        return super(mint_plan_enhancement, self).create(vals)
示例#21
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")
示例#22
0
def configure_environment():
    # Descarga la private key de S3
    if 'AWS_ACCESS_KEY_ID' in os.environ:
        s3 = boto3.client('s3')
        s3.download_file(STP_BUCKET_S3, STP_PRIVATE_KEY, STP_PRIVATE_LOCATION)

    # Edita archivo hosts si es necesario
    if os.environ['EDIT_HOSTS'] == 'true':
        host_ip = os.environ['HOST_IP']
        host_ad = os.environ['HOST_AD']
        hosts = Hosts()
        new_entry = HostsEntry(entry_type='ipv4',
                               address=host_ip,
                               names=[host_ad])
        hosts.add([new_entry])
        hosts.write()
示例#23
0
class HostsFileManager:
    def __init__(self):
        self.my_hosts = Hosts()

    def add_entry(self, ip, name):
        name = name + ".dev"
        # just to be safe
        self.remove_entry(ip, name)

        new_entry = HostsEntry(entry_type='ipv4', address=ip, names=[name])
        self.my_hosts.add([new_entry])
        self.my_hosts.write()

    def remove_entry(self, ip, name):
        name = name + ".dev"
        self.my_hosts.remove_all_matching(address=ip)
        self.my_hosts.remove_all_matching(name=name)
        self.my_hosts.write()
示例#24
0
class HostsFile(object):

    def __init__(self):
        self.hosts = Hosts()

    def verifyentry(self, ip, domains):
        # Check the hosts file for concurrency with the pickle file
        if(self.hosts.exists(address=ip) and self.hosts.exists(names=[domains])):
            return True
        else:
            return False

    def addentry(self, ip, domains):
        # Python-Hosts will support multiple domain entries
        # @ TO-DO add support for multiple domain entries
        new_entry = HostsEntry(entry_type='ipv4', address=ip, names=[domains])
        self.hosts.add(new_entry)
        self.hosts.write()
示例#25
0
def add_host(
    airflow_options: AirflowOptions,
    entry_type: str = "ipv4",
    address: Optional[str] = None,
    force: bool = False,
):
    if address is None:
        address = get_minikube_ip()
    my_hosts = Hosts()
    my_hosts.add(
        [
            HostsEntry(entry_type,
                       address=address,
                       names=[airflow_options.domain_name])
        ],
        force=force,
    )
    my_hosts.write()
示例#26
0
def fixMaybeLocalhost(hosts_path="/etc/hosts", hostname=None, IP=None):
    hosts = Hosts(path=hosts_path)
    removed_hosts = []
    # Consider cases where it is added both node.maas and node
    for h in [hostname.split(".")[0], hostname]:
        r = hosts.remove_all_matching(name=h)
        if r:
            removed_hosts += [str(el) for el in r]
    hosts.add([HostsEntry(entry_type='ipv4', address=IP, names=[hostname])])
    # Check if localhost exists, if not, set it to 127.0.0.1
    if len(hosts.find_all_matching(name="localhost")) == 0:
        # Set localhost
        hosts.add([
            HostsEntry(entry_type='ipv4',
                       address='127.0.0.1',
                       names=["localhost"])
        ])
    hosts.write()
    return removed_hosts
示例#27
0
def add(entry_line=None, hosts_path=None, force_add=False):
    """Add the specified entry

    :param entry_line: The entry to add
    :param hosts_path: The path of the hosts file
    :param force_add: Replace matching any matching entries with new entry
    :return: A dict containing the result and user message to output
    """
    hosts_entry = HostsEntry.str_to_hostentry(entry_line)
    if not hosts_entry:
        output_message({
            'result':
            'failed',
            'message':
            '"{0}": is not a valid entry.'.format(entry_line)
        })

    duplicate_entry = False
    entry_to_add = False

    hosts = Hosts(hosts_path)
    add_result = hosts.add(entries=[hosts_entry], force=force_add)
    if add_result.get('replaced_count'):
        hosts.write()
        return {
            'result': 'success',
            'message': 'Entry added. Matching entries replaced.'
        }
    if add_result.get('ipv4_count') or add_result.get('ipv6_count'):
        entry_to_add = True
    if add_result.get('duplicate_count'):
        duplicate_entry = True
    if entry_to_add and not duplicate_entry:
        hosts.write()
        return {'result': 'success', 'message': 'New entry added.'}
    if not force_add and duplicate_entry:
        return {
            'result':
            'failed',
            'message':
            'New entry matches one or more existing.'
            '\nUse -f to replace similar entries.'
        }
示例#28
0
def test_existing_comments_and_blanks_are_preserved(tmpdir):
    """
    Test that comments and newlines/blanks that exist in the file prior to
    changes are preserved after a new entry is added
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("6.6.6.6\texample.com\n# A test comment\n\n")
    hosts = Hosts(path=hosts_file.strpath)
    new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.132', names=['something.com', 'example'])
    hosts.add(entries=[new_entry], force=False)
    write_result = hosts.write()
    assert write_result.get('comments_written') == 1
    assert write_result.get('blanks_written') == 1
示例#29
0
def stopProject(name):
    if os.path.isdir("./" + name) == False:
        print(
            "Project does not exist, please choose another name or create it with command: python {} create {}"
            .format(sys.argv[0], name))
        exit(0)

    settingsFile = "./" + name + "/settings"
    settings = importlib.import_module(".settings", package=name)

    dockerdbargs = ["docker", "stop", name + "-db"]
    dockerwpargs = ["docker", "stop", name + "-wp"]

    print("Stoping WP\n" + ' '.join(dockerwpargs))
    os.system(' '.join(dockerwpargs))

    print("Stoping DB\n" + ' '.join(dockerdbargs))
    os.system(' '.join(dockerdbargs))

    print("Restore rights")
    osargs = [
        "sudo", "chown", "-R", "docker:docker",
        os.getcwd() + "/" + name + "/html/wp-content"
    ]
    os.system(' '.join(osargs))
    osargs = [
        "sudo", "chmod", "-R", "755",
        os.getcwd() + "/" + name + "/html/wp-content"
    ]
    os.system(' '.join(osargs))

    print("Clear host")
    osargs = ["sudo", "chmod", "666", "/etc/hosts"]
    os.system(' '.join(osargs))
    my_hosts = Hosts()
    my_hosts.remove_all_matching(name=settings.siteurl)
    my_hosts.write()
    osargs = ["sudo", "chmod", "644", "/etc/hosts"]
    os.system(' '.join(osargs))
示例#30
0
    def write_file_entry(self, current_config=None, previous_config=None):
        f = Hosts(self.hosts_file)

        def _fmt(**kwargs):
            return self.pattern.format(**kwargs)

        def _gen_entries():
            for network_name, network_address in current_config.get(
                    'networks', {}).items():
                name = _fmt(name=current_config['name'],
                            hostname=current_config['hostname'],
                            network=network_name)
                if network_address and network_name:
                    logger.debug("Adding host entry %s <> %s", network_address,
                                 name)
                    yield HostsEntry(entry_type='ipv4',
                                     address=network_address,
                                     names=[name])

        for cfg in current_config, previous_config:
            if cfg is None:
                continue

            for _, addr in cfg.get('networks', {}).items():
                if addr:
                    logger.debug("Removing entries matching address: %s", addr)
                    f.remove_all_matching(address=addr)

            for network, _ in cfg.get('networks', {}).items():
                name = _fmt(name=cfg['name'],
                            hostname=cfg['hostname'],
                            network=network)
                logger.debug("Removing entries matching name: %s", name)
                f.remove_all_matching(name=name)

        if current_config:
            f.add(list(_gen_entries()))

        f.write()
示例#31
0
def modify_hosts_file_entries(old_name, new_name):
    """Modify the hosts file to replace old_name with new_name."""
    hosts = Hosts()

    entries = hosts.find_all_matching(name=old_name)

    # Iterate through all relevant entries
    for e in entries:
        # Iterate through all names
        new_names = []
        for n in e.names:
            # Modify name
            new_names.append(n.replace(old_name, new_name))
        new_entry = HostsEntry(entry_type=e.entry_type,
                               address=e.address,
                               names=new_names,
                               comment=e.comment)

        # Replace old entry
        hosts.add([new_entry], force=True)

    hosts.write()
示例#32
0
 def edit_host(enable):
     hosts = Hosts()
     if enable:
         entry = HostsEntry(entry_type='ipv4',
                            address='127.0.0.1',
                            names=[
                                'us-or-rly101.zwift.com',
                                'secure.zwift.com', 'cdn.zwift.com',
                                'launcher.zwift.com'
                            ])
         hosts.add([entry])
     else:
         hosts.remove_all_matching(name='cdn.zwift.com')
     logger.info(hosts.write())
示例#33
0
def block():

    # Remove previous blocks to prevent duplicates
    progress.set( "Processing hosts file" )
    if sys.platform.startswith("win"):
        path = "C:\Windows\System32\drivers\etc\hosts"
    else:
        path = "/etc/hosts"
    hosts = Hosts(path)
    hosts.remove_all_matching(address="0.0.0.1")
    hosts.write()

    # Add domains to hosts file with IP 0.0.0.1
    # 8 domains in a row (hosts file limitation)
    # https://superuser.com/questions/932112/is-there-a-maxium-number-of-hostname-aliases-per-line-in-a-windows-hosts-file
    for i in range(0, len(data), 8):
        new_entry = HostsEntry(entry_type='ipv4', address='0.0.0.1', names=data[i:(i+8)])
        hosts.add([new_entry], False, True)
        progress.set( str( round(100*i/len(data),2)) + "%" )
    hosts.write()

    progress.set( "100 %" )
    status.set("DONE (You can close the program. Restart your browser.)")
示例#34
0
def test_existing_ipv6_addresses_are_preserved(tmpdir):
    """
    Test that existing ipv6 addresses are preserved after adding
    an ipv4 entry
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("fe80::1\tlocalhost\n6.6.6.6\texample.com\n# A test comment\n\n")
    hosts = Hosts(path=hosts_file.strpath)
    new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.132', names=['something.com', 'example'])
    hosts.add(entries=[new_entry], force=False)
    write_result = hosts.write()
    assert write_result.get('ipv6_entries_written') == 1
    assert write_result.get('ipv4_entries_written') == 2
    assert write_result.get('comments_written') == 1
    assert write_result.get('blanks_written') == 1
from python_hosts import Hosts, HostsEntry
import subprocess

my_hosts = Hosts(path="C:\\Windows\\System32\\drivers\\etc\\hosts")

print(" PointBlank Server UDP3 By MoMzGames ")
print("           RESTORE SERVER        ")
print("           by un4ckn0wl3z             ")
print("======================================")

my_hosts.remove_all_matching(address='45.76.187.53')
my_hosts.write()
result = subprocess.Popen("w32tm /resync",
                          shell=True,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)

output, error = result.communicate()

print(output)

result = subprocess.Popen("ipconfig /flushdns ",
                          shell=True,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)

output, error = result.communicate()

print(output)
    if sys.argv[1] == "link":
        code = sys.argv[2]
        result = requests.post(url, data={
            "command": "link",
            "content": code
        }).text.split(" ")
        if result == "Error: code not found in database":
            print("Error: invalid code")
        else:
            resultcombined = result[1] + result[0]
            hosts.add([
                HostsEntry(entry_type='ipv4',
                           address=result[1],
                           names=[result[0]])
            ])
            hosts.write()
            print("Code {} linked".format(code))
    elif sys.argv[1] == "add":
        domain = sys.argv[2]
        ip = sys.argv[4]
        data = domain + " " + ip
        result = requests.post(url, data={
            "command": "add",
            "content": data
        }).text
        if result == "Error: failed to bypass filter":
            print("Error: arguments given is not a domain or IP")
        else:
            print("Please copy this code: {}".format(result))
except IndexError:
    printguide()
    def action_confirm_client(self):

        try:
            new_client_id = False
            res_partner_bank_vals = {}
            res_partner_bank_obj = self.env['res.partner.bank']
            mail_server_obj = self.env['ir.mail_server']
            res_company_obj = self.env['res.company']
            clients_obj = self.env['saas_portal.client']
            ProductUOM = self.env['product.uom']

            # for country and state to be created in client db
            country_name = self.country_id.name
            country_code = self.country_id.code
            # state_name = self.state_id.name
            # state_code = self.state_id.code

            mail_server_rec = mail_server_obj.search([], limit=1)
            base_company = res_company_obj.sudo().search([], limit=1)
            self.check_outgoing_mail()

            if self.plan_id.plan_subscription_ids:
                pass
            else:
                raise Warning(
                    _('Sorry!!, Client will not be confirmed as no '
                      'Subscription is defined in the Plan '
                      'Subscription Page.'))

            res_partner_bank_obj = self.env['res.partner.bank']
            partner_iban_exist = res_partner_bank_obj.search(
                [('acc_number', '=', self.client_bank_acc),
                 ('iban', '=', self.client_bank_iban),
                 ('partner_id', '=', self.client_id.partner_id.id)],
                limit=1)

            other_partner_iban_exist = res_partner_bank_obj.search(
                [('acc_number', '=', self.client_bank_acc),
                 ('iban', '=', self.client_bank_iban),
                 ('partner_id', '!=', self.client_id.partner_id.id)],
                limit=1)

            if other_partner_iban_exist:
                raise Warning(
                    _("Bank Account no or IBAN already exist. "
                      "Please verify it again."))

            # Create his/her bank account detail and link to its partner
            if not (partner_iban_exist or other_partner_iban_exist):
                res_partner_bank_vals.update({
                    'bank_id':
                    self.client_bank_id.id,
                    'acc_number':
                    self.client_bank_acc,
                    'partner_id':
                    self.client_id.partner_id.id,
                    'iban':
                    self.client_bank_iban,
                })

                res_partner_bank_obj.sudo().create(res_partner_bank_vals)

            client_rec = clients_obj.sudo().search([
                ('partner_id', '=', self.client_id.partner_id.id),
                ('name', '=', self.database), ('state', '=', 'draft')
            ])

            # if alread the client is created by process stop due to server
            # disconnection or other sevrer related things then this will
            # delete the existing db and create another one
            if client_rec:
                client_rec._delete_database_server(force_delete=True)

            # ====== Keep this code for future use =================
            # if client_rec:
            #     new_client_id = self.plan_id.create_new_database(
            #         dbname = self.database,
            #         client_id = client_rec.client_id,
            #         partner_id = self.client_id.partner_id.id,
            #         user_id = self.client_id.id,
            #         notify_user = True,
            #         trial = False,
            #     )

            # Create New Databse with Subscription plan recordset
            trial = False
            if self.plan_type == 'trial':
                trial = True
            new_client_id = self.plan_id.create_new_database(
                dbname=self.database,
                partner_id=self.client_id.partner_id.id,
                user_id=self.client_id.id,
                notify_user=True,
                trial=trial,
            )

            new_client_rec = self.env['saas_portal.client'].browse(
                new_client_id['id'])
            vals = {
                'plan_price': self.plan_price,
                'sub_period': self.sub_period,
                'plan_type': self.plan_type,
                'merchant_id': self.merchant_id.id,
                'store_type': self.store_type,
                'req_no': self.request_no
                # 'store_id' : self.store_id.id,
            }
            new_client_rec.write(vals)

            line_dict = []
            analytic_account = self.env['account.analytic.account']
            account_invoice = self.env['account.invoice']

            ana_vals = {}
            if self.client_id.partner_id:
                ana_vals.update({
                    'partner_id':
                    self.client_id.partner_id.id,
                    'name':
                    'Subscription for ' + self.client_id.partner_id.name,
                    'recurring_rule_type':
                    self.plan_id.sub_period,
                    'recurring_invoices':
                    'True',
                    'client_id':
                    new_client_rec.id
                })

                quantity = 1
                if self.num_of_outlets > 0:
                    quantity = self.num_of_outlets

                for data in self.plan_id.plan_subscription_ids:
                    ana_vals_line = {}
                    ana_vals_line.update({
                        'product_id':
                        data.product_id.id,
                        'uom_id':
                        data.product_id.uom_id.id,
                        'name':
                        data.saas_prod_desc or data.product_id.name,
                        'price_unit':
                        data.subscription_price,
                        'quantity': (data.no_of_users * quantity) or 1
                    })
                    line_dict.append((0, 0, ana_vals_line))
                    ana_vals.update({'recurring_invoice_line_ids': line_dict})
                analytic_id = analytic_account.create(ana_vals)
                analytic_id.recurring_create_invoice()
                invoices = account_invoice.search([('contract_id', '=',
                                                    analytic_id.id)])
                invoices.action_invoice_open()
                new_client_rec.write({'subscription_id': analytic_id.id})

            res_user_vals = {
                'name': self.client_id.name,
                'login': self.client_id.login,
            }

            res_partner_vals = {
                'email': self.client_id.login,
                'website': self.company_website,
                'vat': self.vat,
                'mobile': self.contact_no,
                'phone': self.landline_no,
                'city': self.city,
                'street': self.street1,
                'street2': self.street2,
                'state_id': self.state_id.id or ''
                # 'zip' : self.zip,
            }

            # Host entry is done for the ip address and database name
            hosts = Hosts(path='/etc/hosts')
            new_entry = HostsEntry(entry_type='ipv4',
                                   address='127.0.0.1',
                                   names=[self.database])
            hosts.add([new_entry])
            hosts.write()

            # Cursor is being created of the new database created for the
            # User so that a reset password mail can be send to that user
            # of his database only
            new_cr = db_connect(self.database).cursor()
            old_vals = {
                'smtp_host': 'localhost',
                'smtp_port': 25,
                'smtp_encryption': 'none',
                'smtp_user': '',
                'smtp_pass': ''
            }

            # Outgoing mail server to be set in the users database as we
            # need to send the mail from users database
            new_vals = {
                'name': mail_server_rec.name,
                'sequence': mail_server_rec.sequence,
                'smtp_host': mail_server_rec.smtp_host,
                'smtp_port': mail_server_rec.smtp_port,
                'smtp_encryption': mail_server_rec.smtp_encryption,
                'smtp_user': mail_server_rec.smtp_user,
                'smtp_pass': mail_server_rec.smtp_pass
            }

            # Environment variable for the new db created by user
            new_env = Environment(new_cr, SUPERUSER_ID, {})
            # Get country, state and updated vals
            # country_rec, state_rec, res_partner_vals = \
            #     self.get_country_state_rec(new_env, country_name, country_code,
            #                                state_name, state_code,
            #                                res_partner_vals)
            country_rec, res_partner_vals = \
                self.get_country_state_rec(new_env, country_name, country_code,
                                           res_partner_vals)

            local_mail = new_env['ir.mail_server'].browse([1])
            # update the existing default outgoing mail server with main portal
            # outgoing mail server config
            local_mail.write(new_vals)

            # Base company vals to send base company email info in \
            # reset password mail template
            base_company_user_vals = {
                'logo': base_company.logo,
                'name': base_company.name,
                'rml_header1': base_company.rml_header1,
                'website': base_company.website,
                'phone': base_company.phone,
                'email': base_company.email,
            }

            # New user company name given by user at time of registration
            new_user_company_vals = {
                'logo': '',
                'name': self.company,
                'rml_header1': self.company,
                'website': self.company_website,
                'mobile': self.contact_no,
                'phone': self.landline_no,
                'email': self.client_id.partner_id.email,
                'vat': self.vat,
                'country_id': country_rec.id,
            }

            # finds the company in user database
            new_user_company = new_env['res.company'].search([], limit=1)
            # update the existing default company with the base company details
            new_user_company.write(base_company_user_vals)
            ir_config_obj = self.env['ir.config_parameter']
            client_url = new_env['ir.config_parameter'].get_param(
                'web.base.url')

            auth_vals = {
                'name':
                'Auth Provider for ' + self.client_id.name,
                'client_id':
                new_env['ir.config_parameter'].get_param('database.uuid'),
                'enabled':
                True,
                'body':
                'Login with Auth provider',
                'auth_endpoint':
                client_url + '/oauth2/auth',
                'scope':
                'userinfo',
                'validation_endpoint':
                client_url + '/oauth2/tokeninfo'
            }
            portal_provider = self.env['auth.oauth.provider'].create(auth_vals)

            self.client_id.write({
                'oauth_provider_id': portal_provider.id,
                'oauth_uid': 1
            })

            new_env['ir.config_parameter'].set_param('portal.database',
                                                     self.env.cr.dbname)
            new_env['ir.config_parameter'].set_param(
                'portal.url', ir_config_obj.get_param('web.base.url'))
            new_env['ir.config_parameter'].set_param('portal.provider',
                                                     portal_provider.id)
            new_env['ir.config_parameter'].set_param(
                'server.url', self.plan_id.server_id.name)
            new_cr.commit()

            # Search for the user in the new database for updating \
            # his related partner fields
            new_user = new_env['res.users'].search(
                [('login', '=', self.client_id.partner_id.email)], limit=1)
            new_res_partner = new_user.partner_id
            new_user.write(res_user_vals)
            new_res_partner.write(res_partner_vals)

            # Reset password action is called to send mail to the user
            new_user.action_reset_password_custom()
            new_cr.commit()
            # again replaces the old outgoing mail server in user db
            local_mail.write(old_vals)

            # again replace the company to the users company define
            new_user_company.write(new_user_company_vals)

            if self.store_type == 'multi':
                if self.num_of_outlets >= 0:
                    new_user_company.write({
                        'store_type':
                        self.store_type,
                        'number_of_outlets':
                        self.num_of_outlets
                    })
                    group_multi_company = new_env.ref(
                        'base.group_multi_company', False)
                    group_multi_company.write(
                        {'users': [(4, new_user.id), (4, 1)]})
            elif self.store_type == 'single':
                new_user_company.write({
                    'store_type': self.store_type,
                    'number_of_outlets': self.num_of_outlets
                })

            group_store_master = new_env.ref(
                'mint_client_multi_store.saas_store_manager')
            group_store_master.write({'users': [(4, new_user.id)]})
            new_cr.commit()
            new_cr.close()

            return self.write({'state': 'confirmed'})
        except Exception as e:
            raise UserError(
                _("Something Went Wrong while Creating the "
                  "Database. Instead We Got:\n%s") % ustr(e))
示例#38
0
def append_hosts_entry(address, names):
    new_entry = HostsEntry(entry_type='ipv4', address=address, names=names)
    my_hosts = Hosts()
    my_hosts.add([new_entry])
    my_hosts.write()