You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

公网IP变更时自动触发URL访问的批处理实现可行性问询

Is This Feasible?

Absolutely! This approach is totally feasible and a way more efficient alternative to your current scheduled task setup—you’ll only trigger the URL when your IP actually changes, avoiding unnecessary runs that waste resources.

Here’s how to modify your existing batch script to implement this change-detection logic:

Modified Batch Script

@Echo off
setlocal enabledelayedexpansion

:: Define where to store your last known IP (adjust this path to your preference)
set "IP_STORE=C:\last_known_ip.txt"

:: Get your current external IP using your existing method
for /f "tokens=1* delims=: " %%A in (
    'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
) Do set "ExtIP=%%B"

:: Clean up any accidental whitespace in the retrieved IP
set "ExtIP=!ExtIP: =!"

:: Handle first run (no saved IP file exists)
if not exist "%IP_STORE%" (
    echo !ExtIP! > "%IP_STORE%"
    echo External IP detected: !ExtIP! (first run, launching URL)
    start http://your-target-url.com?ip=!ExtIP!
    goto :finish
)

:: Read the last saved IP from the file
set "SavedIP="
for /f "delims=" %%C in (%IP_STORE%) do set "SavedIP=%%C"

:: Compare current IP with saved IP
if not "!ExtIP!"=="!SavedIP!" (
    :: IP changed—update the store and launch the URL
    echo !ExtIP! > "%IP_STORE%"
    echo IP changed from !SavedIP! to !ExtIP! — triggering URL
    start http://your-target-url.com?ip=!ExtIP!
) else (
    :: IP is unchanged—do nothing
    echo External IP remains !ExtIP! — no action needed
)

:finish
endlocal

Key Details Explained

  • setlocal enabledelayedexpansion: Fixes variable access issues when working with values set inside loops or conditionals (uses ! instead of % for updated variables).
  • IP Storage File: We use C:\last_known_ip.txt to track your previous IP—feel free to move this to a folder like your Documents directory if preferred.
  • Whitespace Trimming: The line set "ExtIP=!ExtIP: =!" removes any accidental spaces from the retrieved IP, preventing false "change" detections.
  • Change Logic: The script only updates the file and launches the URL when the current IP differs from the saved one.

How to Use

  1. Replace http://your-target-url.com?ip=!ExtIP! with your actual target URL (keep !ExtIP! where you want the IP inserted).
  2. Save the script as IP_Monitor.bat.
  3. Set up a scheduled task to run this script at a reasonable interval (e.g., every 5-10 minutes)—this is still needed, but now it only acts when your IP changes, making it far more efficient than your original setup.

Quick Notes

  • The nslookup method is reliable, but if you run into issues, you could use curl ifconfig.me or curl ipinfo.io/ip instead (curl is included by default in Windows 10/11).
  • Ensure the user running the scheduled task has write permissions to the folder where last_known_ip.txt is stored.

内容的提问来源于stack exchange,提问作者Animesh Agarwal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 04:47:30