如何解决Powershell - 检查 IP,循环重试并发送电子邮件
我一直在尝试编写脚本,以便 powershell 检查我们的外部 ip - 如果它没有改变 - 什么都不做,只记录它被检查过。
如果脚本认为它已更改,请重试,如果尝试 5 次后它已更改,则发送电子邮件。
我已经包括了我到达的地方,但我今天无法(疲倦或愚蠢)让它发挥作用,所以把它扔给蜂巢寻求帮助!
附件是我也得到的地方!干杯CM
#find old ip
$oldip = gc .\ip.txt
#get current ip address
$currentip = (New-Object net.webclient).downloadstring("http://api.ipify.org")
#current date and time
$currentTime = Get-Date -format "dd-MMM-yyyy HH:mm:ss"
#script location
$scriptpath = $MyInvocation.MyCommand.DeFinition
[string]$dir = Split-Path $scriptpath
set-location $dir
#set the smtp server URL here
$smtpServer = "smtp.my.com"
#Set the Sender Name or Email here
$sender = "sender@my.comm"
#Enter the email reciepients here - you can send to multiple using format: "user1@gmail.com","user2@hotmail.com";
$users = "email@my.com";
#Enter the subject title the reciepient see's here
$subject = "IP Has Changed"
#Type out the email contents here
$body = "Dear Team Member,Your IP address at has just changed from: $oldip
And is Now set too: $currentip
Regards."
#*****************************************************************************************************************************************
do {
$Result = ($oldip -ne $currentip)
if ($Result.Status -eq "Success") {
Write-Output "IP Address Unchanged - Monitoring Continues."
Add-Content $PSScriptRoot\IP_Check_Log.txt "$currentTime - IP Unchanged. "
Start-Sleep -s 2
Exit 0
}
else {
Write-Output "Ping Failed!"
Add-Content $PSScriptRoot\IP_Check_Log.txt "$currentTime - IP Check Failed - retrying"
$Failures += 5
}
} until ($Failures -ge $FailureThreshold)
Write-Output "IP Address Changed!"
Add-Content $PSScriptRoot\IP_Check_Log.txt "$currentTime - IP AddeRSS Changed. "
Add-Content $PSScriptRoot\IP_Check_Log.txt "*****************************************************************************"
#be sure to alter the SMTP server port here if needed.
$smtp = New-Object Net.Mail.SmtpClient($smtpServer,587)
$smtp.EnableSsl = $true
#Enter the sending email address and password here
$smtp.Credentials = New-Object System.Net.NetworkCredential("email@my.com","password");
$smtp.Send($sender,$user,$subject,$body)
解决方法
在这里使用 do{..} until(..)
循环可能比使用 for
更容易,我会在此使用 try{..} catch{..}
。
在循环内部获取新的 ip 地址,不要获取一次并继续遍历该实例。
可能是这样的(省略了电子邮件内容)
$logFile = Join-Path -Path $PSScriptRoot -ChildPath 'IP_Check_Log.txt'
$FailureThreshold = 5
# find old ip
$oldip = [IPAddress](Get-Content .\ip.txt)
$ipchanged = $false
$currentip = $null
for ($i = 0; $i -lt $FailureThreshold; $i++) {
try {
# try to get the get current ip address
$currentip = [IPAddress](New-Object net.webclient).DownloadString("http://api.ipify.org")
if ($currentip -eq $oldip) {
Write-Output "IP Address Unchanged - Monitoring Continues."
Add-Content -Path $logFile -Value "$currentTime - IP Unchanged. "
exit 0
}
else {
$ipchanged = $true
}
}
catch {
$currentip = $null
$ipchanged = $false
Write-Output "Ping Failed!"
Add-Content -Path $logFile -Value "$currentTime - IP Check Failed - Retrying"
}
Start-Sleep -Seconds 2
}
# test if the above returned a $currentip not $null and if boolean $ipchanged is $true
if (($currentip) -and $ipchanged) {
Write-Output "IP Address Changed!"
Add-Content -Path $logFile -Value "$currentTime - IP Address Changed. "
Add-Content -Path $logFile -Value "*****************************************************************************"
# refresh the .\ip.txt file to store the new ip?
Set-Content -Path .\ip.txt -Value $currentip.IPAddressToString
# send the email
}
我对 [IPAddress]
使用强制转换,因为如果 .DownloadString()
失败或它返回一些不是 IP 地址的东西,您将进入 catch 块,以便您可以处理错误上>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。