Is there a way I could check the status of a Pi-Hole through a batch script - batch-file

I'm trying to make a script that can help me remotely monitor my Pi-Hole through batch, and I want to add a feature that checks if the Pi-Hole is actually on, possibly by using ping, but I can't figure out how to.
Current Code:
ECHO OFF
ECHO CHECKING PI-HOLE STATUS
CLS
:MENU
ECHO.
ECHO ........................
ECHO SSH servers
ECHO ........................
ECHO.
ECHO 1 - Pi-Hole Console
ECHO 2 - Pi-Hole Network Interface
ECHO E - EXIT
ECHO.
SET /P M=Type 1 - 2 then press ENTER:
IF %M%==1 GOTO WEB1
IF %M%==2 GOTO WEB2
IF %M%==E GOTO EOF
IF %M%==e GOTO EOF
REM ------------------------------
REM SSH Server details
REM ------------------------------
:WEB1
CLS
call ssh pi#192.168.1.10
GOTO MENU
:WEB2
CLS
start opera pi-hole/admin
GOTO MENU
:EOF
taskkill /f /im cmd.exe

Related

Batch program closes with Enter even if no option is selected

I'm new to stackoverflow!
Straight to it:
PC / Windows 10 x64
Script level: Novice
I've written a basic batch program with selectable menu, however, if Enter is made with nothing from the menu, the batch program closes.
I'm looking for a simple script that will make Enter (with no selection) essentially do nothing, or return to the beginning of the batch program.
Thanks in advance.
Jon
:mainmenu
#echo off
cls
color 1f
echo.
echo =========================================================
echo = R U N A S A D M I N I S T R A T O R =
echo =========================================================
echo.
echo.
echo WINDOWS COMMAND UTILITY (MAIN MENU):
echo ------------------------------------
echo.
echo [1] - BOOT COMMANDS
echo.
echo [2] - WINDOWS HEALTH COMMANDS
echo.
echo [0] - Exit Menu (Closes this window)
echo.
echo.
SET /P M=Enter option [Number] then press ENTER:
IF %M%==1 GOTO bootcommands
IF %M%==2 GOTO winhealth
IF %M%==0 GOTO exit
GOTO exit
:bootcommands
#echo off
cls
color 4e
echo.
echo =========================================================
echo = R U N A S A D M I N I S T R A T O R =
echo =========================================================
echo.
echo.
echo BOOT COMMANDS:
echo --------------
echo.
echo [1] - Windows (Normal Startup)
echo.
echo [2] - Windows Safe Mode (Offline)
echo.
echo [3] - Windows Safe Mode with Networking
echo.
powershell write-host -fore yellow -back red Tampering with the UEFI/Bios may render the PC unbootable!
echo [8] - Boot into UEFI/Bios
echo.
echo [0] - Return to MAIN MENU
echo.
echo.
SET /P M=Enter option [Number] then press ENTER:
IF %M%==1 GOTO normal
IF %M%==2 GOTO safe
IF %M%==3 GOTO safenetwork
If %M%==8 GOTO bootbios
IF %M%==0 GOTO mainmenu
GOTO exit
:normal
bcdedit /deletevalue {default} safeboot
goto reboot
exit
:safe
bcdedit /set {default} safeboot minimal
goto reboot
exit
:safenetwork
bcdedit /set {default} safeboot network
goto reboot
exit
:bootbios
shutdown /r /fw /f /t 2
exit
:reboot
shutdown /f /t 2
:winhealth
#echo off
cls
color 1f
echo.
echo =========================================================
echo = R U N A S A D M I N I S T R A T O R =
echo =========================================================
echo.
echo.
echo WINDOWS HEALTH COMMANDS:
echo ------------------------
echo.
echo [1] - *Create a System Restore Point
echo.
echo [2] - Elevated Command Prompt (Administrator Privileges)
echo.
echo [3] - SFC (Offline) File System Checker
echo.
echo [4] - DISM CheckHealth (Offline) Deployment Image Servicing and Management
echo.
echo [5] - DISM ScanHealth (Offline) Deployment Image Servicing and Management
echo.
powershell write-host -fore red -back yellow Option 6 requires Internet Connection!
echo [6] - DISM RestoreHealth (Online) Deployment Image Servicing and Management
echo.
echo [7] - CHKDSK (Offline) Check Disk checks the file system and file system
echo metadata of a volume for logical and physical errors.
echo.
echo [0] - Return to MAIN MENU
echo.
echo.
SET /P M=Enter option [Number] then press ENTER:
IF %M%==1 GOTO createrestore
IF %M%==2 GOTO cmdadmin
IF %M%==2 GOTO sfc
IF %M%==3 GOTO dismhealth
IF %M%==4 GOTO dismscan
IF %M%==5 GOTO dismrestore
IF %M%==6 GOTO chkdsk
IF %M%==0 GOTO mainmenu
GOTO exit
:createrestore
cls
echo.
echo Creating a System Resore point. Please be patient...
echo.
echo.
wmic.exe /Namespace:\\root\default Path SystemRestore Call CreateRestorePoint "Windows Command Utility", 100, 7
echo.
exit
:cmdadmin
start cmd /k
exit
:sfc
cls
echo.
echo Starting System File Checker. This is a utility in Microsoft Windows
echo that scans for and restores corrupted Windows system files.
cmd /k sfc /scannow
exit
:dismhealth
cls
echo.
echo This is a quick scan and will determine if the image is repairable.
cmd /k DISM /Online /Cleanup-Image /CheckHealth
exit
:dismscan
cls
echo.
echo This is a quick scan and will determine if there are any corruptions detected.
cmd /k DISM /Online /Cleanup-Image /ScanHealth
exit
:dismrestore
cls
echo.
echo Your system will automatically connect to the Windows Update
echo service to download and replace the corrupt files.
cmd /k DISM /Online /Cleanup-Image /RestoreHealth
exit
:chkdsk
cmd /k chkdsk.exe /f /r /x
exit
:exit
exit
As per my comment, the fix is to use the correct command utility, choice.exe.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
:mainmenu
ClS
Color 1F
Echo=
Echo =========================================================
Echo = R U N A S A D M I N I S T R A T O R =
Echo =========================================================
Echo=
Echo=
Echo WINDOWS COMMAND UTILITY (MAIN MENU):
Echo ------------------------------------
Echo=
Echo [1] - BOOT COMMANDS
Echo=
Echo [2] - WINDOWS HEALTH COMMANDS
Echo=
Echo [0] - Exit Menu (Closes this program)
Echo=
Echo=
%SystemRoot%\System32\choice.exe /C 120 /N /M "Input option [Number]"
If ErrorLevel 3 Exit /B
If ErrorLevel 2 GoTo winhealth
:bootcommands
ClS
Color 4E
Echo=
Echo =========================================================
Echo = R U N A S A D M I N I S T R A T O R =
Echo =========================================================
Echo=
Echo=
Echo BOOT COMMANDS:
Echo --------------
Echo=
Echo [1] - Windows (Normal Startup)
Echo=
Echo [2] - Windows Safe Mode (Offline)
Echo=
Echo [3] - Windows Safe Mode with Networking
Echo=
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoP "Write-Host -Fore Yellow -Back Red 'Modifying the UEFI/BIOS may render the PC unbootable!'"
Echo [8] - Boot into UEFI/BIOS
Echo=
Echo [0] - Return to MAIN MENU
Echo=
Echo=
%SystemRoot%\System32\choice.exe /C 12380 /N /M "Input option [Number]"
If ErrorLevel 5 GoTo mainmemu
If ErrorLevel 4 GoTo bootbios
If ErrorLevel 3 GoTo safenetwork
If ErrorLevel 2 GoTo safe
:normal
%SystemRoot%\System32\bcdedit.exe /deletevalue {default} safeboot
goto reboot
:safe
%SystemRoot%\System32\bcdedit.exe /set {default} safeboot minimal
goto reboot
:safenetwork
%SystemRoot%\System32\bcdedit.exe /set {default} safeboot network
goto reboot
:bootbios
%SystemRoot%\System32\shutdown.exe /r /fw
Exit /B
:reboot
%SystemRoot%\System32\shutdown.exe /r
Exit /B
:winhealth
ClS
Color 1F
Echo=
Echo =========================================================
Echo = R U N A S A D M I N I S T R A T O R =
Echo =========================================================
Echo=
Echo=
Echo WINDOWS HEALTH COMMANDS:
Echo ------------------------
Echo=
Echo [1] - Create a System Restore Point
Echo=
Echo [2] - Elevated Command Prompt (Administrator Privileges)
Echo=
Echo [3] - SFC (Offline) File System Checker
Echo=
Echo [4] - DISM CheckHealth (Offline) Deployment Image Servicing and Management
Echo=
Echo [5] - DISM ScanHealth (Offline) Deployment Image Servicing and Management
Echo=
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoP "Write-Host -Fore Red -Back Yellow 'Option 6 requires an internet connection!'"
Echo [6] - DISM RestoreHealth (Online) Deployment Image Servicing and Management
Echo=
Echo [7] - CHKDSK (Offline) Check Disk checks the file system and file system
Echo metadata of a volume for logical and physical errors.
Echo=
Echo [0] - Return to MAIN MENU
Echo=
Echo=
%SystemRoot%\System32\choice.exe /C 12345670 /N /M "Input option [Number]"
If ErrorLevel 8 GoTo mainmenu
If ErrorLevel 7 GoTo chkdsk
If ErrorLevel 6 GoTo dismrestore
If ErrorLevel 5 GoTo dismscan
If ErrorLevel 4 GoTo dismhealth
If ErrorLevel 3 GoTo sfc
If ErrorLevel 2 GoTo cmdadmin
:createrestore
Rem Rest of your code, irrelevant to your question, goes here.

How do I make a batch file that opens certain programs?

cls
#ECHO OFF
title Heirloom SS Tool
:MENU
ECHO.
ECHO __________________________
ECHO| Select SS Option |
ECHO --------------------------
ECHO.
ECHO 1 -> Open Horion Folder
ECHO 2 -> Open Advanced Search Tool
ECHO 3 -> Open UserAssistView
ECHO 4 -> Open LastActivityView
ECHO 5 -> Open ProcessHacker
SET /P M=Type 1, 2, 3, 4 or 5 then press ENTER:
IF %M%==1 GOTO Horion
IF %M%==2 GOTO Search
IF %M%==3 GOTO UAV
IF %M%==4 GOTO LAV
IF %M%==5 GOTO PH
:Horion
explorer C:\Users\%USERNAME%\AppData\Local\Packages\Microsoft.MinecraftUWP_8wekyb3d8bbwe\RoamingState
GOTO MENU
:Search
cd Tools\Search
start everything.exe
GOTO MENU
:UAV
cd Tools\UAV
start UserAssistView.exe
GOTO MENU
:LAV
cd Tools\LAV
start LastActivityView.exe
GOTO MENU
:PH
cd Tools\ProccessHacker
start ProcessHacker.exe
GOTO MENU
EXIT
Basically im trying to make a batch file that opens these certain files that i put in the same folder as this batch.
When i start the batch file it just opens a CMD Prompt for a second and then closes!
Can anyone help me solve this?
You need to escape special characters redirect > and pipe | using caret ^ as already mentioned to you by #Neko in a comment.
I would however rather use choice instead of set /p
cls
#echo off
title Heirloom SS Tool
:menu
echo.
echo __________________________
echo ^| Select SS Option ^|
echo --------------------------
echo.
echo 1 -^> Open Horion Folder
echo 2 -^> Open Advanced Search Tool
echo 3 -^> Open UserAssistView
echo 4 -^> Open LastActivityView
echo 5 -^> Open ProcessHacker
choice /c 12345 /M "Select: "
goto choice%errorlevel%
:choice5
echo cd Tools\ProccessHacker
echo start ProcessHacker.exe
goto menu
:choice4
echo cd Tools\LAV
echo start LastActivityView.exe
goto menu
:choice3
echo cd Tools\UAV
echo start UserAssistView.exe
goto menu
:choice2
echo cd Tools\Search
echo start everything.exe
goto menu
:choice1
echo explorer "C:\Users\%USERNAME%\AppData\Local\Packages\Microsoft.MinecraftUWP_8wekyb3d8bbwe\RoamingState"
goto menu
:choice0
exit
You can read more about choice by running choice /? from cmd

How do I start Minecraft and Valorant from a batch file?

Last night, I decided to try coding a batch file. I used some references and some online walkthroughs and ended up making the following program.
echo off
Title Log into CMD
:home
color 0A
echo ==================
echo.
echo [1] Log In
echo [2] Exit
set /p login=
if %login%==1 goto 1
if %login%==2 goto 2
goto error
:1
echo.
echo ==================
echo.
set and=if
set /p name=Enter your name:
set /p pass=Enter your password:
if %name%==Bob %and% %pass%==***** echo Welcome, Omid
if not %name%==Bob goto no
if not %pass%==***** goto no
goto GameMenu
:no
echo You're not Bob!
pause
echo Goodbye!
pause
exit
:2
exit
:error
echo You have to select a valid option!
:GameMenu
Title Game Menu
echo.
echo ==================
echo.
echo [1] Minecraft
echo [2] krunker.io
echo [3] Valorant
set /p gc=Enter the number of the game you would like to play:
if %gc%==1 goto Minecraft
if %gc%==2 goto krunker
if%gc%==3 goto Valorant
goto error
:Minecraft
start MinecraftLauncher.exe
exit
:krunker
start https://www.krunker.io/
exit
:Valorant
start RiotClientServices.exe
exit
This program is a simple one that you log into with a username and password, which then leads to a directory of games to open. The krunker.io works, but I wasn't sure on how to call Minecraft and Valorant. The processes that open up the games are what I called. For example, MinecraftLauncher.exe is the name of the process that would launch Minecraft. When I choose Minecraft from the menu, it gives the error message 'Windows cannot find "MinecraftLauncher.exe" Make sure you typed the name correctly, and then try again.` This is strange given this is the literal name of the launcher. I was wondering if anyone with a bit more experience could help me out.
You have to go thru the path to initialize the application. Here's a step-by-step:
cd/
cd "Program Files (x86)/Minecraft"
start MinecraftLauncher.exe
You have to ensure that cmd is at c:\ using cd/, then go to launcher file location and starts it. You can do that by using start/d.
So, here's your code:
#echo off
Title Log into CMD
:home
color 0A
echo ==================
echo.
echo [1] Log In
echo [2] Exit
set /p login=
if %login%==1 goto 1
if %login%==2 goto 2
goto error
:1
echo.
echo ==================
echo.
set and=if
set /p name=Enter your name:
set /p pass=Enter your password:
if %name%==Bob %and% %pass%==***** echo Welcome, Omid
if not %name%==Bob goto no
if not %pass%==***** goto no
goto GameMenu
:no
echo You're not Bob!
pause
echo Goodbye!
pause
exit
:2
exit
:error
echo You have to select a valid option!
:GameMenu
Title Game Menu
echo.
echo ==================
echo.
echo [1] Minecraft
echo [2] krunker.io
echo [3] Valorant
set /p gc=Enter the number of the game you would like to play:
if %gc%==1 goto Minecraft
if %gc%==2 goto krunker
if%gc%==3 goto Valorant
goto error
:Minecraft
cd/
start/d "C:\Program Files (x86)\Minecraft" MinecraftLauncher.exe
exit
:krunker
start https://www.krunker.io/
exit
:Valorant
cd/
start/d "C:\Program Files (x86)\*VALORANT LAUNCHER FOLDER*" RiotClientServices.exe
exit
And your login system is broken. If you put any key other than 1 or 2, you go direct to :GameMenu, it can be easily solved by putting :error at the very end of the file or using a goto home before echo You have to select a valid option!
Hope this helps you.

trying to make an "OS" in batch

#echo off
:load
rem imitation of loading the os
color 70
ver
title boot
echo please wait...
ping localhost -n 3 >nul
cls
systeminfo
rem in here user types name he wants to be his account name
:login
title login
cls
date
cls
echo welcome to windows 71
echo before we bigen please type your name
set /P_name=here:
if %name%=admin goto admin
if not goto ms
rem ms=menu start
:ms
echo %time%
echo hello %name%
echo type HELP for list to commands
set /P_command=here:
if %command%=help goto help
if %command%=exit goto exit
if %command%=calendar goto cal
if not goto wc
rem wc=wrong command
:admin
echo hello %name% to the admin panel
echo type HELP for list to commands
set /P_command=here:
if %command%=help goto help
if %command%=exit goto exit
if %command%=calendar goto cal
So the problem is that it crashes after the :LOGIN part and I don't know what to do!
I'm trying to make an OS batch (something like MS-DOS), but it crashes after the "login" part.
I tried everything I could think of and it didn't work, also I want to make a save file so users can set a password for their "account".
As mentioned in above comments, you need to correctly use your variables, you can however use choice instead of set /p for your commands.
#echo off
:load
rem imitation of loading the os
color 70
ver
title boot
echo please wait...
timeout /t 3 >nul
cls
systeminfo
rem in here user types name he wants to be his account name
:login
title login
cls
date /t
timeout /t 2>nul
cls
echo welcome to windows 71
echo Before we begin please type your name
set /P "_name=here:"
if /i "_%name%"=="admin" goto admin
rem ms=menu start
:ms
echo %time%
echo hello %_name%
echo type HELP for list to commands
CHOICE /C HEC /M "Press H for Help, E to exit C for Calender."
goto opt%errorlevel%
:admin
echo hello %_name% to the admin panel
echo type HELP for list to commands
CHOICE /C HEC /M "Press H for Help, E to exit C for Calender."
goto opt%errorlevel%
:opt1
echo Help stuff goes here
goto :eof
:opt2
exit
:opt3
echo Calenders stuff goes here
Some things to note. You do not require to goto ms is the user is not admin as the statement for not being admin will not be met, we will automatically faal through into the ms label.
Notice where the problems were in your code. i.e if %name%=admin should be if "%_name%"=="admin" with double equal sign and the underscore in the name. It is also double quoted to ensure that we do a match without unwanted whitespace. Lastly /I option to catch ADMIN in any case.
See if /?, choice /? from command line for more help around these functions.
Okay this code is quite wrong.
I fixed it.
#echo off
:load
rem imitation of loading the os
color 70
title boot
echo please wait...
ping localhost -n 3 >nul
cls
rem in here user types name he wants to be his account name
:login
title login
cls
echo Welcome to Microsoft Windows 7!
echo Before we begin, please type your name.
set /p name=here:
if "%name%"=="admin" goto admin
if not "%name%"=="admin" goto ms
rem ms=menu start
:ms
echo %time%
echo Hello %name%
echo Type HELP for list to commands
set /p command=here:
if "%command%"=="help" goto help
if "%command%"=="exit" goto exit
if "%command%"=="calendar" goto cal
goto ms
rem wc=wrong command
:admin
CLS
echo hello %name% to the admin panel
echo type HELP for list to commands
set /P command=here:
if "%command%"=="help" goto help
if "%command%"=="exit" goto exit
if "%command%"=="calendar" goto cal
GOTO :ADMIN
Okay but you don't need to start systeminfo and all that.

Batch variables do not work

I have the following batch script:
#echo off
color 2f
title "USERS MANAGEMENT IN A LAN"
:top
echo ***************************************************************
echo.
echo SELECT ACTIONS
echo.
echo ***************************************************************
echo.
echo [1] Ping the service Params: unu
echo [2] TBD
echo.
echo [e] Exit
echo.
echo ***************************************************************
echo Enter the number of the website which you would like to go to:
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==1 (
echo Give the parameters:
set /p argi=
set /p argii=
START /B java -jar JavaClient.jar %argi% %argii%
)
if %udefine%==e goto exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
echo Type [e] to exit or [b] to go back and select another option.
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==b goto top
if %udefine%==e goto exit
:exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
pause
exit
The problem is that if I run it on the first time, the java jar file is throwing exceptions because it doesn't understands the variable arguments %argi% and %argii%. I press [b] to start again at the options menu, I do the same thing again and voila, it works! Why? Why it doesn't work at the first time, and if i press [b] to go back and put again the args it works fine every time?
I start the app:
If I kill the script with Ctrl-C and reopen it, it will have the same behaviour again...first time error, and then success.
Thank you!
you need delayed expansion :
#echo off
setlocal enableDelayedExpansion
color 2f
title "USERS MANAGEMENT IN A LAN"
:top
echo ***************************************************************
echo.
echo SELECT ACTIONS
echo.
echo ***************************************************************
echo.
echo [1] Ping the service Params: unu
echo [2] TBD
echo.
echo [e] Exit
echo.
echo ***************************************************************
echo Enter the number of the website which you would like to go to:
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==1 (
echo Give the parameters:
set /p argi=
set /p argii=
START /B java -jar JavaClient.jar !argi! !argii!
)
if %udefine%==e goto exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
echo Type [e] to exit or [b] to go back and select another option.
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==b goto top
if %udefine%==e goto exit
:exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
pause
exit

Resources