Modifying Host File Using batch file - batch-file

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.

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

Related

How to set up scripts in to a Choice Menu with Batch

I'm building a menu with this command CHOICE /c 123456789 /N /M "Type your choise"
This is inside a my menu batch script and it calls Tools1.bat it makes everything inside the Tools.bat run with Sub folder reader
IF %ERRORLEVEL% EQU 1 (
#For /F "EOL=? Delims=" %%G In ('Dir /B /S /A:D') Do #PushD "%%G" && (Call "%~dp0Tools1.bat" >NUL 2>&1 & PopD)
)
Tools.bat
ren "asset_database_?_*_?_Release" Asset_Database_Cleaned_0.txt
findstr /i "icon" Asset_Database_Cleaned_0.txt > Asset_Database_Cleaned_1.txt
findstr /i "[(0-9)]" Asset_Database_Cleaned_0.txt > Asset_Database_Cleaned_2.txt
ren "Asset_Database_Cleaned_0.txt" asset_database_9_99_9_Release
ren "%~dp01_Prior_Version\Asset_Database_Cleaned_2.txt" "Prior Version.txt"
ren "%~dp02_Current_Version\Asset_Database_Cleaned_2.txt" "Current Version.txt"
)
I have 2 folders with one file inside each folder
01_Prior_Version
+ -- asset_database_2_09_7_Release
02_Current_Version
+ -- asset_database_3_29_4_Release
the asset_database has random numbers so I use wildcards for that
asset_databaseRelease or asset_database_?__?_Release
These files get renamed to Asset_Database_Cleaned_0.txt
and then I use findstr /i to extract content that I need
I have tried many different ways to add that top script to my menu batch script
I have tried many different ways to add a Sub folder reader using SET & Source etc
IF %ERRORLEVEL% EQU 1 (
#For %%G In ("%~dp01_Prior_Version") Do Set "source=%%~fG"
#For %%G In ("%~dp02_Prior_Version") Do Set "target=%%~fG"
Set "Source1=1_Prior_Version\Asset_Database_Cleaned_1.txt"
Set "Target1=1_Prior_Version\Prior Version.txt"
findstr /i "icon [(0-9)]" %Source1% > %Target1%
ren "Prior\Asset_Database_Cleaned_0.txt" asset_database_9_99_9_Release
I tried using this script, but couldn't put all of that above together
#echo off
FOR /F %%i in ('dir /b/s/a-d') DO (
if "%%~xi" == "" rename "%%~fi" "%%~ni.txt"
)
This is my menu
:Data Finder
echo.
ECHO ################################################################
ECHO ALL YOUR ORIGINAL FILES WILL BE SAVED
ECHO ################################################################
echo.
ECHO Press 1 - Extract Data
ECHO Press 2 - Run PY
echo.
ECHO Thank You
ECHO For all the support
echo.
CHOICE /c 123456789 /N /M "Type your choise"
IF %ERRORLEVEL% EQU 1 (
#For /F "EOL=? Delims=" %%G In ('Dir /B /S /A:D') Do #PushD "%%G" && (Call "%~dp0Tools1.bat" >NUL 2>&1 & PopD)
goto=:Data Finder
)
IF %ERRORLEVEL% EQU 2 (
Call "%~dp0Tools2.py"
goto=:Data Finder
)
When I add this in, the CMD just closes

Was unexpected at this time. Error level correcting and continue

I am trying to have my batch file go to a spot if something was unexpected at this time instead of killing the program. This is what I have so far.
:StartConvo
if %errorlevel% neq 0 (goto skiperror & /b %errorlevel%>>ErrorLog.txt) else (goto skiperror)
:skiperror
set InVar=
set /p InVar=%Encrypt%:%=%
If /I "%InVar%"=="/c" goto Commands
If /I "%InVar%"=="/cls" goto CryptoClear
If /I "%InVar%"=="/Releave" goto Kick
If /I "%InVar%"=="/Test" goto Test
If /I "%InVar%"=="/Tuna" goto Tuna
If /I "%InVar%"=="/SProfile" goto Usrchk
If /I "%InVar%"=="/Login" goto Login
If /I "%InVar%"=="/USRC" goto USRC
If /I "%InVar%"=="/msg" goto msg
If /I "%InVar%"=="/photo" goto photo
If /I "%InVar%"=="/Pic" goto photo
If /I "%InVar%"=="/Pics" goto photo
If /I "%InVar%"=="/Photos" goto photo
If /I "%InVar%"=="/Pictures" goto photo
If /I "%InVar%"=="/Picture" goto photo
If /I "%InVar%"=="/log" goto log
If /I "%InVar%"=="/sticker" goto sticker
If /I "%InVar%"=="/expandedstorage" goto expandedstorage
If /I "%InVar%"=="/es" goto expandedstorage
If /I "%InVar%"=="/History" goto History
If /I "%InVar%"=="/CL" goto Changelog
find /c "%Encrypt%" kick.txt >NUL
if %errorlevel% equ 0 goto Kicked
find /c "%Encrypt%" T.txt >NUL
if %errorlevel% equ 0 goto Tunaed
#echo #%Encrypt%:%InVar%>> msg.txt
find /c ":" %Encrypt%.usr >NUL
if %errorlevel% equ 0 goto privtxt
GOTO StartConvo
If someone types << || && it breaks the program how do I fix this?
The error occurs, when you use the string without quotes (quotes make poison chars like &<>| safe). That happens with your #echo line.
Replace
#echo #%Encrypt%:%InVar%>> msg.txt
with
for /f "delims=" %%a in ("#%Encrypt%:%InVar%") do >>msg.txt #echo %%a
(Note: can still break when there are quotes in the input string)

Can someone help me understand the code that obfuscated my file so that I can reverse it? [duplicate]

This question already has an answer here:
I need convert UTF-16 to ANSI [closed]
(1 answer)
Closed 2 years ago.
Recently I obfuscated my own code for a password protection and used
#echo off
if "%~1"=="" exit /b
if /i "%~x1" neq ".bat" if /i "%~x1" neq ".cmd" exit /b
for /f %%i in ("certutil.exe") do if not exist "%%~$path:i" (
echo CertUtil.exe not found.
pause
exit /b
)
>"temp.~b64" echo(//4mY2xzDQo=
certutil.exe -f -decode "temp.~b64" "%~n1o%~x1"
del "temp.~b64"
copy "%~n1o%~x1" /b + "%~1" /b
and Im not sure how I should reverse it so that I can change my password.
Refer to this answer here or here too
You can drag and drop your obfuscated file over this batch file in order to read an comes back your original code !
#echo off
if "%~1"=="" exit /b
if /i "%~x1" neq ".bat" if /i "%~x1" neq ".cmd" exit /b
if exist "%~n1___%~x1" del "%~n1___%~x1"
for /f "skip=1 delims=" %%L in ('CMD /U /C Type "%~1"') do (
echo %%L
echo %%L >>"%~n1___%~x1"
)
pause>nul
OR this :
#echo off &setlocal
if "%~1"=="" exit /b
if /i "%~x1" neq ".bat" if /i "%~x1" neq ".cmd" exit /b
<"%~1" ((for /l %%N in (1 1 8) do pause)>nul&findstr "^">"%~n1___%~x1")

BATCH issue with second Choice

I'm currently working on some batch file that will be deleting all files from selected usb drive. The code works, but i wanted to add second choice to be sure if user is certain that he choose correct drive or so and the second choice is not responding. No matter which option i'll choose it'll somehow delete all files from this point.
I'm new to batch and to programming also
Here's a code:
#echo off
choice /c YN /t 15 /d n /m "Do you want to delete all files from USB drive? Y-yes, N-no"
setlocal enabledelayedexpansion
Set "USB="
if errorlevel == 1 goto ONE
if errorlevel == 2 goto TWO
if errorlevel == 255 goto ERROR
:ONE
for /f "tokens=1-5" %%a in (
'wmic logicaldisk list brief'
) do if %%b Equ 2 if %%d gtr 0 Set USB=!USB! %%a
Echo:Found drive's:%USB%
set /p Drive=Choose drive:
if "%Drive%"=="" goto :ERROR
if not exist %drive%:\ goto :ERROR
if %drive% EQU C goto ERROR
if %drive% EQU D goto ERROR
cd /D %Drive%:
tree
:CHOICE
choice /c YN /t 15 /d N /m "Are you sure you want to delete all files? Y-yes, N-no"
if errorlevel == 1 goto DELETE
if errorlevel == 2 goto TWO
goto END
:TWO
echo "Program was cancelled"
goto END
:DELETE
del * /S /F /Q
rmdir /S /Q %Drive%:
echo "Files are deleted"
goto END
:ERROR
echo "There was an error"
goto end
:END
echo "Done"
pause
Here's an example, which includes a more thorough method of detecting USB drives.
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For /F "Skip=2 Tokens=*" %%A In ('WMIC DiskDrive Where InterfaceType^="USB"^
Assoc /AssocClass:Win32_DiskDriveToDiskPartition 2^>Nul') Do (
For /F UseBackQ^ Delims^=^"^ Tokens^=2 %%B In ('%%A') Do (
For /F Delims^=^":^ Tokens^=6 %%C In (
'WMIC Path Win32_LogicalDiskToPartition^|Find "%%B"') Do (
For /F "Skip=1 Delims=" %%D In ('WMIC LogicalDisk Where^
"DeviceID='%%C:'" Get DeviceID^, VolumeName') Do Echo( %%D
Set "_C=!_C!%%C")))
If Not Defined _C Echo( You do not have a USB drive connected && GoTo :EndIt
If "%_C:~,1%" Equ "%_C%" GoTo :Picked
Echo( Enter the USB drive letter from the above [%_C%]:
For /F "Delims=? Tokens=2" %%A In ('Choice /C %_C%') Do Set "Letter=%%A:"
:Picked
If Not Defined Letter (Call :Task %_C%:) Else (Call :Task %Letter%)
:EndIt
>Nul Timeout 5
Exit/B
:Task
Choice /C YN /T 15 /D N /M "Do you want to delete all files from %1"
If ErrorLevel 2 Exit /B
REM Place your commands here for deleting from the selected drive
I think that the errorlevel's value will be replaced at the first if. I hope that this will help you:
#echo off
setlocal enabledelayedexpansion
Set "USB="
choice /c YN /t 15 /d n /m "Do you want to delete all files from USB drive? Y-yes, N-no"
set x=%errorlevel%
If %x%==1 goto ONE
If %x%==2 goto TWO
:ONE
for /f "tokens=1-5" %%a in (
'wmic logicaldisk list brief'
) do if %%b Equ 2 if %%d gtr 0 Set USB=!USB! %%a
Echo:Found drive's:%USB%
set /p Drive=Choose drive:
if "%Drive%"=="" goto ERROR
if not exist %drive%:\ goto :ERROR
if %drive% EQU C goto ERROR
if %drive% EQU D goto ERROR
cd /D %Drive%:
tree
:CHOICE
choice /c YN /t 15 /d N /m "Are you sure you want to delete all files? Y-yes, N-no"
Set x=%errorlevel%
if %x% == 1 goto DELETE
if %x% == 2 goto TWO
:TWO
echo "Program was cancelled"
goto END
:DELETE
del * /S /F /Q
rmdir /S /Q %Drive%:
echo "Files are deleted"
goto END
:ERROR
echo "There was an error"
goto end
:END
echo "Done"
pause

Windows batch file for invalid microsoft updates

I am struck while creating a windows batch file which just indicates if an invalid KB article is installed on my computer/ windows server.
This is where i am at now,
Script :
#ECHO OFF
WMIC QFE GET HOTFIXID>%~dp0QFE_list.txt
FOR /f "delims=," %%a IN (%~dp0Patch_List.txt) DO (
CALL :PATCH_LIST %%a
)
GOTO :EOF
:PATCH_LIST
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /s /f "%1">NULL.txt
IF %ERRORLEVEL% EQU 0 ECHO %1: INSTALLED
IF %ERRORLEVEL% NEQ 0 (
ECHO FIND %1
FIND /C "%1" %~dp0QFE_List.txt>NULL.txt
IF ERRORLEVEL 0 ECHO %1: QFE INSTALLED
IF ERRORLEVEL 1 ECHO %1: **** NOT INSTALLED! ****
)
Current output : ---------- C:\USERS\PVENK17\DESKTOP\TEST\QFE_LIST.TXT: 1
Desired output : : Installed
Inputfile contents : KB3057839,KB3002657
Issue :
Even though it works for 1 KBarticle. When i place more than 1 in the inputfile it is not working.
Kindly help me resolve this issue.
Thanks
Prashanth
I can make this work by changing the input file format: 1 KB per line
KB3057839
KB3002657
Then, just remove the "delims" stuff and it works for several items. Not sure of the logic of the last lines. It seems to say "installed/not installed"
And BTW redirect your commands to NUL to avoid creating a useless file.
#ECHO OFF
WMIC QFE GET HOTFIXID>%~dp0QFE_list.txt
FOR /f %%a IN (%~dp0Patch_List.txt) DO (
CALL :PATCH_LIST %%a
)
GOTO :EOF
:PATCH_LIST
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /s /f "%1">NUL
IF %ERRORLEVEL% EQU 0 ECHO %1: INSTALLED
IF %ERRORLEVEL% NEQ 0 (
ECHO FIND %1
FIND /C "%1" %~dp0QFE_List.txt>NUL
IF ERRORLEVEL 0 ECHO %1: QFE INSTALLED
IF ERRORLEVEL 1 ECHO %1: **** NOT INSTALLED! ****
)
After much more debugging i wrote this code, i know its not ideal solution but it works
Code :
#ECHO OFF
title IllegalPatchCheck
echo Select a server. (AW/PG)
set /p server=
IF /i "%server%"=="AW" goto AdminWorkstation
IF /i "%server%"=="PG" goto PeripheralGateway
echo Invalid Input.
goto commonexit
:AdminWorkstation
WMIC QFE GET HOTFIXID>%~dp0QFE_list.txt
find /c "KB3057839" %~dp0QFE_list.txt>nul
if %errorlevel% equ 1 ECHO KB3057839 NOT Found
if %errorlevel% neq 1 ECHO KB3057839 Found
find /c "KB3058515" %~dp0QFE_list.txt>nul
if %errorlevel% equ 1 ECHO KB3057839 NOT Found
if %errorlevel% neq 1 echo KB3058515 Found
find /c "KB3059317" %~dp0QFE_list.txt>nul
if %errorlevel% equ 1 echo KB3059317 NOT found
if %errorlevel% neq 1 echo KB3059317 Found
find /c "KB3063858" %~dp0QFE_list.txt>nul
if %errorlevel% equ 1 echo KB3063858 NOT found
if %errorlevel% neq 1 echo KB3063858 Found
goto commonexit
:PeripheralGateway
WMIC QFE GET HOTFIXID>%~dp0QFE_list.txt
find /c "KB2984972" %~dp0QFE_list.txt>nul
if %errorlevel% equ 1 echo KB2984972 NOT Found
if %errorlevel% neq 1 echo KB2984972 Found
find /c "KB3046049" %~dp0QFE_list.txt>nul
if %errorlevel% equ 1 echo KB3046049 NOT Found
if %errorlevel% neq 1 echo KB3046049 Found
find /c "KB3002657" %~dp0QFE_list.txt>nul
if %errorlevel% equ 1 echo KB3002657 NOT Found
if %errorlevel% neq 1 echo KB3002657 Found
goto commonexit
:commonexit
del /q /f %~dp0QFE_list.txt >nul
pause
Thanks
Prashanth
#echo off
title HOTFIXID_KB_FOUND
setlocal enabledelayedexpansion
cd /d "%~dp0"
for /f %%A in (KB_list.txt) do (
wmic qfe get hotfixid |findstr /i "%%A"
if !errorlevel! equ 0 echo %%A: ****INSTALLED****
if !errorlevel! equ 1 echo %%A: NOT INSTALLED
)
pause
KB_list.txt, in my case, is the list of the harmful updates leading to BSOD. You can, of course, automate and delete, but not always get wusa.exe sometimes requires dism.exe.
An example of removing using wusa.exe
wusa.exe /uninstall /kb:3065987 /quiet /norestart
An example of removing using dism.exe
DISM /Online /Get-Packages /Format:Table
DISM /Online /Remove-Package /PackageName:Package_for_KB3045999~31bf3856ad364e35~amd64~~6.1.1.1

Resources