Page 1 of 1

TNSPING Multiple hosts

Posted: 29 Aug 2018 13:20
by pedrohgl18
im trying to create a .bat to check multiple hosts, the code below doent work and idk why. please help me =)

@echo off
set fnm=c:\scripts\databases.txt
set lnm=c:\scripts\results.txt

if exist %fnm% goto Label1

echo.
echo Cannot find %fnm%
echo.
Pause
goto :eof

:Label1
echo Ping Test on %date% at %time% > %lnm%
echo ================================================= >> %lnm%
echo.
for /f %%i in (%fnm%) do call :Sub %%i
echo.
echo ================================================= >> %lnm%
echo Ping Test ENDED on %date% at %time% >> %lnm%
echo ... now exiting
goto :eof

:Sub
echo Testing %1
set state=OK
tnsping 1 %1
if errorlevel 1 set state=Down
echo %1 is %state% >> %lnm%

Re: TNSPING Multiple hosts

Posted: 30 Aug 2018 06:21
by penpen
It probably would help if you describe the error in more detail than just "the code below doent work".

penpen

Re: TNSPING Multiple hosts

Posted: 30 Aug 2018 06:27
by pedrohgl18
Keeps in looping at Testing 1.

Re: TNSPING Multiple hosts

Posted: 30 Aug 2018 15:22
by penpen
I guess the reason is that your "c:\scripts\databases.txt" contains:

Code: Select all

bilad and maybe some additional unknown text
1 some other text
1 text text
1 text
1 *
1
1 ...
Beside we don't know what the content of the databas file really is (if it matches the output without the "Testing " part, then the output is completely correct) the main issue might be unwanted breaking of words/lines by some delimiters (characters that breaks a command line in statement/program name and arguments).

You should notice that the default delimiters of a 'for/f' loop are space and tabulator.
You should also use usebackq, to enable delimiters in filenames (although in your case the filename doesn't contain any delimiter like space).
In addition you should also use doublequotes around a parameter (again to prevent against such poisonous characters == delimiters).
To avoid unwanted breaking of parameters (or filenames) you should use something like:

Code: Select all

for /f "usebackq tokens=* delims=" ("%fnm%") do  do call :Sub %%~i
(That is just an exemplary line; you should also update any other such line.)

Sidenote:
In the future i recommend you to give at least a complete example, including the contents of all related (sample) files (using dummy data), give the expected output and the actual output.
Above i actually guessed the most probable issues, so it hopefully should help you, but it might also be that it doesn't help you.


penpen