中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-07 18:09
48,041 topics / 350,128 posts / today 3 new / 48,252 members
DOS批处理 & 脚本技术(批处理室) » Can a batch file tell whether the IP is the same?
Printable Version  865 / 10
Floor1 ka56 Posted 2008-03-11 13:07
中级用户 Posts 129 Credits 234
When I'm online I often run into cases where ADSL changes IPs. I hope to implement a function like this: use a batch file to disconnect, reconnect, display the current IP, compare the current IP with the previous IP, and if they're the same then disconnect and redial again... until it gets a new IP

I can only do the part before that. Please give me some guidance, experts, thanks!!
rasdial /disconnect
sleep 7000
rasdial adsl 123456@adsl 123456
sleep 150
ipconfig
sleep 6000
Floor2 pooronce Posted 2008-03-11 13:45
中级用户 Posts 250 Credits 484
If you have cygwin support and bring in the sed command, it's fairly easy:


If you use only cmd commands, it's a bit more troublesome, like this:

192.168 is used to filter out the local IP. If you don't have a local one, you can remove it; if you have some other local IP, just add another filter for it.
This will capture one extra space before the IP address, but it doesn't affect use. Or you can remove the space too.
Of course, if you really want to capture it exactly, just slightly change it to "tokens=14 delims=: ". But this 14 may not be universal on your machine, so you'll have to confirm for yourself whether it's the 14th position.

Once the IP address is captured into a variable, the rest is just comparing it and deciding whether redialing is needed. I believe you can finish that part yourself too.
Floor3 ka56 Posted 2008-03-11 14:10
中级用户 Posts 129 Credits 234
Originally posted by pooronce at 2008-3-11 13:45:
If you have cygwin support and bring in the sed command, it's fairly easy:
Floor4 ka56 Posted 2008-03-11 15:26
中级用户 Posts 129 Credits 234
There are a lot of things I don't understand. Could some expert kindly write out the complete code to implement this function? I'd be extremely grateful!!
Floor5 pooronce Posted 2008-03-11 15:35
中级用户 Posts 250 Credits 484
First go to cmd and run ipconfig, let me see what it displays
Floor6 ka56 Posted 2008-03-11 15:50
中级用户 Posts 129 Credits 234
Originally posted by pooronce at 2008-3-11 15:35:
First go to cmd and run ipconfig, let me see what it displays

Microsoft Windows XP
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Administrator>rasdial /disconnect
Command completed successfully.

Connecting to VNET_PPPOE...
Verifying username and password...
Registering your computer on the network...
Connected to VNET_PPPOE.
Command completed successfully.

C:\Documents and Settings\Administrator>ipconfig

Windows IP Configuration


Ethernet adapter Local Area Connection:

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

PPP adapter Vnet_PPPoE:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 125.78.56.190
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : 125.78.56.190
Floor7 pooronce Posted 2008-03-11 16:59
中级用户 Posts 250 Credits 484
Give this a try:



[ Last edited by pooronce on 2008-3-11 at 05:30 PM ]
Floor8 abcd Posted 2008-03-11 17:16
银牌会员 Posts 739 Credits 1,436
if not pc%goon%=pcy goto :eof
Missing =
Floor9 terse Posted 2008-03-11 17:19
银牌会员 Posts 946 Credits 2,404
Floor10 pooronce Posted 2008-03-11 17:29
中级用户 Posts 250 Credits 484
Originally posted by abcd at 2008-3-11 05:16 PM:
if not pc%goon%=pcy goto :eof
Missing =


Oh,
didn't notice it~

[ Last edited by pooronce on 2008-3-11 at 05:31 PM ]
Floor11 ka56 Posted 2008-03-11 17:34
中级用户 Posts 129 Credits 234
Originally posted by terse at 2008-3-11 17:19:
@echo off
for /f "tokens=2 delims=:" %%i in ('ipconfig /all^|find /i "Address"') do set ip1=%%i
:aaa
rasdial /disconnect&&rasdial adsl 123456@adsl 123456
for /f &quo ...


Thanks, experts, it's already working!!!! :))
Thanks pooronce!!! Thanks to the brothers who replied, to the friends above who helped me, if you like QQ numbers, I'll give away a QQ one...
Added a delay, here is the final code:
@echo off
for /f "tokens=2 delims=:" %%i in ('ipconfig /all^|find /i "Address"') do set ip1=%%i
echo Current IP %ip1%
:aaa
rasdial /disconnect
sleep 5000
rasdial adsl 123456@qzadsl 123456
for /f "tokens=2 delims=:" %%i in ('ipconfig /all^|find /i "Address"') do set ip2=%%i
echo IP after redial %ip2%
if "%ip1%"=="%ip2%" goto aaa
pause
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023