MacOS script

Required installation :gwak; Brew Install gwak will do the job. Linux version: change gawk to AWk.

#! /bin/bash
ip='192.168.1.106'
current_day=$(date "+%Y%m%d")
log_dir="${ip}_${current_day}"
if [ ! -d "$log_dir" ]; then mkdir "$log_dir"; fi;
while true;
do
  current_time=$(date "+%Y%m%d_%H")
  ping -c 1 "$ip" | gawk '{print "["strftime("%F %H:%M:%S")"]:"$0}' | grep from >> ". /${log_dir}/${current_time}.log";
  sleep 1;   
done &

Copy the code

Windows script

@echo off
@REM set /p host=host:
set host=172.22.101.162

@REM get current time 
set "date_dir=%date:~,4%% % date: ~ 5, 2% % date: ~ 8, 2"
set "time_str=%time:~,2%"
echo %date_dir%%time_str%

set log_dir=%host%\%date_dir%
if not exist %log_dir% md %log_dir%

set logfile="%host%\%date_dir%\%time_str%.log"
echo "Start Time:"%date:~0,-3% %time:~0,-3%"     IP:"%host% >>%logfile%

:loop
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1') do (
set "date_dir=%date:~,4%% % date: ~ 5, 2% % date: ~ 8, 2"
set "time_str=%time:~,2%"
set log_dir=%host%\%date_dir%
if not exist %log_dir% md %log_dir%
set logfile="%host%\%date_dir%\%time_str%.log"

@REM log ping info
echo %date:~0,-3% %time:~0,-3% %%A>>%logfile%
echo %date:~0,-3% %time:~0,-3% %%A
timeout /t 1 /nobreak>nul
goto loop
)
pause>nul
Copy the code