Created
March 8, 2022 01:40
-
-
Save t2psyto/8df5d1e199c5f5b925bfb0e4acb3c528 to your computer and use it in GitHub Desktop.
グローバルIPをチェックしてメール通知する。 # # globalIPに変化がなくてもメール通知する。 # globalIPに変化があった場合は追加CC先にもメールを送る。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## myipcheck.ps1 | |
# グローバルIPをチェックしてメール通知する。 | |
# | |
# globalIPに変化がなくてもメール通知する。 | |
# globalIPに変化があった場合は追加CC先にもメールを送る。 | |
# | |
# このスクリプトを myipcheck.bat から呼び出す場合:: | |
# > powershell -NoProfile -ExecutionPolicy Unrestricted "%~dpn0.ps1" | |
# メールタイトルの接頭辞 | |
$subject_prefix = "check global-IP " | |
## メール通知 From,To,Cc | |
$param_From="hogehoge@example.com" | |
$param_To="alert-target@example.com" | |
$param_Cc=@() | |
#$param_Cc = "hoge@example.com", "hoge2@example.com", | |
#IP変更を検知した際に追加でCcを送る先 | |
$param_Cc2 = "ip-changed1@example.com", "ip-changed2@example.com" | |
# 前回のチェック結果を保存しておくファイル | |
$DATADIR = $PSScriptRoot | |
$DATAFILE = "$DATADIR\myipcheck.txt" | |
Function sendmail { | |
Param( | |
$From, | |
$To, | |
$Cc, | |
$Subject, | |
$mailbody | |
) | |
# 送信メールサーバーの設定 | |
$SMTPServer="smtp.gmail.com" | |
$Port="587" | |
$User = "example_gmailuser@gmail.com" | |
$Password = "XXXXXXXXXXXXXX" | |
$SMTPClient=New-Object Net.Mail.SmtpClient($SMTPServer,$Port) | |
# SSL暗号化通信しない $false | |
#$SMTPClient.EnableSsl=$false | |
$SMTPClient.EnableSsl=$true | |
$SMTPClient.Credentials=New-Object System.Net.NetworkCredential($User,$Password) | |
# メールメッセージの作成 | |
$MailMassage=New-Object Net.Mail.MailMessage($From,$To,$Subject,$mailbody) | |
foreach ($ccaddr in $Cc) { $MailMassage.Cc.Add($ccaddr) } | |
## ファイルから添付ファイルを作成 | |
#$Attachment=New-Object Net.Mail.Attachment($targetzip) | |
## メールメッセージに添付 | |
#$MailMassage.Attachments.Add($Attachment) | |
# メール本文をコンソールに表示する。 | |
echo $MailMassage | select "From","To","CC","Subject","Body" | |
# メールメッセージを送信 | |
$SMTPClient.Send($MailMassage) | |
#$Attachment.Dispose() | |
$SMTPClient.Dispose() | |
} | |
$globalip_now = (curl https://ifconfig.me/ip).content | |
$datetime_now = Get-Date -Format "yyyyMMdd_HHmmss" | |
$now = "$datetime_now $globalip_now" | |
$data_now = $now.split() | |
if ((Test-Path -Path $DATAFILE) -eq $true) { | |
$last = (Get-Content $datafile) | |
if ($last.length -eq 0) {$last = "nodata nodata"} | |
} else { | |
$last = "nodata nodata" | |
} | |
$data_last = $last.split() | |
echo $now | Out-File $datafile | |
# ipチェックの結果で文言を切り替え+CC先を追加 | |
if ($data_now[1] -eq $data_last[1]) { | |
$summarytxt = "グローバルIP は前回と同じです。" | |
} else { | |
$summarytxt = "グローバルIP の変更を検知しました。" | |
$param_Cc += $param_Cc2 | |
} | |
# メールSubjectに ipチェックの値を付加 | |
$param_Subject = $subject_prefix + $now | |
# メールbody の構成 | |
$param_mailbody = @" | |
$subject_prefix` | |
` | |
$summarytxt` | |
` | |
前回: $last` | |
本日: $now` | |
"@ | |
# メール送信 | |
sendmail $param_From $param_To $param_Cc $param_Subject $param_mailbody |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment