Automated checking of external IP

You host your own mailserver (and the provider even lets you), but the external IP address of your DSL/Cable connection is dynamic. So after a few days suddenly you realise there is no connection to the mailserver, and all the mail is bounced back to the senders. Sure makes for a quiet weekend, but not what you intended with this setup.

If you are somehow unable or unwilling to use Dynamic DNS setups, you can use the script below to periodically lookup the external IP address and send it via email, on the internal side of the mailserver ofcourse, to you if it has changed.

This batch script uses wget.exe and VMailer.exe, you can download both from the downloads section. Automate this script with the Windows Task Scheduler. Make sure to put this script on the actual mailserver so it can actually send the email to you internally. Options that need changing are marked in red.

@ECHO OFF
SET email=email@domain.com
SET from=email_from@domain.com
SET emailserver=localhost

SET url=http://automation.whatismyip.com/n09230945.asp
SET out=out.txt
SET oldipfile=oldipfile.txt

IF NOT EXIST wget.exe ECHO wget.exe is missing. && GOTO :eof
IF NOT EXIST VMailer.exe ECHO VMailer.exe is missing. && GOTO :eof
IF NOT EXIST %oldipfile% ECHO 0.0.0.0 > %oldipfile%

wget.exe -q -O %out% %url%
FOR /f “tokens=1” %%a IN (%out%) DO SET ip=%%a

ECHO.
ECHO The IP address is %ip%.
ECHO Checking last known IP…
ECHO.

TYPE %oldipfile% | FIND /i “%ip%” > nul
IF “%ERRORLEVEL%”==”0” ECHO The Ip address is unchanged. && GOTO :eof

ECHO The IP address has changed. The old IP was:
TYPE %oldipfile%
ECHO The new IP address is mailed to %email%

ECHO %ip% > %oldipfile%

echo To: %email% >mail.txt
echo From: %from% >>mail.txt
echo Subject: External IP changed! >>mail.txt
echo.>>mail.txt
echo The new IP address is: %ip% >>mail.txt

VMailer.exe mail.txt %emailserver% %email% %from%
del mail.txt

 

Leave a Reply

Your email address will not be published.