中国DOS联盟

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

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

中国DOS联盟论坛
现在时间是 2026-06-14 18:55
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 怎样获取当前网关 并添加静态路由
楼 主 怎样获取当前网关 并添加静态路由 发表于 2007-05-09 15:20 ·  中国 河南 许昌 电信
初级用户
积分 24
发帖 10
注册 2006-11-08 01:30
UID 69964
性别 男
状态 离线
首先获取当前网关
for /f "delims=: tokens=2" %%i in ('"ipconfig | find /i "default gateway""') do set gateway=%%i

然后得到的 添加路由表

route add 0.0.0.0 mask 0.0.0.0 *.*.*.*

怎么把两行连起来做到一起..呵呵.我是个白菜.请赐教
最好简单说明一下 FOR那行的意思


[ Last edited by lspzj on 2007-5-9 at 03:22 PM ]
2 发表于 2007-05-09 15:32 ·  中国 湖北 武汉 电信
版主
★★★★★
积分 11,386
发帖 4,938
注册 2006-07-23 17:10
UID 59080
状态 离线
route add 0.0.0.0 mask 0.0.0.0 %gateway%


for 命令是查找“ipconfig”命令的输出结果中含有"default gateway"的行,然后以“:”号作为分隔符,截取第二列,将其赋值给gateway变量。
3 发表于 2007-05-09 15:37 ·  中国 河南 许昌 电信
初级用户
积分 24
发帖 10
注册 2006-11-08 01:30
UID 69964
性别 男
状态 离线
Originally posted by lxmxn at 2007-5-9 03:32 PM:
route add 0.0.0.0 mask 0.0.0.0 %gateway%


for 命令是查找“ipconfig”命令的输出结果中含有"default gateway"的行,然后以“:”号作为分隔符,截取第二列,将其赋值给gateway变量。

简单明了.谢谢..太谢谢哥们了.
:P:D
4 发表于 2007-05-09 15:42 ·  中国 河南 许昌 电信
初级用户
积分 24
发帖 10
注册 2006-11-08 01:30
UID 69964
性别 男
状态 离线
Originally posted by lxmxn at 2007-5-9 03:32 PM:
route add 0.0.0.0 mask 0.0.0.0 %gateway%


for 命令是查找“ipconfig”命令的输出结果中含有"default gateway"的行,然后以“:”号作为分隔符,截取第二列,将其赋值给gateway变量。


还是不行啊.加引号后

for /f "delims=: tokens=2" %%i in ('"ipconfig | find /i "default gateway""') do set gateway=%%i
route add 0.0.0.0 mask 0.0.0.0 %gateway%


执行错误啊
5 发表于 2007-05-09 15:44 ·  中国 湖北 武汉 电信
版主
★★★★★
积分 11,386
发帖 4,938
注册 2006-07-23 17:10
UID 59080
状态 离线
在批处理里面,要把%改成%%。也就是for里面的%i要改成%%i。
6 发表于 2007-05-09 15:49 ·  中国 河南 许昌 电信
初级用户
积分 24
发帖 10
注册 2006-11-08 01:30
UID 69964
性别 男
状态 离线
Originally posted by lxmxn at 2007-5-9 03:44 PM:
在批处理里面,要把%改成%%。也就是for里面的%i要改成%%i。

谢谢哥们关注 .你跟贴真快

我就是%%i 发的是候写错了.后编辑了

C:\Documents and Settings\毛毛虫\桌面>for /F "delims=: tokens=2" %i in ('"ipco
ig | find /i "default gateway""') do set tt=%i

:\Documents and Settings\毛毛虫\桌面>set tt=

:\Documents and Settings\毛毛虫\桌面>set tt=

:\Documents and Settings\毛毛虫\桌面>set tt=

:\Documents and Settings\毛毛虫\桌面>set tt= 123.4.174.182

:\Documents and Settings\毛毛虫\桌面>set tt=

C:\Documents and Settings\毛毛虫\桌面>route add 0.0.0.0 mask 0.0.0.0

Manipulates network routing tables.

执行到这就不行了
7 发表于 2007-05-09 15:57 ·  中国 湖北 武汉 电信
版主
★★★★★
积分 11,386
发帖 4,938
注册 2006-07-23 17:10
UID 59080
状态 离线
不知道你为什么不行,我这里是可以的
$for /F "delims=: tokens=2" %i in (' "ipconfig|find /i "default gateway" "') do @set tt=%i

$echo %tt%
192.168.1.1

$
8 发表于 2007-05-09 16:13 ·  中国 河南 安阳 联通
初级用户
积分 24
发帖 10
注册 2006-11-08 01:30
UID 69964
性别 男
状态 离线
Originally posted by lxmxn at 2007-5-9 03:57 PM:
不知道你为什么不行,我这里是可以的

我找到原因了
你看我上面的DOS结果
他把最后一个空白项赋予%TT%了

应该怎么指定 IPCONFIG显示特定的链接的网关
9 试一下,我没有测试的。。 发表于 2007-05-09 16:30 ·  中国 广东 广州 天河区 电信
银牌会员
★★★
积分 1,206
发帖 517
注册 2007-03-25 01:18
UID 82819
性别 男
状态 离线
@echo off

for /f "tokens=* " %%i in ('ipconfig ^| find /i "def"') do (
for %%a in (%%i) do set "x=%%a"
)
route add 0.0.0.0 mask 0.0.0.0 %x%
pause
知,不觉多。不知,乃求知
10 发表于 2007-05-09 16:36 ·  中国 河南 许昌 电信
初级用户
积分 24
发帖 10
注册 2006-11-08 01:30
UID 69964
性别 男
状态 离线
Originally posted by flyinspace at 2007-5-9 04:30 PM:
@echo off

for /f "tokens=* " %%i in ('ipconfig ^| find /i "def"') do (
for %%a in (%%i) do set "x=%%a"
)
route add 0.0.0.0 mask 0.0.0.0 %x%
pause



不行啊.

C:\Documents and Settings\毛毛虫\桌面>(for %a in (Default Gateway . . . . . . .
) do set "x=%a" )

C:\Documents and Settings\毛毛虫\桌面>set "x=Default"

C:\Documents and Settings\毛毛虫\桌面>set "x=Gateway"

C:\Documents and Settings\毛毛虫\桌面>set "x=."

C:\Documents and Settings\毛毛虫\桌面>set "x=."

C:\Documents and Settings\毛毛虫\桌面>set "x=."

C:\Documents and Settings\毛毛虫\桌面>set "x=."

C:\Documents and Settings\毛毛虫\桌面>set "x=."

C:\Documents and Settings\毛毛虫\桌面>set "x=."

C:\Documents and Settings\毛毛虫\桌面>set "x=."

C:\Documents and Settings\毛毛虫\桌面>set "x=."

C:\Documents and Settings\毛毛虫\桌面>set "x=."

C:\Documents and Settings\毛毛虫\桌面>set "x=:"

C:\Documents and Settings\毛毛虫\桌面>route add 0.0.0.0 mask 0.0.0.0 :
route: bad gateway address :

C:\Documents and Settings\毛毛虫\桌面>pause
请按任意键继续. . .
11 发表于 2007-05-09 16:41 ·  中国 河南 许昌 电信
初级用户
积分 24
发帖 10
注册 2006-11-08 01:30
UID 69964
性别 男
状态 离线
C:\Documents and Settings\毛毛虫\桌面>for /F "delims=: tokens=2" %i in ('"ipco
ig | find /i "default gateway""') do set tt=%i

:\Documents and Settings\毛毛虫\桌面>set tt=

:\Documents and Settings\毛毛虫\桌面>set tt=

:\Documents and Settings\毛毛虫\桌面>set tt=

:\Documents and Settings\毛毛虫\桌面>set tt= 123.4.174.182

:\Documents and Settings\毛毛虫\桌面>set tt=

C:\Documents and Settings\毛毛虫\桌面>route add 0.0.0.0 mask 0.0.0.0

Manipulates network routing tables.

怎么能把 最后一个空值跳过.或者 不查找 空白值. 或者只查找12*.*.*的值
12 发表于 2007-05-09 16:44 ·  中国 河南 许昌 电信
初级用户
积分 24
发帖 10
注册 2006-11-08 01:30
UID 69964
性别 男
状态 离线
C:\Documents and Settings\毛毛虫>ipconfig

Windows IP Configuration


Ethernet adapter 虚拟机8:

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

Ethernet adapter 虚拟机1:

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

Ethernet adapter 本地连接:

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

PPP adapter 宽带登陆:

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

PPP adapter 网通加速器【UC软件应用】:

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

C:\Documents and Settings\毛毛虫>
13 再测试,开了回显就会出错的。。不开回显则不会 发表于 2007-05-09 18:03 ·  中国 广东 广州 联通
银牌会员
★★★
积分 1,206
发帖 517
注册 2007-03-25 01:18
UID 82819
性别 男
状态 离线
指出几点不足。。。请不要把回显打开。。会出问题的。。

@echo off

for /f "tokens=* " %%i in ('ipconfig ^| find /i "def" ^| findstr /b /r "[0-9]"') do (
for %%a in (%%i) do set "x=%%a"
)
route add 0.0.0.0 mask 0.0.0.0 %x%
pause
知,不觉多。不知,乃求知
论坛跳转: