我是批处理文件的完整新手,我只想完成一件事 获取某个路由的IP的接口ip,并将相同的接口用于ip的列表。
环顾四周,我设法提取了接口ip,但随后无法在路由部分的代码中使用它。
使用以下代码,您便会明白
set _netstat_cmd=netstat -nr
FOR /F "tokens=5" %G IN ('%_netstat_cmd% ^|find "10.11.3.31"') Do echo Result is %G
set rslt=%G
route delete 10.11.30.188
route delete 10.11.30.103
route delete 10.11.30.68
route delete 10.11.0.140
route delete 10.11.30.63
route add 10.11.30.188 mask 255.255.255.255 %rslt% METRIC 1
route add 10.11.30.103 mask 255.255.255.255 %rslt% METRIC 1
route add 10.11.30.68 mask 255.255.255.255 %rslt% METRIC 1
route add 10.11.0.140 mask 255.255.255.255 %rslt% METRIC 1
route add 10.11.30.63 mask 255.255.255.255 %rslt% METRIC 1
我想念什么? 我做了一些反复试验,但很难理解基本规则...
答案 0 :(得分:0)
在cmdline
上工作时,我们使用单个%
,但是在批处理文件内部,您需要将%
的符号变量加倍%%G
:delayedexpansion
为了运行每个结果,您需要一个代码块,因此如果我们不设置变量,则无需@echo off
set "_netstat_cmd=netstat -nr"
FOR /F "tokens=5" %%G IN ('%_netstat_cmd% ^|find "10.11.3.31"') Do (
echo Result is %%G
route delete 10.11.30.188
route delete 10.11.30.103
route delete 10.11.30.68
route delete 10.11.0.140
route delete 10.11.30.63
route add 10.11.30.188 mask 255.255.255.255 %%G METRIC 1
route add 10.11.30.103 mask 255.255.255.255 %%G METRIC 1
route add 10.11.30.68 mask 255.255.255.255 %%G METRIC 1
route add 10.11.0.140 mask 255.255.255.255 %%G METRIC 1
route add 10.11.30.63 mask 255.255.255.255 %%G METRIC 1
)
就可以做到。
delayedexpansion
但是,如果您确实要设置变量,则需要@echo off
setlocal enabledelayedexpansion
set "_netstat_cmd=netstat -nr"
FOR /F "tokens=5" %%G IN ('%_netstat_cmd% ^|find "10.11.3.31"') Do (
echo Result is %%G
set "rslt=%%G"
route delete 10.11.30.188
route delete 10.11.30.103
route delete 10.11.30.68
route delete 10.11.0.140
route delete 10.11.30.63
route add 10.11.30.188 mask 255.255.255.255 !rslt! METRIC 1
route add 10.11.30.103 mask 255.255.255.255 !rslt! METRIC 1
route add 10.11.30.68 mask 255.255.255.255 !rslt! METRIC 1
route add 10.11.0.140 mask 255.255.255.255 !rslt! METRIC 1
route add 10.11.30.63 mask 255.255.255.255 !rslt! METRIC 1
)
在代码块内扩展变量。
.txt file