Ijack :
I look forward to your batch file solution to this exercise with eager anticipation.
Ok, so here is a draft for a working script. I have made it somewhat longer than might be necessary, but I like using variables for most things and only define them at the top.
There is also some pseudo code in here, since we do not know the syntax of the telnet interface (if exist at all!)
This also apply to the fixing of the output from that command. (Done with FOR below.)
You were right about telnet.exe, when I did something similar some years ago I used netcat (nc.exe) for feeding a file directly to port tcp/23.
The script also uses the Windows command line mailer tool blat.exe. Both these files are freely available on the net.
Anyway, the script is totaly untested and should be seen mostly as a proof of concept.
Here is the script then:
@ECHO OFF
REM Change these parameters:
SET routerip=192.168.10.1
SET routerpw=secretpassword
SET smtp=mail.test.com
SET mail=example@test.com
REM Do not change these parameters
SET telnetfile=telnet.txt
SET telnetout=telnetout.txt
SET oldipfile=oldipfile.txt
SET tmp1=tempfile1.txt
SET ip=
SET mailfile=mailfile.txt
REM Checking for nc and blat
IF NOT EXIST nc.exe ECHO nc.exe is missing. && GOTO :eof
IF NOT EXIST blat.exe ECHO blat.exe is missing. && GOTO :eof
REM Create telnet answer file
ECHO %routerpw% > %telnetfile%
ECHO show ip interface 1 >> %telnetfile%
ECHO exit >> %telnetfile%
REM Creating old IP file if not existing
IF NOT EXIST %oldipfile% ECHO 0.0.0.0 > %oldipfile%
REM Use nc.exe to telnet the router and extract the ip address
nc.exe %routerip% 23 < %telnetfile% > %telnetout%
REM Extract the line with the actual IP address and put into "ip" variable
TYPE %telnetout% | FIND /i "IP Address:" > %tmp1%
FOR /f "tokens=3" %%a IN (%tmp1%) DO SET ip=%%a
ECHO The IP address is %ip%.
ECHO Checking last known IP..
ECHO.
REM Checking if changed
TYPE %oldipfile% | FIND /i "%ip%" > nul
IF "%ERRORLEVEL%"=="0" ECHO The same IP as before. && GOTO :cleanup
ECHO The IP has been changed. Old IP was:
TYPE %oldipfile%
REM Creating mail content
ECHO. > %mailfile%
ECHO Hello, >> %mailfile%
ECHO your outside IP address has been changed. >> %mailfile%
ECHO The new IP is %ip%. >> %mailfile%
ECHO. >> %mailfile%
ECHO Regards, your script. >> %mailfile%
REM Sending mail
blat.exe %mailfile% -to %mail% -f "Script" -i "Script" -s "IP has been changed!" -server %smtp%
REM Updating the oldipfile with the new IP
ECHO %ip% > %oldipfile%
:cleanup
REM Cleaning up
IF EXIST %tmp1% DEL %tmp1%
IF EXIST %telnetfile% DEL %telnetfile%
IF EXIST %telnetout% DEL %telnetout%
IF EXIST %mailfile% DEL %mailfile%