【问题】
我想ping一个域名发现ip变了就写到文本里,变的时间同时输出。
【代码】
Step1.bat- @echo off
- set "URL=www.baidu.com"
- for /f "tokens=2 delims=[]" %%i in ('ping -n 1 %URL% ^| findstr "\[.*\]"') do (
- set IP=%%i
- )
- echo,%IP%
- pause
复制代码 Step2.bat- @echo off
- set "URL=bbs.csdn.net"
- set "FileIP=IP.txt"
- set "FileLog=URL_IP.log"
- if not exist "%FileIP%" (
- >"%FileIP%" echo 0.0.0.0
- )
- if not exist "%FileLog%" (
- >"%FileLog%" type nul
- )
-
- :Monitor
- for /f "tokens=2 delims=[]" %%a in ('ping -n 1 %URL% ^| findstr "\[.*\]"') do (
- set IP_Current=%%a
- )
- set /p IP_Previous=<"%FileIP%"
- echo %IP_Previous% -- %IP_Current%
- if "%IP_Previous%" neq "%IP_Current%" (
- >"%FileIP%" echo %IP_Current%
- >>"%FileLog%" echo [%date% %time%] %IP_Current%
- )
- timeout /nobreak /t 3 >nul
- goto :Monitor
复制代码
|