中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --
联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
游客 | 登录 | 注册 | 会员 | 搜索 | 中国DOS联盟
中国DOS联盟论坛
现在时间是 2026-08-25 23:31
47,812 主题排行 / 349,910 发帖 / 今日 0 篇 / 48,264 会员排行
DOS批处理 & 脚本技术(批处理室) » 怎样获取当前网关 并添加静态路由
可打印版本  2,424 / 12
第1楼 lspzj 发表于 2007-05-09 15:20
初级用户 发帖 10 积分 24
怎样获取当前网关 并添加静态路由
首先获取当前网关
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楼 lxmxn 发表于 2007-05-09 15:32
版主 发帖 4,938 积分 11,386


for 命令是查找“ipconfig”命令的输出结果中含有"default gateway"的行,然后以“:”号作为分隔符,截取第二列,将其赋值给gateway变量。
第3楼 lspzj 发表于 2007-05-09 15:37
初级用户 发帖 10 积分 24
Originally posted by lxmxn at 2007-5-9 03:32 PM:


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

简单明了.谢谢..太谢谢哥们了.
:P:D
第4楼 lspzj 发表于 2007-05-09 15:42
初级用户 发帖 10 积分 24
Originally posted by lxmxn at 2007-5-9 03:32 PM:


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楼 lxmxn 发表于 2007-05-09 15:44
版主 发帖 4,938 积分 11,386
在批处理里面,要把%改成%%。也就是for里面的%i要改成%%i。
第6楼 lspzj 发表于 2007-05-09 15:49
初级用户 发帖 10 积分 24
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楼 lxmxn 发表于 2007-05-09 15:57
版主 发帖 4,938 积分 11,386
不知道你为什么不行,我这里是可以的
$for /F "delims=: tokens=2" %i in (' "ipconfig|find /i "default gateway" "') do @set tt=%i

$echo %tt%
192.168.1.1

$
第8楼 lspzj 发表于 2007-05-09 16:13
初级用户 发帖 10 积分 24
Originally posted by lxmxn at 2007-5-9 03:57 PM:
不知道你为什么不行,我这里是可以的

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

应该怎么指定 IPCONFIG显示特定的链接的网关
第9楼 flyinspace 发表于 2007-05-09 16:30
银牌会员 发帖 517 积分 1,206
试一下,我没有测试的。。
@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楼 lspzj 发表于 2007-05-09 16:36
初级用户 发帖 10 积分 24
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楼 lspzj 发表于 2007-05-09 16:41
初级用户 发帖 10 积分 24
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楼 lspzj 发表于 2007-05-09 16:44
初级用户 发帖 10 积分 24
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楼 flyinspace 发表于 2007-05-09 18:03
银牌会员 发帖 517 积分 1,206
再测试,开了回显就会出错的。。不开回显则不会
指出几点不足。。。请不要把回显打开。。会出问题的。。

@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
[ 联系联盟系统管理团队 - 中国DOS联盟 - 标准版 ]
Sponsored by ifanr Inc | © 2001–2023