最近帮朋友写的一个批处理,因为他经常在公司和家里切换IP觉得很麻烦,不过他的系统是WIN7的,而且每次在公司所切换的IP也可能会不同,所以就写了个支持WINDOWS下XP WIN7 VISTA系统的IP切换批处理,该批处理可以支持多张网卡,支持手工/自动/读取IP配置表。经测试可以正常使用,但代码可能有一些冗余,希望大家可以多多提出宝贵的建议-
- @echo off&setlocal enabledelayedexpansion
- title make by zcHacker QQ: 36868508
- color 1f
- mode con: cols=78 lines=30
- for /f "tokens=* delims= " %%f in ('ver') do echo 本机系统为:%%f
- echo 系统内核为:%os%
- echo.
- echo.☆请注意:目前该系统仅支持Windows下的XP Vista Win7三类系统。
- if /i not "%os%" == "Windows_NT" echo.&echo 当前系统非Windows_NT内核,本软件暂不支持!请按任意键退出。 &pause>nul&goto :b
- ::if /i "!zcz:~28,2!" == "XP" (goto :XP) else (echo.&echo 当前系统非Windows XP,本软件暂不支持!请按任 意键退出。&pause>nul&goto :b)
- ::win7 Microsoft Windows 7
- ::方案1: for /f "tokens=3 delims= " %%f in ('ver') do echo 本机系统为:%%f
- for /f "tokens=3 delims= " %%z in ('ver') do set zcz=%%z
- if /i "!zcz!" == "XP" (set ip=ip) else (set ip=ipv4)
- if /i "!zcz!" == "XP" (goto :xp) else (goto :v7)
- :xp
- echo.
- echo 本机网卡:
- for /f "tokens=2*" %%i in ('ipconfig^|find /i "Ethernet adapter"') do set/a bb+=1&set zc_!bb!=%%j
- for /l %%p in (1,1,!bb!) do set zcc=!zc_%%p!&echo %%p.!zcc:~0,-2!
- goto :a
- :v7
- echo.
- echo 本机网卡:
- for /f "tokens=1*" %%g in ('ipconfig^|find /i "适配器"') do set/a bbb+=1&set zccc_!bbb!=%%h
- for /l %%q in (1,1,!bbb!) do set zccz=!zccc_%%q!&echo %%q.!zccz:~0,-1!
- ::for /l %%q in (1,1,!bbb!) do set zccz=!zccc_%%q!&echo %%q.!zccz:~0,-2!
- goto :a
- :a
- echo.
- set/p y=请对所需要设置的网卡进行选择:
- if /i %y% == x goto :b
- if %y% lss 1 echo.输入错误!请重新输入,退出请按【X】&goto :a
- if %y% gtr !bb! echo.输入错误!请重新输入,退出请按【X】&goto :a
- if /i "!zcz!" == "XP" (set Q="!zc_%y%:~0,-2!") else (set Q="!zccc_%y%:~0,-1!")
- echo.当前所选择的网卡为:%Q%
-
- :d
- echo.
- echo 1.手动设置 2.自动设置
- set/p sz=请选择所需要设置的类别:
- if /i %sz% == x (goto :b) else (if %sz% geq 1 (if %sz% leq 2 if %sz% equ 1 (goto :shou) else (goto :auto) else echo.输入错误!请重新输入,退出请按【X】&goto :d) else echo.输入错误!请重新输入,退出请按 【X】&goto :d)
- echo.
-
- :shou
- cls
- echo.是否读取IP配置表?(读取请按"Y",手工输入请按"N"):
- set/p ynn=
- if /i %ynn% == y goto :py
- echo.
- echo 请输入IP地址:
- set/p aaa=
- echo 请输入子网掩码:
- set/p bbb=
- echo 请输入网关地址:
- set/p ccc=
- echo 请输入首选DNS服务器:
- set/p ddd=
- echo 请输入备用DNS服务器:
- set/p eee=
- echo.
- echo.请问是否保存该配置?(确认请按"Y",取消请按"N"):
- set/p yn=
- if /i %yn% == y (goto :save) else (goto :she)
- :py
- echo.
- echo.当前配置表如下:
- echo.
- if not exist "%~dp0"IP.txt (echo.该文件不存在,请按任意键返回。&pause>nul&goto :shou)
- type "%~dp0"\IP.txt
- echo.请输入你所需要使用的配置表名:(返回请按"R")
- set/p iip=
- ::for /f "tokens=2* delims=:" %%i in ('type "%~dp0"IP.txt') do set/a n+=1&if !n!==1 set ii=%%i
- ::for /f "tokens=2* delims=:" %%i in ('type "%~dp0"IP.txt') do set/a n+=1&set i!n!=%%i
- for /f "tokens=2* delims=:" %%i in ('type "%~dp0"IP.txt^|find /i "%iip%"') do set/a n+=1&set i!n!=%%i
- for /f "tokens=2* delims=:" %%i in ('type "%~dp0"IP.txt^|find /i "IP配置表:%iip%"') do set ipi=%%i
- ::for /l %%i in (1,1,!n!) do set !i%%1!
- if /i %iip% == R (goto :d)
- if /i %iip% == %ipi% (goto :one) else (cls&echo.操作失败。请重新输入:&goto :py)
-
- :one
- set aaa=!i2!
- set bbb=!i3!
- set ccc=!i4!
- set ddd=!i5!
- set eee=!i6!
- goto :she
- :save
- echo.
- echo.请对该配置表进行命名:
- set/p ipp=
- for /f "tokens=2* delims=:" %%i in ('type "%~dp0"IP.txt^|find /i "IP配置表:%ipp%"') do set ipi=%%i
- if /i %ipp% == %ipi% (echo.echo.该表名已存在,请重新输入:&goto :save)
- echo.IP配置表:%ipp%>>"%~dp0"\IP.txt
- echo.%ipp%的IP地址:%aaa%>>"%~dp0"\IP.txt
- echo.%ipp%的子网掩码:%bbb%>>"%~dp0"\IP.txt
- echo.%ipp%的网关地址:%ccc%>>"%~dp0"\IP.txt
- echo.%ipp%的首选DNS:%ddd%>>"%~dp0"\IP.txt
- echo.%ipp%的备用DNS:%eee%>>"%~dp0"\IP.txt
- echo. >>"%~dp0"\IP.txt
- echo.
- echo.保存成功!
-
- :she
- echo.
- echo 正在设置,请稍后。。。。。
- echo.
- netsh interface %ip% set address name=%Q% source=static addr=%aaa% mask=%bbb%
- netsh interface %ip% set address name=%Q% gateway=%ccc% gwmetric=0
- netsh interface %ip% set dns name=%Q% source=static addr=%ddd% register=PRIMARY
- netsh interface %ip% add dns name=%Q% addr=%eee%
- netsh interface %ip% set wins name=%Q% source=static addr=none
- goto :c
-
- :auto
- cls
- echo 正在设置,请稍后。。。。。
- echo.
- @echo.正在设置IP自动获取.....
- netsh interface %ip% set address name=%Q% source = dhcp>nul
- @echo.正在设置DNS自动获取.....
- netsh interface %ip% set dns %Q% source = dhcp register=PRIMARY>nul
- @echo.正在设置WINS自动获取.....
- netsh interface %ip% set wins name=%Q% source=dhcp>nul
- goto :c
-
- :b
- exit
- :c
- echo.
- echo.已经设置完成,按按任意键退出。
- pause>nul
复制代码
|