[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[网络连接] 批处理提取ipconfig/all输出中的DHCP-IP作route命令的输入参数?

RT,我有多网卡,希望提取ipconfig /all输出中的其中一块网卡的DHCP服务器IP,作为route命令的输入参数,不知能否实现?

查了一下,发现一个贴的问题跟我的非常的相似:
[求助]用何命令获取PPPoE拨号连接的IP?
我的问题是这样:

对于如图的IP设置,我要提取后面一块网卡的DHCP的IP:10.104.176.145,作为route命令的参数:
route add 0.0.0.0 mask 0.0.0.0 10.104.176.145

因为这个值是会变化的,所以想做个批处理可以动态更新路由表。

参照找到那个帖子的,我写了下面的语句,可是却不成功,各位帮下忙了
  1. for /f "tokens=1,2* delims=:" %%i in ('ipconfig /all') do (
  2. if /i "%%j"==" Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
  3. if defined flag (
  4. if "%%i "==" DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0 %%j))
复制代码

返回信息

附上返回信息:


D:\temp\CMD>for /F "tokens=1,2* delims=:" %i in ('ipconfig /all') do (
if /I "%j" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
if defined flag (if "%i " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0 %j )
)
D:\temp\CMD>(
if /I "" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
" == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  )
)
D:\temp\CMD>(
if /I "" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
" == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  )
)
D:\temp\CMD>(
if /I "" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
" == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  )
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Host Name . . . . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  K-K-K
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Primary Dns Suffix  . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Node Type . . . . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  Unknown
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        IP Routing Enabled. . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  No
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        WINS Proxy Enabled. . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  No
)
D:\temp\CMD>(
if /I "" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
" == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  )
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "Ethernet adapter Local Area Connection " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0
)
D:\temp\CMD>(
if /I "" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
" == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  )
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Connection-specific DNS Suffix  .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Description . . . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  Realtek RTL8169/8
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Physical Address. . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  00-E0-4C-5B-28-F3
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Dhcp Enabled. . . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  No
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        IP Address. . . . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  172.16.143.180
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Subnet Mask . . . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  255.255.255.0
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Default Gateway . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        DNS Servers . . . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  172.16.2.1
)
D:\temp\CMD>(
if /I "" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
" == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  )
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "Ethernet adapter {C254AFDB-E199-471E-AB9D-A6E7D08B36DA} " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0
)
D:\temp\CMD>(
if /I "" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
" == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  )
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Connection-specific DNS Suffix  .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Description . . . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  Netpas Win32 Virt
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Physical Address. . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  00-FF-C2-54-AF-DB
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Dhcp Enabled. . . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  Yes
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Autoconfiguration Enabled . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  Yes
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        IP Address. . . . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  10.104.176.146
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Subnet Mask . . . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  255.255.255.252
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        Default Gateway . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  10.104.176.145
)
D:\temp\CMD>(
" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
)  defined flag (if "        DHCP Server . . . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  10.104.176.145
)
D:\temp\CMD>(
if /I " 2008年12月7日 7" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
if defined flag (if "        Lease Obtained. . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  2008年12月7日 7 )
)
D:\temp\CMD>(
if /I " 2008年12月9日 7" == " Netpas Win32 Virtual Network Adapter - Packet Scheduler Miniport" set flag=a
if defined flag (if "        Lease Expires . . . . . . . . . .  " == "        DHCP Server . . . . . . . . . . . " route -p add 0.0.0.0 mask 0.0.0.0  2008年12月9日 7 )
)

TOP

  1. for /f "tokens=1,2* delims=:" %%i in ('ipconfig /all^|find "IP Address"') do (
  2.   set IP=%%j
  3. )
  4. route -p add 0.0.0.0 mask 0.0.0.0 %IP%
复制代码
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

Batcher大:

非常多谢你的回复!我看了你的代码,觉得你理解错我的问题了,你的代码是把当前的IP地址作为后面命令的参数输入,是这样吧?我其实是想把DHCP服务器的IP作为参数输入。

我把“IP Address”改成“DHCP Server”,成功执行了。

不过有个小问题,就是我是双网卡的,刚好我的第一块网卡没有使用DHCP,上面没有错误。我想问一下,如果我的第一块网卡也使用了DHCP,那是不是没法选择那块网卡的DHCPIP了?

另外,我一楼改写的代码尝试去定位网卡再提取,可为什么不成功呢?还是批处理新手,希望解决这个问题学习下,呵呵!

TOP

回复 4楼 的帖子

第一块网卡的DHCP:
  1. @echo off
  2. for /f "tokens=1,2* delims=:" %%i in ('ipconfig /all^|find "DHCP Server"') do (
  3.   set IP=%%j
  4.   route -p add 0.0.0.0 mask 0.0.0.0 %IP%
  5.   goto :eof
  6. )
复制代码
最后一块网卡的DHCP:
  1. @echo off
  2. for /f "tokens=1,2* delims=:" %%i in ('ipconfig /all^|find "DHCP Server"') do (
  3.   set IP=%%j
  4. )
  5. route -p add 0.0.0.0 mask 0.0.0.0 %IP%
复制代码
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

返回列表