Board logo

标题: [文本处理] 根据自动获取的IP、DNS设置成固定,并自动识别网卡名称 [打印本页]

作者: duduchong    时间: 2019-8-10 19:14     标题: 根据自动获取的IP、DNS设置成固定,并自动识别网卡名称

前半部分已经解决,网卡名称这部分是在头痛,以下是前半部分:
  1. @echo off
  2. ipconfig>ipc.txt
  3. netsh interface ip show address>gwc.txt
  4. for /f "delims=: tokens=2" %%a in ('find "IPv4 地址" ipc.txt') do set IP=%%a
  5. for /f "delims=: tokens=2" %%b in ('find "子网掩码" ipc.txt') do set mask=%%b
  6. for /f "delims=: tokens=2" %%i in ('find "默认网关" gwc.txt') do set gw=%%i
  7. netsh interface ip set address "本地连接" static %IP% %mask% %gw% 0
  8. netsh interface ip set dns name="本地连接" source=static addr=10.46.20.96
  9. netsh interface ip add dns name="本地连接" addr=10.46.20.97 index=2
  10. del ipc.txt
  11. del gwc.txt
复制代码
各位大神有没有办法,本地连接能够自动识别,好多电脑名称都不一样,有本地连接1~5都碰到过
作者: Batcher    时间: 2019-8-11 16:13

情况比较复杂,遇到过电脑有4块板载网卡和1块外置USB无线网卡
作者: duduchong    时间: 2019-8-11 19:00

本地连接一般只有一个,但是后面的数字就不一定是几了
作者: /zhqsystem/zhq    时间: 2019-8-11 20:49

用序列递增变量,获取时用 for /l 获取这些变量
作者: Batcher    时间: 2019-8-11 21:00

回复 3# duduchong


    我们这边的实际情况是有几块板载网卡就有几个本地链接x
作者: duduchong    时间: 2019-8-12 18:37

可能是我没表达清楚,我需要使用的电脑上,只有一个本地连接,但是本地连接后面有数字,有些电脑是2,有些是4,所以不需要区别
作者: 窄口牛    时间: 2019-8-12 21:26

不需要区别,就先把无效的本地连接全部删除,然后你随便搞了。
作者: duduchong    时间: 2019-8-14 19:04

哪位大哥帮我改改,获取名称啊
作者: duduchong    时间: 2019-8-18 08:57

用了个很SB的办法解决了
@echo off

ipconfig>ipc.txt

netsh interface ip show address>gwc.txt

for /f "delims=: tokens=2" %%a in ('find "IPv4 地址" ipc.txt') do set IP=%%a

for /f "delims=: tokens=2" %%b in ('find "子网掩码" ipc.txt') do set mask=%%b

for /f "delims=: tokens=2" %%i in ('find "默认网关" gwc.txt') do set gw=%%i

netsh interface ip set address "本地连接" static %IP% %mask% %gw% 0

netsh interface ip set dns name="本地连接" source=static addr=10.46.20.96

netsh interface ip add dns name="本地连接" addr=10.46.20.97 index=2

netsh interface ip set address "本地连接 2" static %IP% %mask% %gw% 0

netsh interface ip set dns name="本地连接 2" source=static addr=10.46.20.96

netsh interface ip add dns name="本地连接 2" addr=10.46.20.97 index=2

netsh interface ip set address "本地连接 3" static %IP% %mask% %gw% 0

netsh interface ip set dns name="本地连接 3" source=static addr=10.46.20.96

netsh interface ip add dns name="本地连接 3" addr=10.46.20.97 index=2

netsh interface ip set address "本地连接 4" static %IP% %mask% %gw% 0

netsh interface ip set dns name="本地连接 4" source=static addr=10.46.20.96

netsh interface ip add dns name="本地连接 4" addr=10.46.20.97 index=2

netsh interface ip set address "以太网" static %IP% %mask% %gw% 0

netsh interface ip set dns name="以太网" source=static addr=10.46.20.96

netsh interface ip add dns name="以太网" addr=10.46.20.97 index=2

netsh interface ip set address "以太网 2" static %IP% %mask% %gw% 0

netsh interface ip set dns name="以太网 2" source=static addr=10.46.20.96

netsh interface ip add dns name="以太网 2" addr=10.46.20.97 index=2

netsh interface ip set address "以太网 3" static %IP% %mask% %gw% 0

netsh interface ip set dns name="以太网 3" source=static addr=10.46.20.96

netsh interface ip add dns name="以太网 3" addr=10.46.20.97 index=2

netsh interface ip set address "以太网 4" static %IP% %mask% %gw% 0

netsh interface ip set dns name="以太网 4" source=static addr=10.46.20.96

netsh interface ip add dns name="以太网 4" addr=10.46.20.97 index=2

del ipc.txt

del gwc.txt
作者: wwdzcdb    时间: 2020-4-5 11:50

  1. @echo off
  2. ipconfig>ipc.txt
  3. netsh interface ip show address>gwc.txt
  4. for /f "tokens=*" %%A in ('wmic nic where "NetConnectionStatus='2'" get NetConnectionID /value^|find "="') do set %%A
  5. echo NetConnectionID="%NetConnectionID%"
  6. for /f "tokens=*" %%A in ('wmic nic where "NetConnectionID='%NetConnectionID%'" get index  /value^|find "="') do set %%A
  7. echo index="%index%"
  8. for /f "delims=: tokens=2" %%a in ('find "IPv4 地址" ipc.txt') do set IP=%%a
  9. for /f "delims=: tokens=2" %%b in ('find "子网掩码" ipc.txt') do set mask=%%b
  10. for /f "delims=: tokens=2" %%i in ('find "默认网关" gwc.txt') do set gw=%%i
  11. netsh interface ip set address "%NetConnectionID%" static %IP% %mask% %gw% 0
  12. netsh interface ip set dns name="%NetConnectionID%" source=static addr=10.46.20.96
  13. netsh interface ip add dns name="%NetConnectionID%" addr=10.46.20.97 index=2
  14. netsh interface set interface "%NetConnectionID%" disabled
  15. netsh interface set interface "%NetConnectionID%" enable
  16. del ipc.txt
  17. del gwc.txt
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2