batch to check if terminal can ping servers or not - windows

I wrote a batch but it doesnt work at all:
set THEFILE=ip_list.txt
if exist result.txt del result.txt
::read %THEFILE% and loop through each line
for /F %%A in (%THEFILE%) do (
SETLOCAL ENABLEDELAYEDEXPANSION
SET counter=1
FOR /F "tokens=* USEBACKQ" %%F IN (`ping %%A`) DO (
SET result%counter%=%%F
SET counter=%counter%+1
)
ECHO %result1%
ECHO %result2%
ECHO %result3%
echo %result9% >> result.txt
ENDLOCAL
echo ******************************************* >> result.txt
)
inside of ip_list.txt :
8.8.8.8
4.4.4.4
I need to get ip adresses if not %100 loss occurs..
I need a quick help in my code..

Here is how I'd do it:
#echo off
setlocal
set THEFILE=ip_list.txt
if exist result.txt del result.txt
for /F %%A in (%THEFILE%) do (
call :IsPingable %%A && (
Echo %%A is pingable ) || (Echo %%A is not pingable)
)
exit /b
:IsPingable <comp>
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b

Related

Errorlevel always returns 0 in a for loop with ping command

I am trying to create a batch script that will ping a machine by hostname, save ip and domain to a variable and display the state (ON or OFF).
How can i fix this code so it will display the status correctly?
My code always returns ON even if pc is actually OFF.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set hostname=non-existent-hostname
set domain=UNRESOLVED
set ip=UNRESOLVED
for /f "tokens=2,3 delims= " %%b in ('ping -a -4 -n 1 !hostname! ^| find "Pinging"') do (
set domain=%%b
set ip=%%c
)
if errorlevel 1 (
echo !hostname! !ip! [!domain!] is OFF
) else (
echo !hostname! !ip! [!domain!] is ON
)
pause
I have another approach using this kind of ping, because my machine is French.
For example, this batch script pings a list of URLs and displays their status (ON/OFF) with different colors.
The URLs are defined in the "URLS" variable.
The script loops through the URLs, formats them using a StringFormat function, gets the IP address of each URL using the "ping" command, and then displays the status of each URL (ON or OFF) with a color using a PSColor function.
#echo off
Title Multi-Ping hosts with colors
Set URLS="non-existent-hostname","https://www.google.com","Nothingtotest","https://www.yahoo.com","www.reddit.com",^
"http://www.wikipedia.com","www.stackoverflow.com","www.bing.com","NoBodyHere.com"
setlocal ENABLEDELAYEDEXPANSION
for %%a in (%URLS%) do (
Call :StringFormat "%%~a"
set "ip="
for /f "tokens=2 delims=[]" %%b in ('ping -n 1 !URL!') do set "ip=%%b"
ping -n 1 !URL!>nul && set "msg=!URL! - !ip! ON" && Call :PSColor "!msg!" Green \n || set "msg=!URL! - !ip! OFF" && Call :PSColor "!msg!" Red \n
)
pause & Exit
::---------------------------------------------------------------------------------
:StringFormat <URL>
(
echo Function StringReplace(Str^)
echo Str = Replace(Str,"http://",""^)
echo Str = Replace(Str,"https://",""^)
echo StringReplace = str
echo End Function
echo wscript.echo StringReplace("%~1"^)
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%a in ('Cscript /nologo "%tmp%\%~n0.vbs"') do ( set "URL=%%~a" )
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
exit /b
::---------------------------------------------------------------------------------
:PSColor <String> <Color> <NewLine>
If /I [%3] EQU [\n] (
Powershell Write-Host "`0%~1" -ForegroundColor %2
) Else (
Powershell Write-Host "`0%~1" -ForegroundColor %2 -NoNewLine
)
Exit /B
::---------------------------------------------------------------------------------
I figured it out, thanks everyone for hints.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set hostname=non-existent-hostname
set domain=UNRESOLVED
set ip=UNRESOLVED
for /f "tokens=2,3 delims= " %%b in ('ping -a -4 -n 1 !hostname! ^| find "Pinging"') do (
set domain=%%b
set ip=%%c
)
ping -n 1 %hostname% >nul
if %errorlevel% == 0 (
echo %hostname% %ip% [%domain%] is ON
) else (
echo %hostname% %ip% [%domain%] is OFF
)
pause

Batch script help, script exiting after parentheses

I wrote a very simple script to output the host machine's MAC addresses to a text file.
The script is exiting right after line 3 - 'IF DEFINED WRITEOK ('.
#echo off
cls
copy /Y NUL "%CD%\.writable" > NUL 2>&1 && set WRITEOK=1
IF DEFINED WRITEOK (
rem ---- we have write access ----
set DIR=%CD%\interfaces
set FILE=%DIR%\%USERNAME%.txt
IF NOT EXIST "%DIR%" (
MKDIR "%DIR%"
echo DIR '%DIR%' was created
) else (
echo DIR '%DIR%' already exists
) for /f "tokens=2 delims=:" %%i in ('ipconfig /all ^| findstr /i "Physical Host"') do (
echo %%i >> "%FILE%"
echo OUTPUT written to '%FILE%'
)
) else (
rem ---- we don't ----
echo DIR '%DIR%' is not writable
)
echo.
echo DONE!
pause
Try to put the FOR one line after the closing parenthesis :
...)
for /f "tokens=2 delims=:" %%i in ('ipconfig /all ^| findstr /i "Physical Host"') do (...
you can't start a FOR with a closing parenthesis in front :
This will not work :
(echo 1
) for /l %%a in (1,1,10) do echo %%a
and this will work :
(echo 1
)
for /l %%a in (1,1,10) do echo %%a
EDIT 1 :
For the path variables containing space use double quote :
"%cd%"
when using it.

Windows batch For Loop dropping empty strings to the end

I have this following code.
#echo off
setlocal EnableDelayedExpansion
set holdingline=,Measure,,+ X,,,0,0
FOR /F "tokens=1,2,3,4,5,6,7,8 delims=," %%a IN ("%holdingline%") DO (
echo %%a
echo %%b
echo %%c
echo %%d
echo %%e
echo %%f
echo %%g
echo %%h
echo %holdingline%
)
pause
Output displayed is as below:
Measure
+ X
0
0
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
,Measure,,+ X,,,0,0
The empty strings are pushed to the end and I wonder why. I am expecting them in order, say something like:
ECHO is off.
Measure
ECHO is off.
+ X
ECHO is off.
ECHO is off.
0
0
,Measure,,+ X,,,0,0
This would enable me to assign them to the correct variables. I tried searching but did not find much help.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set holdingline=,Measure,,+ X,,,0,0
FOR /F "tokens=1-8 delims=," %%a IN (""%holdingline:,=","%"") DO (
ECHO(%%~a
ECHO(%%~b
ECHO(%%~c
ECHO(%%~d
ECHO(%%~e
ECHO(%%~f
ECHO(%%~g
ECHO(%%~h
echo %holdingline%
)
GOTO :EOF
This should solve your problem
To process a file, producing a new file
#ECHO OFF
SETLOCAL
(
FOR /f "delims=" %%x IN (q29639243.txt) DO (
set "holdingline=%%x"
CALL :process
)
)>u:\new.txt
GOTO :EOF
:process
FOR /F "tokens=1-8 delims=," %%a IN (""%holdingline:,=","%"") DO (
ECHO(%%~a
ECHO(%%~b
ECHO(%%~c
ECHO(%%~d
ECHO(%%~e
ECHO(%%~f
ECHO(%%~g
ECHO(%%~h
echo %holdingline%
)
GOTO :EOF
I used a file named q29639243.txt containing similar data for my testing.
Produces u:\new.txt
This should do what you want it to:
#echo off
setlocal EnableDelayedExpansion
set holdingline=,Measure,,+ X,,,0,0
FOR /F "tokens=1,2,3,4,5,6,7,8 delims=," %%a IN ("%holdingline:,,=, ,%") DO (
echo %%a
echo %%b
echo %%c
echo %%d
echo %%e
echo %%f
echo %%g
echo %%h
echo %holdingline%
)
pause
Replacing ,, with , , will cause it to set the middle variables to (space) and hence treat them like nothing. If you don't have anything between two , it will skip them and hence only 4 variables exist, leaving e-h blank

Batch - Printing Echo is OFF

I have the following code:
#ECHO OFF
FOR /F "tokens=*" %%A IN (%ThisService_WorkingDirectory%MPMissions\%ThisService_mapname%\init.sqf) DO (
ECHO %%A
IF "%%A" EQU "DZE_BuildOnRoads = false; // Default: False" (
TYPE D:\god-bases\god-bases.txt
)
) >> temp.txt
move /y temp.txt %ThisService_WorkingDirectory%MPMissions\%ThisService_mapname%\init.sqf
How do I stop this from printing "Echo is OFF" in blank space?
Change echo %%A to echo(%%A.
This will avoid problems with nearly all possible combinations of %%A like
<empty>, ON or /?

why does my batch script says "missing operator"?

I'm using this script:
#echo on
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (C:\Users\Diana\Desktop\names.txt) do (
SET /A vidx=!vidx! + 1
snmpwalk -v 2c -c root %%A .1.3.6.1.2.1.25.3.3.1.2 > C:\Users\Diana\Desktop\cpu.txt
snmpwalk -v 2c -c root %%A .1.3.6.1.2.1.25.5.1.1.2 > C:\Users\Diana\Desktop\ramvid.txt
snmpwalk -v 2c -c root %%A .1.3.6.1.2.1.25.2.2 > C:\Users\Diana\Desktop\ram.txt
)
FOR /F "tokens=4" %%B IN (C:\Users\Diana\Desktop\ram.txt) DO echo %%B >> C:\Users\Diana\Desktop\ramfiltruotas.txt
FOR /F "tokens=4" %%B IN (C:\Users\Diana\Desktop\cpu.txt) DO echo %%B >> C:\Users\Diana\Desktop\cpufiltruotas.txt
FOR /F "tokens=4" %%B IN (C:\Users\Diana\Desktop\ramvid.txt) DO echo %%B >> C:\Users\Diana\Desktop\ramvidfiltruotas.txt
for /F %%a in (C:\Users\Diana\Desktop\ramvidfiltruotas.txt) do set /a c+=%%a echo %c% >> C:\Users\Diana\Desktop\naujas.txt
I am trying to calculate the sum of numbers that are in file ramvidfiltruotas.txt, but when I start the script it shows me a lot of this:
C:\Users\Diana>set /a c+=3472 echo 1>>C:\Users\Diana\Desktop\naujas.txt
Missing operator.
C:\Users\Diana>set /a c+=2616 echo 1>>C:\Users\Diana\Desktop\naujas.txt
Missing operator.
Maybe someone could help me to find the problem here?
By the way, this script works on other computer.
You have two commands running together. Either separate the set and echo on different lines, or concatenate them with an ampersand &:
set /a c+=2616 & echo 1>>C:\Users\Diana\Desktop\naujas.txt

Resources