China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-07 19:00
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Can a batch file tell whether the IP is the same? View 867 Replies 10
Original Poster Posted 2008-03-11 13:07 ·  中国 福建 泉州 电信
中级用户
★★
Credits 234
Posts 129
Joined 2006-11-28 09:21
19-year member
UID 71989
Gender Male
Status Offline
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
Floor 2 Posted 2008-03-11 13:45 ·  中国 上海 黄浦区 电信
中级用户
★★
Credits 484
Posts 250
Joined 2007-06-05 23:33
19-year member
UID 90372
Gender Male
Status Offline
If you have cygwin support and bring in the sed command, it's fairly easy:

for /f "tokens=1 delims=" %%i in ('ipconfig ^| sed -n -e '/adsl/{n^;n^;n^;s/.*\: //^;p}'') do set adip=%%i


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

for /f "tokens=2 delims=:" %i in ('ipconfig ^| findstr "Address" ^| findstr /v "192.168"') do @set adip=%i

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.
Floor 3 Posted 2008-03-11 14:10 ·  中国 福建 泉州 电信
中级用户
★★
Credits 234
Posts 129
Joined 2006-11-28 09:21
19-year member
UID 71989
Gender Male
Status Offline
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:

for /f "tokens=1 delims=" %%i in ('ipconfig ^| sed -n -e '/adsl/{n^;n^;n^;s/.*\: //^;p}'') do set adip=%%i
...

Thanks, expert pooronce!! I'm really a newbie, I'll study it some more...
Floor 4 Posted 2008-03-11 15:26 ·  中国 福建 泉州 电信
中级用户
★★
Credits 234
Posts 129
Joined 2006-11-28 09:21
19-year member
UID 71989
Gender Male
Status Offline
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!!
Floor 5 Posted 2008-03-11 15:35 ·  中国 上海 黄浦区 电信
中级用户
★★
Credits 484
Posts 250
Joined 2007-06-05 23:33
19-year member
UID 90372
Gender Male
Status Offline
First go to cmd and run ipconfig, let me see what it displays
Floor 6 Posted 2008-03-11 15:50 ·  中国 福建 泉州 电信
中级用户
★★
Credits 234
Posts 129
Joined 2006-11-28 09:21
19-year member
UID 71989
Gender Male
Status Offline
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
Floor 7 Posted 2008-03-11 16:59 ·  中国 上海 黄浦区 电信
中级用户
★★
Credits 484
Posts 250
Joined 2007-06-05 23:33
19-year member
UID 90372
Gender Male
Status Offline
Give this a try:


@echo off
::capture old IP
for /f "tokens=2 delims=:" %%i in ('ipconfig ^| findstr "Address" ^| findstr /v "192.168"') do @set oldip=%%i
echo Your current IP address is %oldip%, confirm that you want to redial to change the IP? Enter y and press Enter to continue
set /P goon=
if not pc%goon%==pcy goto :eof

::loop starts here
:start

rasdial adsl /DISCONNECT
ping 127.0.0.1 -n 2 >nul

echo Redialing...
rasdial adsl ad4148414 pspsljljhaha
for /f "tokens=2 delims=:" %%i in ('ipconfig ^| findstr "Address" ^| findstr /v "192.168"') do @set newip=%%i
if %newip%==%oldip% (
echo The newly dialed IP is the same as the old IP, try again!
goto :start
)

echo IP change successful, the current IP is:%newip%
echo Press any key to exit
set oldip=
set newip=
set goon=
pause&exit


[ Last edited by pooronce on 2008-3-11 at 05:30 PM ]
Floor 8 Posted 2008-03-11 17:16 ·  中国 浙江 杭州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
if not pc%goon%=pcy goto :eof
Missing =
Floor 9 Posted 2008-03-11 17:19 ·  中国 江苏 常州 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
简单!简单!再简单!
Floor 10 Posted 2008-03-11 17:29 ·  中国 上海 黄浦区 电信
中级用户
★★
Credits 484
Posts 250
Joined 2007-06-05 23:33
19-year member
UID 90372
Gender Male
Status Offline
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 ]
Floor 11 Posted 2008-03-11 17:34 ·  中国 福建 泉州 电信
中级用户
★★
Credits 234
Posts 129
Joined 2006-11-28 09:21
19-year member
UID 71989
Gender Male
Status Offline
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
Forum Jump: