0

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.

Regie Baguio
  • 241
  • 5
  • 13
  • 2
    To edit the hosts file on the local machine you'll need admin privileges . – npocmaka Mar 24 '17 at 11:43
  • Yes, Im aware of it. I have admin rights in every of my clients units. The things is, my file already copied my modified hosts file. There is no issue about copying. what I'm talking about is it copies even if it detects a certain entry if I dont want it to copy. – Regie Baguio Mar 24 '17 at 11:54
  • aside from LotPings much nicer solution, your main problem is [delayed expansion](http://stackoverflow.com/a/30284028/2152082) with the `errorlevel` variable. And you should rethink your logic. Somehow it makes no sense. – Stephan Mar 24 '17 at 13:08
  • I really think that the most robust method is to check for a difference between the hosts file's modified and created date and time. A difference shows that it has been changed and should be bypassed. – Compo Mar 24 '17 at 17:49
  • @Compo No, by the time a unit encounters an resolvable issue, we will re-image the unit(you know, freshly installed OS). So time referring wont be that effective. – Regie Baguio Mar 28 '17 at 01:50
  • 1
    Okay then, if the 'modified' differs from the known stamp from your imaged file; then the hosts has been altered post image. _(You should also be able to make a similar check using the file size)_. I would also suggest that you change the hosts pre-image if you have a reimaging strategy. _(It just seems OTT to read the file searching for specific site entries just to determine if it has been altered)_. – Compo Mar 28 '17 at 07:31
  • @Compo Sounds good, Ill try to implement your suggestion. Check the file size in bytes is more accurate than kilobytes. Ill just post another question if Ill encounter difficulties, thanks. +1 – Regie Baguio Mar 28 '17 at 13:34

1 Answers1

2

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
  • But the loop stops once it copies my modified hosts file. I want it to continue. is there any possible solution. I bet placing a for loop into a for loop isn't lawful. – Regie Baguio Mar 28 '17 at 07:13