Войти

Показать полную графическую версию : Проверка файла host


Sisin
24-04-2015, 11:01
Уважаемые форумчане, подсобите, пожалуйста,

Я хочу, чтобы при запуске CMD-файла проверялся файл host на наличие строчек:
127.0.0.1 rad.msn.com
127.0.0.1 apps.skype.com
127.0.0.1 download.skype.com
127.0.0.1 g.msn.com
127.0.0.1 cdn.eyewonder.com

Если строчки присутствуют, то CMD-файл исполняется дальше, если строчек нет, они добавляются и CMD-файл исполняется дальше.

Подскажите, как это реализовать???

Sisin
24-04-2015, 11:06
Для полной замены host я использую скрипт:

@echo off

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

ipconfig /flushdns
pause

Но мне надо, чтобы проверялось наличие конкретных строчек.

Iska
24-04-2015, 13:04
@echo off
setlocal enableextensions enabledelayedexpansion

set sSourceFile=C:\Песочница\069\list.txt
set sDestFile=%SystemRoot%\system32\drivers\etc\hosts

if exist "%sSourceFile%" (
if exist "%sDestFile%" (
for /f "usebackq delims=" %%i in ("%sSourceFile%") do (
type "%sDestFile%" | >nul 2>&1 find.exe /i "%%~i" || (
echo Add string [%%~i].
>>"%sDestFile%" echo %%~i
)
)
) else (
echo Can't find destination file [%sDestFile%].
exit /b 2
)
) else (
echo Can't find source file [%sSourceFile%].
exit /b 1
)

endlocal
exit /b 0

Sisin
24-04-2015, 13:22
А как сделать без дополнительного файла list.txt???
Чтобы строчки были прописаны в cmd.

Iska
24-04-2015, 13:27
Sisin, по-хорошему, конечно, вместо «find.exe» надо пользовать «findstr.exe» с указанием в маске начала и конца строки, наподобие:
type "%sDestFile%" | >nul 2>&1 findstr.exe /i /r /c:"^%%~i$" || (

а то попадётся в исходном файле что-нибудь этакое:
127.0.0.1 g.msn.com.ru
127.0.0.1 g.msn.com
— и будет ошибочно определено, что вторая строка уже есть в целевом файле (по совпадению подстроки).

А как сделать без дополнительного файла list.txt???
Чтобы строчки были прописаны в cmd. »
А чем оно Вам мешает — отдельным файлом? Возни чуть больше.

Sisin
24-04-2015, 13:35
А чем оно Вам мешает — отдельным файлом? Возни чуть больше. »

а как сделать относительный путь до list.txt???

Iska
24-04-2015, 13:51
а как сделать относительный путь до list.txt??? »
Относительно самого пакетного файла? Например, так:
set sSourceFile=%~dp0list.txt
— в том же каталоге, что и пакетный файл.

Sisin
24-04-2015, 13:58
У меня после запуска дублируются строчки.
т.е. 127.0.0.1 download.skype.com уже была, а после запуска таких строчек стало две.

Подумал, может пробелы в конце мешают, но нет не из-за них...

Iska
24-04-2015, 14:10
У меня после запуска дублируются строчки. »
Упакуйте в архив Ваш файл «host» и файл «list.txt». Выложите архив сюда или на RGhost.

Sisin
24-04-2015, 14:18
Я разобрался, они были в разных кодировках.

Спасибо, Iska.




© OSzone.net 2001-2012