I wrote this script in case of need of receiving periodical email reports with live network devices.
This script has predefined list of IP addresses to be checked. It goes thru this list and saves result in the .txt file. By list of active devices it makes network latency measurements. When done, it makes .7z archive with results by date and sends it to needed email.
It needs mailsend.exe in the script folder to be able to send emails and open needed ports in network access politics.
@echo off
setlocal enabledelayedexpansion
chcp 65001 > nul
set "LogFolder=%date:/=-%"
set "LogFolder=%LogFolder: =%"
set "LogFolder=%LogFolder:/=_%"
If Not Exist "%LogFolder%" MD "%LogFolder%"
set FOLDER_PATH=C:\Work
set ZIP_PATH=C:\Program Files\7-Zip\7z.exe
set ARCHIVE_NAME_FORMAT=%DATE:~-4%_%DATE:~3,2%_%DATE:~0,2%__%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
set OLD_INFO_FOLDER=C:\Work\archived_info
set "ServerList=C:\Work\devices.txt"
set "ActiveList=C:\Work\active_devices.txt"
If Not Exist "%ServerList%" (
echo %ServerList% does not exist.
) else (
> "%ActiveList%" (
for /F UseBackQ %%A In ("%ServerList%") do (
ping -n 1 %%A | findstr "Reply from" > nul
if !errorlevel! == 0 (
for /f "tokens=3 delims= " %%B in ('ping -n 1 %%A ^| findstr /i "TTL"') do (
set "IP=%%B"
set "IP=!IP:~0,-1!"
echo !IP!
)
)
)
)
)
If Not Exist "%ActiveList%" (
echo %ActiveList% does not exist.
) else (
for /F UseBackQ %%A In ("%ActiveList%") do (
measure_latency.bat %LogFolder%
)
del "%ActiveList%"
for /d %%f in ("%FOLDER_PATH%\*.202") do (
"%ZIP_PATH%" a -t7z "%FOLDER_PATH%\archive_%ARCHIVE_NAME_FORMAT%.7z" "%%f" -mx9
move "%%f" "%OLD_INFO_FOLDER%"
)
mailsend.exe -to This email address is being protected from spambots. You need JavaScript enabled to view it. -from This email address is being protected from spambots. You need JavaScript enabled to view it. -starttls -port 587 -auth -smtp mail.com -user This email address is being protected from spambots. You need JavaScript enabled to view it. -pass yourpass -sub "Devices report" -M "Devices report goes in attachment" -attach "%FOLDER_PATH%\archive_%ARCHIVE_NAME_FORMAT%.7z"
del "%FOLDER_PATH%\archive_%ARCHIVE_NAME_FORMAT%.7z"
)
)
endlocal