Reputation: 241
I got the codes below. Here is my situation, I want to copy my modified hosts the host file in each of my clients units. But some of my clients hosts file was already modified(and I dont want to mess it, they are allowed to access these some site) while some host arent modified.
I want my code to check if atleast one host entry is written in the host file, if it finds atleast one entry it wont copy my modified hosts file.
@echo off
@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a:%%b)
echo "Host File modification starts at %mydate% %mytime% " >> "%~dp0Host_log.txt"
for /F "UseBackQ" %%M in ("%~dp0Modify_Host.txt") do (
ping -n 1 -w 1 %%M >nul 2>&1
If ErrorLevel 1 (Echo=%%M is Down %mytime%>>"%~dp0Host_log.txt") Else (
FIND /C /I "www.facebook.com" \\%%M\C$\Windows\System32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 1 (goto END)
FIND /C /I "https://m.www.facebook.com" \\%%M\C$\Windows\System32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 1 (goto END)
FIND /C /I "edge-star-shv-01-sit4.facebook.com" \\%%M\C$\Windows\System32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 1 goto END
FIND /C /I "login.facebook.com" \\%%M\C$\Windows\System32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 1 goto END
FIND /C /I "fbcdn.net" \\%%M\C$\Windows\System32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 1 goto END
FIND /C /I "https://m.facebook.com" \\%%M\C$\Windows\System32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 1 goto END
FIND /C /I "www.fbcdn.com" \\%%M\C$\Windows\System32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 1 goto END
FIND /C /I "static.ak.fbcdn.net" \\%%M\C$\Windows\System32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 1 goto END
FIND /C /I "static.ak.connect.facebook.com" \\%%M\C$\Windows\System32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 1 goto END
FIND /C /I "connect.facebook.net" \\%%M\C$\Windows\System32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 1 goto END
FIND /C /I "apps.facebook.com" \\%%M\C$\Windows\System32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 1 goto END
FIND /C /I "www.facebook.com" \\%%M\C$\Windows\System32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 1 goto END
FIND /C /I "ns2.intranet.de" \\%%M\C$\Windows\System32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 1 (goto END) ELSE (
copy /Y "%~dp0hosts" \\%%M\c$\windows\system32\drivers\etc
)
:END
Echo Host file in %%M is already modified
)
)
pause
But either it detects a certain host entry or not, my code will copy my modified host to the remote PC. Please help me, thanks.
Upvotes: 0
Views: 604
Reputation:
It's IMO easier to put the checks into a sub where you can return on any fail to the call in a for loop. I don't use If's but conditional execution on fail ||
or success &&
.
@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do set "mydate=%%c-%%a-%%b"
Set Log=^>^>"%~dp0Host_log.txt"
%Log% echo=[%time:/=:%] Host File modification starts at %mydate%
for /F "UseBackQ" %%M in ("%~dp0Modify_Host.txt") do Call :CheckM "%%M"
goto :Eof
:CheckM
ping -n 1 -w 1 %1 >nul 2>&1 || (
%Log% Echo=[%time:/=:%] %~1 is Down
Goto :Eof
)
Set "RemoteHosts=\\%~1\C$\Windows\System32\drivers\etc\hosts"
For %%U in (
"apps.facebook.com"
"connect.facebook.net"
"edge-star-shv-01-sit4.facebook.com"
"fbcdn.net"
"https://m.facebook.com"
"https://m.www.facebook.com"
"login.facebook.com"
"ns2.intranet.de"
"static.ak.connect.facebook.com"
"static.ak.fbcdn.net"
"www.facebook.com"
"www.fbcdn.com"
) do FIND /C /I %%U "%RemoteHosts%" >Nul 2>&1 && (
Echo [%time:/=:%] Host file in %~1 is already modified
Goto :Eof
)
Echo copy /Y "%~dp0hosts" "\\%~1\c$\windows\system32\drivers\etc"
copy /Y "%~dp0hosts" "\\%~1\c$\windows\system32\drivers\etc"
pause
Upvotes: 2