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-03 13:01
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 【Original】:Gateway Automatic Switcher. View 1,643 Replies 3
Original Poster Posted 2007-02-16 02:27 ·  中国 浙江 温州 电信
初级用户
★★
Credits 154
Posts 67
Joined 2006-10-31 00:50
19-year member
UID 68933
Gender Male
Status Offline
I see a lot of friends in the forum looking for this thing. I couldn't find it when I needed it some time ago. So I wrote this. There are still many bugs to fix and improve, but basically it works. Now I'm sending it out to those who need it.

****************** Code Segment ********************

@Title IP Gateway Automatic Switcher By Hanhe & Color 0a&&Mode CON Cols=45 Lines=15 &&Echo OFF
If Exist config.ini Del config.ini
@Echo The program will be automatically deleted after execution!>config.ini&Ipconfig|Find "IP Address">>config.ini&&Ipconfig|Find "Default Gateway" >>config.ini&&Ipconfig/all|Find "Ethernet adapter">>config.ini

::::::::::::::::: Define detailed variables :::::::::::::::::

Set First=192.168
Set Scend1=1
Set Scend2=0
Set Mask=255.255.255.0
Set Gate1=192.168.1.200
Set Gate2=192.168.0.200

::::::::::::::::: Get the local IP address information :::::::::::::::::

For /f "SkIp=2 Tokens=13" %%d in (config.ini) do Set Gate=%%d
For /f "SkIp=1 Tokens=15" %%c in (config.ini) do Set Ip=%%c
For /f "SkIp=3 Tokens=3,4 Delims=: " %%a in (config.ini) do Set Name=%%a %%b
@Echo %Ip% >>config.ini
For /f "SkIp=4 Tokens=4 Delims=./ " %%f in (config.ini) do Set Shree=%%f
@Echo ⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙
@Echo ⊙The current network information of your machine is as follows: ⊙
@Echo ⊙Connection name: %Name% ⊙
@Echo ⊙Used IP: %Ip% ⊙
@Echo ⊙Used gateway: %Gate% ⊙
@Echo ⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙

::::::::::::::::: Judge the network segment where the local machine is located :::::::::::::::::

IF /i "%Gate%"=="192.168.1.200" (Goto A_INTERNAME) Else (IF /i "%Gate%"=="192.168.0.200" (Goto B_INTERNAME))


::::::::::::::::: Processing for segment 1 :::::::::::::::::

:A_INTERNAME
@Echo.
@Echo Processing, please be patient...
Netsh interface Ip Set address "%Name%" static %First%.%Scend2%.%Shree% %Mask% %Gate2% 1
@Cls
@Echo Your gateway has been successfully switched. Current gateway (0).
For /f "SkIp=7 Tokens=15" %%k in ('Ipconfig') do Echo The modified network information of you is as follows: %%k
For /f "SkIp=9 Tokens=13" %%g in ('Ipconfig') do Echo The modified network information of you is as follows: %%g
Del config.ini
@Echo.
@Echo The line has been successfully switched. Press any key to exit the program.
@pause>nul
@exit

::::::::::::::::: Processing for segment 0 :::::::::::::::::

:B_INTERNAME
@Echo Processing, please be patient...
Netsh interface Ip Set address "%Name%" static %First%.%Scend1%.%Shree% %Mask% %Gate1% 1
@Cls
@Echo Your gateway has been successfully switched. Current gateway (1).
For /f "SkIp=7 Tokens=15" %%k in ('Ipconfig') do Echo The modified network information of you is as follows: %%k
For /f "SkIp=9 Tokens=13" %%g in ('Ipconfig') do Echo The modified network information of you is as follows: %%g
Del config.ini
@Echo.
@Echo The line has been successfully switched. Press any key to exit the program.
@pause>nul
@exit

********************* End *********************

The code is not encrypted. Friends who need it can modify it themselves. I wrote this just for my own convenience.
I didn't consider the convenience for the public..
I'll improve it if I have the opportunity in the future..

[ Last edited by newaifi on 2007-2-16 at 10:50 PM ]
Floor 2 Posted 2007-02-16 06:15 ·  中国 湖南 衡阳 电信
初级用户
Credits 24
Posts 10
Joined 2007-02-03 06:54
19-year member
UID 78499
Gender Male
Status Offline
I don't know what the use of such a long piece of code is... I tried it, and I only know that it switches gateways like 192.168. But if it's another gateway, it simply doesn't switch. This is called automatic? It's not as convenient as changing it manually.
Floor 3 Posted 2007-02-16 22:56 ·  中国 浙江 温州 电信
初级用户
★★
Credits 154
Posts 67
Joined 2006-10-31 00:50
19-year member
UID 68933
Gender Male
Status Offline
I have already said. Writing this is just for my own convenience. I didn't consider the needs of the public. Those who need it can take it and modify it.

As for why it can only modify the 192.168 segment, I think anyone with a little common sense should understand. Some things are completely impossible to achieve automatically. Even if it can recognize your IP, the program may not know which segment you want to modify, and it can't know how many segments you have. So I used the ::::::: symbol above this thing to declare it as a variable, and users need to modify it themselves. If you want to enjoy the fruits without effort, you won't learn anything..
Floor 4 Posted 2007-02-24 16:51 ·  中国 广东 肇庆 四会市 电信
中级用户
★★
Credits 384
Posts 189
Joined 2005-10-19 13:12
20-year member
UID 43709
Gender Male
Status Offline
Originally posted by windows9x at 2007-2-16 06:15:
I don't know what this long piece of code is for... I tried it and only know that it can switch gateways like 192.168. If it's another gateway, it simply won't switch. This is called automatic? It's not as convenient as manual change.



This can first obtain the current network segment... save it...

Then change the network segment...

Finally, when it needs to be restored, just take out the previously saved network segment and restore it. Isn't that okay...
Forum Jump: