Page 1 of 1

Bat File IP Monitoring

Posted: 22 Apr 2011 16:21
by central10
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

Re: Bat File IP Monitoring

Posted: 22 Apr 2011 20:28
by nitt
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!".

Re: Bat File IP Monitoring

Posted: 23 Apr 2011 10:44
by central10
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!!

Re: Bat File IP Monitoring

Posted: 23 Apr 2011 17:10
by nitt
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.

Re: Bat File IP Monitoring

Posted: 23 Apr 2011 19:58
by phillid
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

Re: Bat File IP Monitoring

Posted: 24 Apr 2011 12:21
by nitt
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.

Re: Bat File IP Monitoring

Posted: 24 Apr 2011 12:41
by aGerman
IPCONFIG returns more than one IP adress. You have to define which of them you need.

Regards
aGerman

Re: Bat File IP Monitoring

Posted: 25 Apr 2011 18:00
by phillid
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!

Re: Bat File IP Monitoring

Posted: 27 Apr 2011 16:54
by nitt
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.

Re: Bat File IP Monitoring

Posted: 29 Apr 2011 13:47
by scienceguru1.bat
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.