admin 发表于 2024-9-22 06:29:21

A small code to record IP history.

[*]$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[*]}复制代码
页: [1]
查看完整版本: A small code to record IP history.