Last active
February 26, 2025 05:15
-
-
Save poblabs/6b172d1698b42edcef5c to your computer and use it in GitHub Desktop.
Detect external IP and track it in a text file. If the IP is changed during a periodic script run, send an email that your external IP has changed.
This file contains hidden or 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
# | |
# PowerShell script that will check your external IP address and compare it to | |
# the one saved in c:\ip.txt. If a change is detected, use GMail to send you | |
# an email. | |
# | |
# Author: Pat O'Brien | |
# Created: July 16, 2015 | |
# | |
function sendMail($newIP) { | |
$gmailUsername = "Your GMAIL Username" | |
$gmailPassword = "Your GMAIL Password, yes in plaintext" | |
$password = ConvertTo-SecureString $gmailPassword -AsPlainText -Force | |
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $gmailUsername, $password | |
$From = "Who to send the email as" | |
$To = "Who to send the email to" | |
$Subject = "IP Address Change" | |
$Body = "Your IP Address has changed. Your IP is now $newIP" | |
$SMTPServer = "smtp.gmail.com" | |
$SMTPPort = "587" | |
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $credentials | |
} | |
# Find new IP | |
$newIP = (Invoke-WebRequest http://ipinfo.io/ip).content | |
$newIP = $newIP -replace "`t|`n|`r","" # Remove tabs, newlines and carriage returns from the content | |
# Load current IP from text file if it exists. If it doesn't exist, create it. | |
if (Test-Path "c:\ip.txt") { | |
$oldIP = Get-Content "c:\ip.txt" | |
} else { | |
New-Item "c:\ip.txt" -type file | |
"127.0.0.1" | Out-file c:\ip.txt | |
} | |
# Compare and send email if there is a change | |
if ($oldIP -ne $newIP) { | |
"Saved IP of $oldIP doesn't match the new IP we found of $newIP. Sending email that the IP has changed." | |
$newIP | Out-file c:\ip.txt | |
sendMail($newIP) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PowerShell script that will check your external IP address and compare it to
the one saved in c:\ip.txt. If a change is detected, use GMail to send you
an email.
Author: Pat O'Brien
Created: July 16, 2015
function sendMail($newIP) {
$gmailUsername = "Your GMAIL Username"
$gmailPassword = "Your GMAIL Password, yes in plaintext"
$password = ConvertTo-SecureString $gmailPassword -AsPlainText -Force
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $gmailUsername, $password
$From = "Who to send the email as"
$To = "Who to send the email to"
$Subject = "IP Address Change"
$Body = "Your IP Address has changed. Your IP is now $newIP"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $credentials
}
Find new IP
$newIP = (Invoke-WebRequest http://ipinfo.io/ip).content
$newIP = $newIP -replace "
t|
n|`r","" # Remove tabs, newlines and carriage returns from the contentLoad current IP from text file if it exists. If it doesn't exist, create it.
if (Test-Path "c:\ip.txt") {
$oldIP = Get-Content "c:\ip.txt"
} else {
New-Item "c:\ip.txt" -type file
"127.0.0.1" | Out-file c:\ip.txt
}
Compare and send email if there is a change
if ($oldIP -ne $newIP) {
"Saved IP of $oldIP doesn't match the new IP we found of $newIP. Sending email that the IP has changed."
$newIP | Out-file c:\ip.txt
sendMail($newIP)
}
Comment