中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

中国DOS联盟论坛
现在时间是 2026-08-08 02:25
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [解决]如何用批处理获取不同网络连接的网关 查看 1,012 回复 5
楼 主 [解决]如何用批处理获取不同网络连接的网关 发表于 2008-04-12 00:12 ·  中国 上海 上海电信科技发展有限公司(中山南路111号)
新手上路
积分 8
发帖 3
注册 2008-04-11 23:25
18年会员
UID 115652
性别 男
状态 离线
C:\>ipconfig

Windows IP Configuration


Ethernet adapter 本地连接:

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

Ethernet adapter {5982BEEE-FC3F-43B1-9262-F2C4BF4AD972}:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 10.255.255.253
Subnet Mask . . . . . . . . . . . : 255.255.255.224
Autoconfiguration IP Address. . . : 169.254.19.67
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :

PPP adapter ADSL:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 118.147.61.160
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : 118.147.61.160

PPP adapter 铁通去网通加速:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 172.29.23.169
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : 172.29.23.169

PPP adapter 铁通去电信加速:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 172.28.7.174
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : 172.28.7.174


要求是将上面“ADSL”这个拨号连接的网关、“铁通去网通加速”和“铁通去电信加速”这两个VPN连接的网关,分别赋值给批处理中的%ctt%%cnc%%tel%这3个变量

只需要帮我做完上面的操作即可,我的最终目的是用route命令来改路由表了,写批处理避免重复劳动,但IP都是动态的,用到for命令我就搞不掂了

先行感谢大家的帮忙

[ Last edited by feifeiyucn on 2008-4-12 at 04:54 PM ]
2 不知道你有没有Perl环境 发表于 2008-04-12 13:21 ·  中国 湖北 武汉 电信
中级用户
★★
积分 471
发帖 207
注册 2007-05-03 14:53
19年会员
UID 87369
性别 男
状态 离线
use strict;
use warnings;

my $infile = shift || "ipconfig.txt" || die "Can't open the ipconfig.txt file : $!\n";

open OK,$infile;
undef $/;
my $text=<OK>;
my($ctt,$cnc,$tel);

if ($text =~ /PPP adapter ADSL:.*?Default Gateway.+?:\s*(+)/s) {
$ctt=$1;
}

if ( $text =~ /PPP adapter 铁通去网通加速:.*?Default Gateway.+?:\s*(+)/s) {
$cnc=$1;
}
if ( $text =~ /PPP adapter 铁通去电信加速:.*?Default Gateway.+?(+)$/s) {
$tel=$1;
}
close $infile;
print "\$ctt is $ctt\n\$cnc is $cnc\n\$tel is $tel\n";
3 发表于 2008-04-12 13:53 ·  中国 上海 上海电信科技发展有限公司(中山南路111号)
新手上路
积分 8
发帖 3
注册 2008-04-11 23:25
18年会员
UID 115652
性别 男
状态 离线
回复楼上的:没有。。。我只希望用批处理解决这个问题

手滑了,不小心将这里内容删了,简要重新写一下吧

将网关都写进gw.txt里
暂时ADSL拨号连接都是118开头,铁通到电信加速都是172.28开头,铁通到网通加速都是172.29开头
如何分别读取出来赋值给%ctt%,%tel%,%cnc%?

[ Last edited by feifeiyucn on 2008-4-12 at 02:32 PM ]
4 发表于 2008-04-12 14:09 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
积分 3,105
发帖 1,276
注册 2008-03-08 13:00
18年会员
UID 112398
性别 男
状态 离线
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in (gw.txt) do (
set a=%%i
if "!a:~,3!"=="118" set ctt=!a!
if "!a:~,6!"=="172.28" set tel=!a!
if "!a:~,6!"=="172.29" set cnc=!a!
)
批处理之家新域名:www.bathome.net
5 发表于 2008-04-12 14:25 ·  中国 北京 华为云
银牌会员
★★★
积分 1,436
发帖 739
注册 2007-10-11 17:44
18年会员
UID 99469
性别 男
状态 离线
Originally posted by zw19750516 at 2008-4-12 02:09 PM:
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in (gw.txt) do (
set a=%%i
if "!a:~,3!"=="118" set ctt=!a!
if "!a:~,6! ...

真聪明。。。。
6 发表于 2008-04-12 14:40 ·  中国 上海 上海电信科技发展有限公司(中山南路111号)
新手上路
积分 8
发帖 3
注册 2008-04-11 23:25
18年会员
UID 115652
性别 男
状态 离线
Originally posted by zw19750516 at 2008-4-12 14:09:
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in (gw.txt) do (
set a=%%i
if "!a:~,3!"=="118" set ctt=!a!
if "!a:~,6! ...


有点问题,gw.txt每行前面都有个空格,改成这样就行了(暂时用了echo来代替set)

@echo off&setlocal enabledelayedexpansion
if exist gw.txt del gw.txt
for /f "tokens=1* delims=:" %%i in ('ipconfig^|find /i "Default Gateway"') do @echo %%j>>gw.txt

for /f "delims=" %%i in (gw.txt) do (
set a=%%i
if "!a:~1,3!"=="118" echo ctt=!a:~1!
if "!a:~1,6!"=="172.28" echo tel=!a:~1!
if "!a:~1,6!"=="172.29" echo cnc=!a:~1!
)

pause


[ Last edited by feifeiyucn on 2008-4-12 at 02:56 PM ]
论坛跳转: