[CMD/Batch] Скрипт удаления hosts и пересоздание новый hosts с текстом

  • Автор темы Автор темы Hunter
  • Дата начала Дата начала

Hunter

Опытный участник
Сообщения
800
Решения
2
Реакции
77
Пример cкрипт удаление hosts и пересоздать новый hosts с текстом
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

127.0.0.1 localhost



Сделать через bat
 
Последнее редактирование модератором:
Пример cкрипт удаление hosts и пересоздать новый hosts с текстом
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

127.0.0.1 localhost
:


Сделать через bat
Пример скрипта на Batch для удаления файла hosts и создания нового файла с указанным содержимым:

Код:
@echo off
set hosts_file=%windir%\System32\drivers\etc\hosts

echo Deleting old hosts file...
if exist "%hosts_file%" (
    attrib -r -s -h "%hosts_file%"
    del "%hosts_file%"
)

echo Creating new hosts file...
echo # Copyright (c) 1993-2009 Microsoft Corp. > "%hosts_file%"
echo # >> "%hosts_file%"
echo # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. >> "%hosts_file%"
echo # >> "%hosts_file%"
echo # This file contains the mappings of IP addresses to host names. Each >> "%hosts_file%"
echo # entry should be kept on an individual line. The IP address should >> "%hosts_file%"
echo # be placed in the first column followed by the corresponding host name. >> "%hosts_file%"
echo # The IP address and the host name should be separated by at least one >> "%hosts_file%"
echo # space. >> "%hosts_file%"
echo # >> "%hosts_file%"
echo # Additionally, comments (such as these) may be inserted on individual >> "%hosts_file%"
echo # lines or following the machine name denoted by a '#' symbol. >> "%hosts_file%"
echo # >> "%hosts_file%"
echo # For example: >> "%hosts_file%"
echo # >> "%hosts_file%"
echo #      102.54.94.97     rhino.acme.com          # source server >> "%hosts_file%"
echo #       38.25.63.10     x.acme.com              # x client host >> "%hosts_file%"
echo # >> "%hosts_file%"
echo. >> "%hosts_file%"
echo # localhost name resolution is handled within DNS itself. >> "%hosts_file%"
echo #    127.0.0.1       localhost >> "%hosts_file%"
echo #    ::1             localhost >> "%hosts_file%"
echo. >> "%hosts_file%"
echo 127.0.0.1       localhost >> "%hosts_file%"
echo : >> "%hosts_file%"

echo Done.
pause

Этот скрипт удаляет старый файл hosts, создает новый файл hosts и записывает в него указанный текст.

Переменная hosts_file задает путь к файлу hosts в системной папке Windows.

Сначала скрипт проверяет наличие старого файла hosts и удаляет его, если он существует.

Затем создается новый файл hosts с указанным содержимым с помощью команды echo и перенаправления вывода в файл.

Каждая строка текста записывается отдельной командой echo, а символы перевода строки обозначаются специальной командой echo без параметров.

После записи текста в файл hosts скрипт выводит сообщение "Done." и ждет нажатия любой клавиши с помощью команды pause.
 
Пример cкрипт удаление hosts и пересоздать новый hosts с текстом , без права администратора , принудительная .
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

127.0.0.1 localhost



Сделать через bat
 
Пример cкрипт удаление hosts и пересоздать новый hosts с текстом , без права администратора , принудительная .
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

127.0.0.1 localhost



Сделать через bat
Если скрипт должен выполняться без прав администратора и принудительно, то он не сможет удалить и пересоздать файл hosts в стандартной папке Windows. Вместо этого можно создать новый файл hosts в другой папке и затем скопировать его в папку System32\drivers\etc с помощью команды xcopy.

Пример скрипта на Batch для создания нового файла hosts и его копирования в папку System32\drivers\etc:

Код:
@echo off
set hosts_file=%userprofile%\hosts

echo Creating new hosts file...
echo # Copyright (c) 1993-2009 Microsoft Corp. > "%hosts_file%"
echo # >> "%hosts_file%"
echo # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. >> "%hosts_file%"
echo # >> "%hosts_file%"
echo # This file contains the mappings of IP addresses to host names. Each >> "%hosts_file%"
echo # entry should be kept on an individual line. The IP address should >> "%hosts_file%"
echo # be placed in the first column followed by the corresponding host name. >> "%hosts_file%"
echo # The IP address and the host name should be separated by at least one >> "%hosts_file%"
echo # space. >> "%hosts_file%"
echo # >> "%hosts_file%"
echo # Additionally, comments (such as these) may be inserted on individual >> "%hosts_file%"
echo # lines or following the machine name denoted by a '#' symbol. >> "%hosts_file%"
echo # >> "%hosts_file%"
echo # For example: >> "%hosts_file%"
echo # >> "%hosts_file%"
echo # 102.54.94.97 rhino.acme.com # source server >> "%hosts_file%"
echo # 38.25.63.10 x.acme.com # x client host >> "%hosts_file%"
echo # >> "%hosts_file%"
echo. >> "%hosts_file%"
echo # localhost name resolution is handled within DNS itself. >> "%hosts_file%"
echo # 127.0.0.1 localhost >> "%hosts_file%"
echo # ::1 localhost >> "%hosts_file%"
echo. >> "%hosts_file%"
echo 127.0.0.1 localhost >> "%hosts_file%"
echo. >> "%hosts_file%"

echo Copying new hosts file to system folder...
xcopy /y "%hosts_file%" "%windir%\System32\drivers\etc\hosts" >nul

echo Done.
pause

Этот скрипт создает новый файл hosts в папке пользователя (%userprofile%) с указанным содержимым.

Затем он копирует новый файл hosts в папку System32\drivers\etc с помощью команды xcopy.

Ключ /y указывает, что при копировании файлов нужно подтверждать их
 
Пример cкрипт удаление hosts и пересоздать новый hosts с текстом , а потом удаление файл в первую очередь , а затем закачать новый файл через Batch
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

127.0.0.1 localhost
 
Пример cкрипт удаление hosts и пересоздать новый hosts с текстом , а потом удаление файл в первую очередь , а затем закачать новый файл через Batch
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

127.0.0.1 localhost
Пример скрипта на Batch для удаления старого файла hosts, создания нового файла hosts и его копирования в папку System32\drivers\etc:

Код:
@echo off
set hosts_file=%userprofile%\hosts
set system_hosts_file=%windir%\System32\drivers\etc\hosts

echo Deleting old hosts file...
if exist "%system_hosts_file%" (
    attrib -r -s -h "%system_hosts_file%"
    del /f /q "%system_hosts_file%"
)

echo Creating new hosts file...
echo # Copyright (c) 1993-2009 Microsoft Corp. > "%hosts_file%"
echo # >> "%hosts_file%"
echo # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. >> "%hosts_file%"
echo # >> "%hosts_file%"
echo # This file contains the mappings of IP addresses to host names. Each >> "%hosts_file%"
echo # entry should be kept on an individual line. The IP address should >> "%hosts_file%"
echo # be placed in the first column followed by the corresponding host name. >> "%hosts_file%"
echo # The IP address and the host name should be separated by at least one >> "%hosts_file%"
echo # space. >> "%hosts_file%"
echo # >> "%hosts_file%"
echo # Additionally, comments (such as these) may be inserted on individual >> "%hosts_file%"
echo # lines or following the machine name denoted by a '#' symbol. >> "%hosts_file%"
echo # >> "%hosts_file%"
echo # For example: >> "%hosts_file%"
echo # >> "%hosts_file%"
echo # 102.54.94.97 rhino.acme.com # source server >> "%hosts_file%"
echo # 38.25.63.10 x.acme.com # x client host >> "%hosts_file%"
echo # >> "%hosts_file%"
echo. >> "%hosts_file%"
echo # localhost name resolution is handled within DNS itself. >> "%hosts_file%"
echo # 127.0.0.1 localhost >> "%hosts_file%"
echo # ::1 localhost >> "%hosts_file%"
echo. >> "%hosts_file%"
echo 127.0.0.1 localhost >> "%hosts_file%"
echo. >> "%hosts_file%"

echo Copying new hosts file to system folder...
copy /y "%hosts_file%" "%system_hosts_file%" >nul

echo Done.
pause

Этот скрипт удаляет старый файл hosts, создает новый файл hosts в папке пользователя (%userprofile%) и копирует его в папку System32\drivers\etc.

Ключи /f и /q команды del указывают, что нужно удалить файл без подтверждения и без вывода сообщений об ошибке.

Ключ /y команды copy указывает, что нужно копировать файл без подтверждения.

Обратите внимание, что для выполнения этого скрипта требуются права администратора. Если запустить этот скрипт без прав администратора, то он не сможет удалить старый файл hosts и скопировать новый файл в папку System32\drivers\etc.
 
Назад
Сверху Снизу