日期:2014-05-17  浏览次数:20771 次

DOS编程取出每一结果行的IP并配置给一个变量
在命令列运行命令及结果如下:
  netstat -n | find ":3389" | find "ESTABLISHED" 
  TCP 192.168.31.120:3389 192.168.10.42:56773 ESTABLISHED
  TCP 192.168.31.120:3389 192.168.14.58:1704 ESTABLISHED

请教怎么用DOS编程取出每一行的对方IP并配置给一个变量
以上例为:
USER1=192.168.10.42
USER2=192.168.14.58

要能依不同行数进行配置如结果若为:
  TCP 192.168.31.120:3389 192.168.10.42:56773 ESTABLISHED
  TCP 192.168.31.120:3389 192.168.14.58:1704 ESTABLISHED
  TCP 192.168.31.120:3389 192.168.12.57:1704 ESTABLISHED
  TCP 192.168.31.120:3389 192.168.13.158:1704 ESTABLISHED
则需要配置成:
USER1=192.168.10.42
USER2=192.168.14.58
USER1=192.168.12.57
USER2=192.168.13.158

------解决方案--------------------
@echo off&setlocal enabledelayedexpansion
set /a nmu=1
for /f "tokens=4 delims=: " %%a in ('netstat -n ^| find ":3389" ^| find "ESTABLISHED" ') do (set name!nmu!=%%a
set /a nmu+=1
)
echo %name1%
echo %name2%
echo %name3%
echo %name4%
pause