Batch script - Multiple ip ping and output to mail

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
saturnino
Posts: 1
Joined: 28 Jul 2017 01:35

Batch script - Multiple ip ping and output to mail

#1 Post by saturnino » 28 Jul 2017 01:53

Hello everyone,
i explain my question.
I want to make a batch file to ping a multiple external IP (Around 20Ips) and then, when it end save a report in a txt file.
After all i want this batch send a mail with attachment (with mailsend)
Well i know mailsend commands but i'm not able to send a single mail after the process is end.
This is a code of my batch

Code: Select all

@echo off
set fnm=C:\Batch\IPList.txt
set lnm=C:\Batch\PingResults.txt

if exist %fnm% goto Label1

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

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

:Sub
echo Testing %1
set state=ONLINE
ping -n 1 %1
if errorlevel 1 set state=OFFLINE
echo %1 is %state% >> %lnm%
mailsend.exe **arguments**


So the code is correct, the problem is the mail send, sent me 20 mail and not only one with the end of work!

Thank you advance for your reply

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Batch script - Multiple ip ping and output to mail

#2 Post by elzooilogico » 28 Jul 2017 05:15

you're almost done

Code: Select all

@echo off
set "fnm=C:\Batch\IPList.txt"
set "lnm=C:\Batch\PingResults.txt"

if not exist %fnm% (
  echo.
  echo Cannot find %fnm%
  echo.
  Pause
  goto :eof


>"%lnm%" (
  echo PingTest STARTED on %date% at %time%
  echo =================================================
  echo.>CON
  for /f %%i in (%fnm%) do call :Sub %%i
  echo.>CON
  echo =================================================
  echo PingTest ENDED on %date% at %time%
  echo ... now exiting>CON
)

mailsend.exe **arguments** "%lnm%"
goto :eof

:Sub
echo Testing %1>CON
set state=ONLINE
ping -n 1 %1>CON
if %errorlevel% NEQ 0 set state=OFFLINE
echo %1 is %state%
exit/B

Post Reply