Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows cmd scripts to change hosts permissions #43

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions cmd/win-hosts-check.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@ECHO off
SETLOCAL ENABLEEXTENSIONS
REM modify hosts access rights current user can change the file contents

call :hostfileperms _perms
echo Permissins on hosts for user %USERNAME%: %_perms%

If "%_perms%" == "F" goto :PERM_OK
If "%_perms%" == "M" goto :PERM_OK
If "%_perms%" == "RX,W" goto :PERM_OK
If "%_perms%" == "R,W" goto :PERM_OK
If "%_perms%" == "W" goto :PERM_OK

If "%_perms%" == ":" goto :PERM_INSUFFICIENT
If "%_perms%" == "R" goto :PERM_INSUFFICIENT
If "%_perms%" == "RX" goto :PERM_INSUFFICIENT

echo Unhandled "%_perms%", raising.
goto :PERM_INSUFFICIENT

:PERM_INSUFFICIENT
echo|set /p=Raising permissions...
call win-hosts-set.cmd
echo|set /p=done.
call :hostfileperms _perms_new
echo New permissins are: %_perms_new%
IF "%_perms%" == "%_perms_new%" (
echo Changing permissions failed. Exiting.
exit /b 1
)

:PERM_OK
echo Suffice. Exiting.
goto :EOF

:hostfileperms
setlocal
set _icacls_cmd=Icacls %SystemRoot%\system32\drivers\etc\hosts /T
FOR /f "tokens=2" %%G IN ('%_icacls_cmd% ^|find "%USERNAME%"') DO set _permission_info=%%G
set "_permission_perms=%_permission_info:*:=%"
set _permission=%_permission_perms:~1,-1%

( endlocal
set "%1=%_permission%"
)
exit /b
13 changes: 13 additions & 0 deletions cmd/win-hosts-set.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@if (1==1) @if(1==0) @ELSE
@echo off&SETLOCAL ENABLEEXTENSIONS
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"||(
cscript //E:JScript //nologo "%~f0"
@goto :EOF
)
CACLS %SystemRoot%\system32\drivers\etc\hosts /E /G %USERNAME%:W
@goto :EOF
@end @ELSE
ShA=new ActiveXObject("Shell.Application")
ShA.ShellExecute("cmd.exe","/c \""+WScript.ScriptFullName+"\"","","runas",5);
WScript.Sleep(500)
@end