kuhnikuehnast
Goto Top

Batch- Server nach Kontrolle (Ip und spez. Progr.) runterfahren

Ich habe eine Batch für einen Server geschrieben.
Es soll alle 10 min kontrolliert werden, ob Pcs der Domäne noch erreichbar sind. Ist dies nciht mehr der Fall, wird mit einer config.txt abgeglichen, ob noch ein oder mehrere zuvor definierte Programme laufen. Ist dies auch nicht mehr der Fall, fährt der Server das System herunter.

Hi!
Ich poste mal hier den Quellcode... Evtl. ist manches etwas umständlich und hätte auch einfacher gelöst werden können... Vielleicht werden aber auch noch Fehler gefunden, die mir bisher entgangen sind. Deshalb stell ich den Quellcode mal online. Würde mich über Kritik freuen (aber bitte nicht bez.: Das hätte auch so einfacher gehen können... Nur, wenn wirklcih Fehler drin sind! ;) )

lg Kuhni


edit: Code wurde nochmals überarbeitet... face-smile


@echo off


set ipmax=12
set ipmin=9
set countdown=2
set Wartezeit=600
set Programmliste=config.txt

set xcountdown=%countdown%


REM ###################Umlaute definieren###################
chcp 1252>nul
set ue=ü
set ae=ä
set oe=ö
set Uue=Ü
set Aae=Ä
set Ooe=Ö
set ss=ß
chcp 850>nul
REM ##########################################################






REM ###################Wartezeit berechnen###################

set /a min=%wartezeit%/60
set /a y=%min%*60
set /a sek=%wartezeit%-%y%
set waittime=Wartezeit: %min% Minute(n), %sek% Sekunde(n)
REM ##########################################################

if not exist "%CD%/log/" mkdir "%CD%/log/"  

	call :ausgabe Serverstart
        call :ausgabe %waittime%
	echo. & echo. >>"%CD%/log/[%date%]- Logfile Server.txt"  

ping localhost -n %Wartezeit% > nul

	
REM ###################Mainloop###################
:mainloop
	set ip=%ipmin%
	if %xcountdown%==0 goto programm

	call :ausgabe Ping Start- Ip-Bereich: 192.168.0.%ip% - 192.168.0.%ipmax% 



REM ###################Ping ausfhren###################

:pingloop
	if %ip% EQU %ipmax% goto endeschlecht
	set /a ip=%ip%+1
	ping 192.168.0.%ip% -n 1 -w 10 %1|find "TTL=" > nul  
	if %errorlevel%==1 goto not-available
	goto :endegut



:not-available
	goto pingloop




:endegut
	

		set xcountdown=%countdown%

	call :ausgabe Ping Ergebnis Positiv- Erste Ip: 192.168.0.%ip% 
	call :ausgabe %waittime%

		echo. & echo. >>"%CD%/log/[%date%]- Logfile Server.txt"  

		ping localhost -n %Wartezeit% > nul


	goto mainloop



:endeschlecht
	

		set /a xcountdown=%xcountdown%-1

	call :ausgabe Ping  Negativ- Noch %xcountdown% Ping(s) bis zum herunterfahren
 	call :ausgabe %waittime%
	
		echo. & echo. >>"%CD%/log/[%date%]- Logfile Server.txt"  

		ping localhost -n %Wartezeit% > nul


	goto mainloop	
REM ##########################################################

REM ##########################################################






REM ###################Runterfahren###################

:runterfahren
	echo. & echo. >>"%CD%/log/[%date%]- Logfile Server.txt"  

		call :ausgabe Initiale Shutdownkontrolle gestartet:

			:pingloop2
			if %ip% EQU %ipmax% goto endeschlecht2
			set /a ip=%ip%+1
					ping 192.168.0.%ip% -n 1 -w 10 %1|find "TTL=" > nul  
			if %errorlevel%==1 goto not-available2
			goto :endegut

			:not-available2
			goto pingloop2



			:endeschlecht2
			call :ausgabe Kein Pc erreichbar: 192.168.0.%ip% - 192.168.0.%ipmax%
			
				set prog=0

					for /f %%a in (%Programmliste%) do Tasklist |find /i "%%a" && set /a prog+=1 1>NUL 2>NUL   

				if %prog%==0 (
					goto :programm-aus2 
				) else (
					goto :programm-an
				)

			:programm-aus2
                        call :ausgabe Es l%ae%uft kein wichtiges Programm mehr.
			call :ausgabe Shutdown initiiert.
			echo. & echo. >>"%CD%/log/[%date%]- Logfile Server.txt"  

	shutdown.exe /f /s
exit
REM ##########################################################





REM ###################Schleife läuft noch ein Programm?###################

:programm
	
	
	set prog=0

		for /f %%a in (%Programmliste%) do Tasklist |find /i "%%a" && set /a prog+=1 1>NUL 2>NUL   

	
	if %prog%==0 (
		goto :programm-aus 
	)else (
		goto :programm-an
	)

:programm-an
        call :ausgabe Programmtest:
        call :ausgabe Es l%ae%uft/laufen noch %prog% Programm(e).
	call :ausgabe %waittime%

		echo. & echo. >>"%CD%/log/[%date%]- Logfile Server.txt"  

		ping localhost -n %Wartezeit% > nul
	goto :programm

:programm-aus
        call :ausgabe Programmtest (1. Versuch):
        call :ausgabe Es l%ae%uft kein wichtiges Programm mehr. 
	call :ausgabe %waittime%

		echo. & echo. >>"%CD%/log/[%date%]- Logfile Server.txt"  

		ping localhost -n %Wartezeit% > Nul

	set prog=0

		for /f %%a in (%Programmliste%) do Tasklist |find /i "%%a" && set /a prog+=1 1>NUL 2>NUL  


	
	if %prog%==0 (
		goto :test
	) else (
		goto :programm-an
	)

:test
	call :ausgabe Programmtest (2. Versuch):
        call :ausgabe Es l%ae%uft kein wichtiges Programm mehr. 
	call :ausgabe %waittime% 
	ping localhost -n %Wartezeit% > nul
	goto :runterfahren	
REM ##########################################################









REM ###################Schleife für Ausgabe und Log###################

:ausgabe

	set Zeit=%Time%
chcp 1252>nul
	echo [%date% - %Zeit%] %* >> "%CD%/log/[%date%]- Logfile Server.txt"  
chcp 850>nul
	echo [%date% - %Zeit%] %*

goto :eof
REM ##########################################################

Content-Key: 133613

Url: https://administrator.de/contentid/133613

Printed on: April 19, 2024 at 18:04 o'clock