用bat脚本监测网络公网ip地址
时间: 2024-01-30 07:01:34 浏览: 164
@echo off
setlocal enabledelayedexpansion
set previous_ip=
set current_ip=
:loop
rem 获取当前公网IP地址
for /f "tokens=2 delims=," %%a in ('nslookup myip.opendns.com. resolver1.opendns.com ^| find "Address"') do (
set current_ip=%%a
)
rem 检查当前IP地址是否与之前相同
if "!previous_ip!" neq "!current_ip!" (
echo IP地址已更改:%current_ip%
set previous_ip=!current_ip!
)
rem 等待5分钟后再次检查
timeout /t 300 >nul
goto loop
exit /b
相关问题
windows bat脚本自动获取本地公网IP发送到指定邮箱
您可以使用Windows批处理脚本(.bat)来实现自动获取本地公网IP并发送到指定邮箱的功能。以下是一个示例的批处理脚本:
```bat
@echo off
REM 配置发送邮箱和接收邮箱
set "sender_email=your_sender_email@gmail.com"
set "sender_password=your_sender_password"
set "receiver_email=your_receiver_email@gmail.com"
REM 获取公网IP
powershell -Command "(Invoke-WebRequest -Uri 'https://api.ipify.org').Content.Trim()" > ip.txt
REM 读取IP地址
set /p ip=<ip.txt
REM 发送邮件
powershell -ExecutionPolicy Bypass -Command "Send-MailMessage -SmtpServer 'smtp.gmail.com' -Port 587 -UseSsl -Credential (New-Object System.Management.Automation.PSCredential ('%sender_email%', (ConvertTo-SecureString -String '%sender_password%' -AsPlainText -Force))) -From '%sender_email%' -To '%receiver_email%' -Subject 'Public IP Notification' -Body 'Your public IP is %ip%'"
REM 删除临时文件
del ip.txt
```
请将脚本中的 `your_sender_email@gmail.com`、`your_sender_password`、`your_receiver_email@gmail.com` 替换为您自己的邮箱信息。确保您的邮箱支持SMTP服务,并开启了SMTP授权。这个示例使用了Gmail的SMTP服务器,如果您使用其他邮箱,请相应地修改SMTP服务器信息。
将以上代码保存为一个批处理脚本(例如 `get_public_ip.bat`),然后在Windows上运行该脚本,您的公网IP将会被获取并发送到指定的邮箱。您可以使用Windows计划任务或其他自动化工具来定期运行该批处理脚本。
腾讯云ddns bat脚本
腾讯云ddns(动态域名解析)bat脚本是一种用于自动更新腾讯云域名解析的脚本。腾讯云提供了API接口,可以通过这个接口来更新域名解析的IP地址。而bat脚本则是一个简单的批处理脚本,常用于Windows系统。
在实现腾讯云ddns的bat脚本中,首先要获取当前主机的公网IP地址,可以使用一些网络服务返回当前公网IP的网址(例如:http://ipinfo.io/ip)。然后要调用腾讯云提供的API接口来更新域名解析的IP地址。这个接口需要提供腾讯云账号的密钥以及需要更新的域名和解析记录的ID。
脚本的实现步骤如下:
1. 创建一个新的bat脚本(例如:ddns.bat)。
2. 使用文本编辑器打开bat脚本。
3. 在脚本中使用curl或wget等工具,发送HTTP GET请求从网络服务获取当前公网IP地址,将其保存到一个变量中。
4. 使用腾讯云的API密钥、域名和解析记录ID,构造API请求的URL。
5. 使用curl或wget等工具,发送HTTP GET请求更新腾讯云域名解析的IP地址。
6. 将脚本保存并执行。
执行脚本后,它将自动获取当前主机的公网IP地址,并使用腾讯云提供的API接口更新指定的域名解析。
需要注意的是,脚本中的API密钥、域名和解析记录ID需要根据实际情况进行替换。此外,由于操作系统之间的差异,可能需要根据具体的系统环境和工具来进行适当的调整和修改。
阅读全文
相关推荐















