How to detect IP change and send email alert (bash & powershell)

18 Mar 2022 | How to detect IP change and send email alert (bash & powershell) |

A common thing folks that run any kind of home server, need to do, is detect when they’re IP addressed normally a DHCP assigned address via they’re Internet server provider changes, by doing this is allows them continue to communicate with they’re server.

Below are a couple of script one a bash script and the other a power shell script that allow you to do this. All the script work pretty much the same. They use a curl or wget command or PowerShell equivalent to make a headless web request such as:

 wget -q -O - checkip.dyndns.com  

to a popular server on the internet , retrieve the value and then check it against a previously saved address , and if it has changed then update it and send out an alert.

Linux/Mac (Bash script)

Here’s the code for the bash equivalent. This script requires basic access to the bash shell, ability to use the wget command , and ability to add it to a cron.

#!/bin/bash

IPFILE="wan_ip.txt"
LAST_IP="-.-.-.-"

# Retrieve the current WAN IP
echo "Checking WAN IP..."
WAN_IP=$(wget -q -O - checkip.dyndns.com  | grep -Po "[\d\.]+")

echo "Current WAN IP  is: $WAN_IP"


if [[ -f "$IPFILE" ]]
then

LAST_IP=$(cat "$IPFILE")

echo "Previous WAN IP was: $LAST_IP"

if [ "$LAST_IP" != "$WAN_IP" ]; then
    echo "WAN IP Address has CHANGE to $WAN_IP since last checked."

#now lets send an email usingan SMTP Service
 
else
    echo "WAN IP Still the same as $WAN_IP."
fi

else
   echo "WAN IP first run  is : $WAN_IP"

fi

#Now lets write this to the file
echo "Updating WAN_IP file to: $WAN_IP"
cat <<EOM >  $IPFILE
$WAN_IP
EOM


# finally associate this with a crontab entry to run periodically.
# such as this example runs hourly to check for changes
# 0 * * * *   /<path_to/ip_monitor.sh

Windows (Powershell)

The Windows version is similar but it uses SMTP server instead of an email server to send out the alert.

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "[email protected]"
$Password = "password"
$ip = get-content c:\ip_last.txt

$to = "[email protected]"
$cc = "@vtext.com"
$subject = "New IP Address Detected"
$body = "New IP Address '$ip'"
$attachment = "C:\test.txt"

$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $body
$message.to.add($to)
$message.cc.add($cc)
$message.from = $username
$message.attachments.add($attachment)

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);

if ($ip -ne "X.X.X.X") {

$smtp.send($message)
write-host "Mail Sent"

}

Save the above script into a file such as ip_monitor.ps1, then using some crafty Powershell commands create a schedueld task so it can be run periodically, Do this with the following commands.

$Action = New-ScheduledTaskAction -Execute 'pwsh.exe' -Argument '-NonInteractive -NoLogo -NoProfile -File "C:\ip_monitor.ps1"'
$Trigger = New-ScheduledTaskTrigger -Once -At 3am
$Settings = New-ScheduledTaskSettingsSet
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings

Finally when all this is said and done register the above schedule task script with a command similar to:

Register-ScheduledTask -TaskName 'Monitor PowerShell Script' -InputObject $Task -User 'username' -Password 'windows Password'

Email notification

The easiest method is to use your email account , and configure it to authorize email for alternate sources usually in Gmail this requires allowing additional permissions and access to port 587. I do not recommend using standard sendmail port 25 as this is typically blocked by most residential ISPs.

Another method is to use one of the many email SMTP services such as :

Most of these services, offer free low volume accounts (typically less than 100 emails/day) , most of these services are geared for email marketers ,but you can use them just fine for this purpose.

Push/SMS Notifications

If you need more immediate types of alerts, you can use one of the many messaging systems avaiable online, here’s an example of a coupel of popular ones.

Leave a Reply