Board logo

标题: 怎样获取当前网关 并添加静态路由 [打印本页]

作者: lspzj     时间: 2007-5-9 15:20    标题: 怎样获取当前网关 并添加静态路由
首先获取当前网关
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 ]

作者: lxmxn     时间: 2007-5-9 15:32
route add 0.0.0.0 mask 0.0.0.0 %gateway%


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

作者: lspzj     时间: 2007-5-9 15:37
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

作者: lspzj     时间: 2007-5-9 15:42
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%


执行错误啊

作者: lxmxn     时间: 2007-5-9 15:44
在批处理里面,要把%改成%%。也就是for里面的%i要改成%%i。

作者: lspzj     时间: 2007-5-9 15:49
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.

执行到这就不行了

作者: lxmxn     时间: 2007-5-9 15:57
不知道你为什么不行,我这里是可以的
$for /F "delims=: tokens=2" %i in (' "ipconfig|find /i "default gateway" "') do @set tt=%i

$echo %tt%
192.168.1.1

$

作者: lspzj     时间: 2007-5-9 16:13
Originally posted by lxmxn at 2007-5-9 03:57 PM:
不知道你为什么不行,我这里是可以的

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

应该怎么指定 IPCONFIG显示特定的链接的网关

作者: flyinspace     时间: 2007-5-9 16:30    标题: 试一下,我没有测试的。。
@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

作者: lspzj     时间: 2007-5-9 16:36
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
请按任意键继续. . .

作者: lspzj     时间: 2007-5-9 16:41
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*.*.*的值

作者: lspzj     时间: 2007-5-9 16:44
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\毛毛虫>

作者: flyinspace     时间: 2007-5-9 18:03    标题: 再测试,开了回显就会出错的。。不开回显则不会
指出几点不足。。。请不要把回显打开。。会出问题的。。

@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