Bat File IP Monitoring

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
central10
Posts: 11
Joined: 12 May 2010 10:30

Bat File IP Monitoring

#1 Post by central10 » 22 Apr 2011 16:21

Hello,

I am trying to create a bat file that will monitor my ip address and subsequently run a program every time my ip address changes.

I'm using a VPN that's changing my ip every so often and I want a program or command to run every time it changes.

If anyone can give me a code that would work that would be greatly apreciated!!

Thank you
David

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Bat File IP Monitoring

#2 Post by nitt » 22 Apr 2011 20:28

central10 wrote:Hello,

I am trying to create a bat file that will monitor my ip address and subsequently run a program every time my ip address changes.

I'm using a VPN that's changing my ip every so often and I want a program or command to run every time it changes.

If anyone can give me a code that would work that would be greatly apreciated!!

Thank you
David


1. I'm not sure if Batch can do this.
2. ...I thought you couldn't change your workstation IP.
3. You probably should use VBScript

It would be best to use VBScript for this. These files run without any software just like batch, but they are .VBS files.

Code: Select all

start
sub start
Set objWMIService = GetObject("winmgmts:")
Set IPConfigSet = objWMIService.ExecQuery     ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
For Each IPConfig in IPConfigSet
urip = IPConfig.IPAddress(0)
Next
start2
end sub

sub start2()
Set objWMIService = GetObject("winmgmts:")
Set IPConfigSet = objWMIService.ExecQuery     ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
For Each IPConfig in IPConfigSet
lestip = IPConfig.IPAddress(0)
if (lestip= urip) then
msgbox("Your IP Has Changed!")
start
end if
Next
start2
end sub


I'm sorry but I do not have a clue how to do this in Batch.

This should loop forever, and every time your IP changes it gives you the popup message "Your IP Has Changed!".

central10
Posts: 11
Joined: 12 May 2010 10:30

Re: Bat File IP Monitoring

#3 Post by central10 » 23 Apr 2011 10:44

Hello,

I tried your script how-ever it doesn't seem to work. I get the following error message when i try to run it.

http://img132.imageshack.us/img132/2503/45019659.jpg

Also, is there a query we can add to the script that will run a program when the ip changes?

For clarification - I am using a VPN to change my ip every 20 minutes or so - so my external ip changes.

Thank you!!

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Bat File IP Monitoring

#4 Post by nitt » 23 Apr 2011 17:10

central10 wrote:Hello,

I tried your script how-ever it doesn't seem to work. I get the following error message when i try to run it.

http://img132.imageshack.us/img132/2503/45019659.jpg

Also, is there a query we can add to the script that will run a program when the ip changes?

For clarification - I am using a VPN to change my ip every 20 minutes or so - so my external ip changes.

Thank you!!


I'm so stupid! >.>
I keep on forgetting that you can't loop subs like that.

Here we go, this should do the trick:

Code: Select all

start
set wshell = createobject("wscript.shell")
sub start
Set objWMIService = GetObject("winmgmts:")
Set IPConfigSet = objWMIService.ExecQuery     ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
For Each IPConfig in IPConfigSet
urip = IPConfig.IPAddress(0)
Next
start2
end sub

sub start2()
do
Set objWMIService = GetObject("winmgmts:")
Set IPConfigSet = objWMIService.ExecQuery     ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
For Each IPConfig in IPConfigSet
lestip = IPConfig.IPAddress(0)
if (lestip= urip) then
wshell.run("C:\Windows\system32\notepad.exe")
start
end if
Next
loop
end sub


Change "c:\windows\system32\notepad.exe" to whatever you want it to open.

I can't test this, though, because I have to clue how to change my IP.

phillid
Posts: 109
Joined: 03 Apr 2010 20:27
Location: Wellington, New Zealand
Contact:

Re: Bat File IP Monitoring

#5 Post by phillid » 23 Apr 2011 19:58

This should work and is pure batch, and much smaller:

Code: Select all

@echo off
echo %date% %time% -- Program Started
:loop
for /f "delims=" %%a in ('ipconfig^|findstr /i /c:"ip address"') do set "ip=%%a"
set ip=%ip:~44,20%
if not "%ip%"=="%oldip%" (
:: IP WAS CHANGED
echo %date% %time% -- IP changed
)
set oldip=%ip%
goto loop

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Bat File IP Monitoring

#6 Post by nitt » 24 Apr 2011 12:21

phillid wrote:This should work and is pure batch, and much smaller:

Code: Select all

@echo off
echo %date% %time% -- Program Started
:loop
for /f "delims=" %%a in ('ipconfig^|findstr /i /c:"ip address"') do set "ip=%%a"
set ip=%ip:~44,20%
if not "%ip%"=="%oldip%" (
:: IP WAS CHANGED
echo %date% %time% -- IP changed
)
set oldip=%ip%
goto loop


I tried this but it keeps on saying my IP has changed, when it has not.
Are you sure there's not an error in here? I've never used some of this stuff so I'm not quite sure.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Bat File IP Monitoring

#7 Post by aGerman » 24 Apr 2011 12:41

IPCONFIG returns more than one IP adress. You have to define which of them you need.

Regards
aGerman

phillid
Posts: 109
Joined: 03 Apr 2010 20:27
Location: Wellington, New Zealand
Contact:

Re: Bat File IP Monitoring

#8 Post by phillid » 25 Apr 2011 18:00

Yes, I used the FOR loop to search for the IP Address field, but on some computers it may not work I guess...
Well, I guess it's time to learn a higher level programming language like C or C++ then :D!

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Bat File IP Monitoring

#9 Post by nitt » 27 Apr 2011 16:54

phillid wrote:Yes, I used the FOR loop to search for the IP Address field, but on some computers it may not work I guess...
Well, I guess it's time to learn a higher level programming language like C or C++ then :D!


Go with C#. Not only does it have the syntax of C++, but it's no where near as hard. And very efficient.

scienceguru1.bat
Posts: 44
Joined: 01 Jan 2011 20:54

Re: Bat File IP Monitoring

#10 Post by scienceguru1.bat » 29 Apr 2011 13:47

for ipconfig the 2nd & 4th ip addresses in the1st catagory are the computer and router ip (don't remember which is which) try having it set the ip to a variable using some of the code above, and then have it moniter the ip you have. also when you have the ip at the start of the day set to a variable, use the echo command to tell you the current ip your computer has.

Post Reply