一元网络论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 29|回复: 0

A small code to record IP history.

[复制链接]

2万

主题

2万

帖子

6万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
60921
发表于 7 天前 | 显示全部楼层 |阅读模式
[ol]
  • $logFile = "C:\path\to\your\ip_log.txt"
  • $checkInterval = 300 # 检查间隔,单位为秒(这里设置为5分钟)
  • function Get-ExternalIP
  •     try {
  •         $ip = Invoke-RestMethod -Uri 'http://ipinfo.io/ip'
  •         return $ip.Trim()
  •    } catch {
  •         return $null
  •     }
  • }
  • $lastIP = $null
  • while ($true)
  •     $currentIP = Get-ExternalIP
  •     $currentTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
  •    
  •     if ($currentIP -and ($currentIP -ne $lastIP))
  •         $logMessage = "&$currentTime - IP changed to: $currentIP"
  •         Add-Content -Path $logFile -Value $logMessage
  •         Write-Host $logMessage
  •         $lastIP = $currentIP
  •    
  •     start-sleep -seconds $checkInterval
  • }[/ol]复制代码
  • 回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|手机版|小黑屋|一元网络论坛

    GMT+8, 2024-9-29 07:28 , Processed in 0.108348 second(s), 19 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表