Skip navigation

JSI Tip 7940. How do I remotely manage my 'server' if its' IP address is NOT static?


If the IP address of your 'server' changes, because your ISP does not provide a static IP address, you need to be notified every time the IP address changes.

I have scripted IPChg.bat to detect the IP address change and to notify a list of users.

When you type IPConfig at a CMD prompt on your 'server', Windows displays all the connections and their associated IP address, like:

Windows IP Configuration

Ethernet adapter Internet:

   Connection-specific DNS Suffix  . :
   IP Address. . . . . . . . . . . . : 34.49.22.177
   Subnet Mask . . . . . . . . . . . : 255.255.255.240
   Default Gateway . . . . . . . . . : 34.49.22.159

Ethernet adapter JSIINC:

   Connection-specific DNS Suffix  . : 
   IP Address. . . . . . . . . . . . : 192.168.0.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

Ethernet adapter Wireless Network Connection:

   Connection-specific DNS Suffix  . : 
   IP Address. . . . . . . . . . . . : 192.168.1.101
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 

Ethernet adapter Local Area Connection 2:

   Connection-specific DNS Suffix  . :
   IP Address. . . . . . . . . . . . : 192.168.2.11
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 
IPChg.bat uses an IPChg.IPs file, which you store in the same folder as IPChg.bat, to record the previous Internet IP address, and to specify what local IP addresses to bypass. The IPChg.IPs file contains one ( 1) record, using the following format:
PreviousInternetIP \[LocalIP1 LocalIP2 ... LocalIPn\]

where:

PreviousInternetIP is the IP address currently assigned to your Internet connection. 
                   All four (4) octets must be specified, as in 34.49.22.177.

LocalIPi           is a local IP address. It must contain four (4) octets,
                   but octets 2, 3, and 4 may be an asterisk (*) to indicate a forced match.
                   You may specify as many local IP addresses as you require. For the above IPConfig,
                   output, any one of the following records will work, but if local IP addresses are
                   assigned by DHCP, use one of the last 3 lines:

                       34.49.22.177 192.168.0.1 192.168.1.101 192.168.2.11
                       34.49.22.177 192.168.0.1 192.168.1.* 192.168.2.*
                       34.49.22.177 192.168.0.* 192.168.1.* 192.168.2.*
                       34.49.22.177 192.168.*.*
                       34.49.22.177 192.*.*.*
When IPChg.bat detects a change to the Internet IP address, it uses a list of e-mail addresses that you provide, one ( 1) per line, in the IPChg.WHO file, which is also located in the same folder as IPChg.bat. IPChg.bat uses BLAT, the free SMTP mailer, to e-mail a message to each contact that you specify. If the Internet IP address gets changed to 34.49.22.160, and the IPChg.WHO file contained:
[email protected]
[email protected]
[email protected]
[email protected]

then one of the e-mail messages might look like:

To: [email protected]
Subject: IP Address on JSI001 changed from 34.49.22.177 to 34.49.22.160.
From: [email protected]
Date: Thu, 15 Apr 2004 03:13:51 -0400

IP Address on JSI001 changed from 34.49.22.177 to 34.49.22.160.
IPChg.bat then records the change in the IPChg.IPs file.

To run IPChg.bat, use the Scheduled Tasks application to schedule the batch to run At System Startup (When my computer starts), using an account whose password never expires, and that never logs on, so it will run in the background, such as [email protected].

IPChg.bat contains:

@echo off
setlocal
:: set folder path
set ipchg=%~f0
set ipchg=%ipchg:.bat=%
:: Sleep for 2 minutes
ping -n 121 127.0.0.1>nul
:again
:: Sleep for 3 minutes
ping -n 181 127.0.0.1>nul
for /f "Tokens=1*" %%a in ('type "%ipchg%.ips"') do ( 
 set pip=%%a
 set lcl=%%b
)
set ip=N
for /f "Tokens=2 Delims=:" %%i in ('ipconfig ^|findstr /L /C:"IP Address"') do (
 set ipw=%%i
 call :findip
)
if "%ip%" EQU "N" call :error
goto :again
:error
@echo Error: Cannot determine IP Address on %ComputerName%.>"%temp%\IPChg.txt"
for /f "Tokens=*" %%a in ('type "%ipchg%.who"') do (
 blat "%temp%\IPChg.txt" -to %%a -s "Error: Cannot determine IP Address on %ComputerName%."
)
goto :EOF
:findip
set ipw=%ipw: =%
call :findip1 %lcl%
goto :EOF
:findip1
if \{%1\}==\{\} goto findip3
for /f "Tokens=1-4 Delims=." %%o in ('@echo %1') do (
 set oct1=%%o
 set oct2=%%p
 set oct3=%%q
 set oct4=%%r
)
for /f "Tokens=1-4 Delims=." %%o in ('@echo %ipw%') do (
 set ioct1=%%o
 set ioct2=%%p
 set ioct3=%%q
 set ioct4=%%r
)
if "%oct1%" NEQ "%ioct1%" goto findip2
if "%oct2%" EQU "*" goto :EOF
if "%oct2%" NEQ "%ioct2%" goto findip2
if "%oct3%" EQU "*" goto :EOF
if "%oct3%" NEQ "%ioct3%" goto findip2
if "%oct4%" EQU "*" goto :EOF
if "%oct4%" EQU "%ioct4%" goto :EOF
:findip2
shift
goto findip1
:findip3
set ip=%ipw%
if "%ip%" NEQ "%pip%" call :tell
goto :EOF
:tell
@echo IP Address on %ComputerName% changed from %pip% to %ip%.>"%temp%\IPChg.txt"
for /f "Tokens=*" %%a in ('type %ipchg%.who') do (
 blat "%temp%\IPChg.txt" -to %%a -s "IP Address on %ComputerName% changed from %pip% to %ip%."
)
set pip=%ip%
@echo %pip% %lcl%>"%ipchg%.ips"
:: Sleep 15 seconds
ping -n 16 127.0.0.1>nul



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish