Board logo

标题: 修改IP批处理程序 [打印本页]

作者: yexiaomeng     时间: 2008-4-29 11:31    标题: 修改IP批处理程序

本人出门在外比较多,老是修改IP是个很头疼的事,于是自己写了个批处理程序,本着共享和学习的态度拿出来和大家一起研究下。本人是菜鸟,各位高手见笑了。
以下是完整源代码

@rem 版本记录:
@rem 2008.4.27  基础版本V0.1完成  作者:叶小萌

@echo off
color 1A
Title [网络IP设置V0.1]
cls
echo.
echo 网络IP设置程序 V0.1     %date%%time%
echo.
@ipconfig /all > c:\ipconRt.txt

for /f "tokens=1,2* delims=:" %%i in ('findstr /NIC:"Description" c:\ipconRt.txt') do (
  echo 网卡描述:%%k
  goto netaddress
)
:netaddress
for /f "tokens=1,2* delims=:" %%i in ('findstr /NIC:"IP Address" c:\ipconRt.txt') do (
  echo   IP地址:%%k
  goto gateway
)
:gateway
for /f "tokens=1,2* delims=:" %%i in ('findstr /NIC:"Gateway" c:\ipconRt.txt') do (
  echo 网关地址:%%k
  goto mask
)
:mask
for /f "tokens=1,2* delims=:" %%i in ('findstr /NIC:"Mask" c:\ipconRt.txt') do (
  echo 子网掩码:%%k
  goto dns
)
:dns
for /f "tokens=1,2* delims=:" %%i in ('findstr /NIC:"DNS Servers" c:\ipconRt.txt') do (
  echo  DNS地址:%%k
  goto mac
)
:mac
for /f "tokens=1,2* delims=:" %%i in ('findstr /NIC:"Physical Address" c:\ipconRt.txt') do (
  echo 物理地址:%%k
  goto dispE
)
:dispE
echo.
del c:\ipconRt.txt

echo **********************
echo *       1)办公室     *
echo *       2)家庭       *
echo *       3)自动获取   *
echo *       4)手动设置   *
echo *   Enter)退出       *
echo **********************
echo.

:START
set   VAR=
set/p VAR=请选择网络[1,2,3,4,Enter]?:
if "%VAR%"=="1" goto office
if "%VAR%"=="2" goto home
if "%VAR%"=="3" goto auto
if "%VAR%"=="4" goto manul
if "%VAR%"==""  goto exit
goto end

:exit
echo.
rem @ipconfig
echo 保留当前配置。
goto end

ffice
echo.
echo 正在处理中,请稍候...
echo.
@netsh interface ip set address "本地连接" static 192.168.101.196 255.255.255.0 192.168.101.254 1 >NUL
@netsh interface ip set dns "本地连接" static 219.140.251.250 >NUL
goto end

:home
echo.
echo 正在处理中,请稍候...
echo.
@netsh interface ip set address "本地连接" static 218.199.24.17 255.255.255.0 218.199.24.2 1 >NUL
@netsh interface ip set dns "本地连接" static 202.112.20.131 >NUL
goto end

:auto
echo.
echo 正在处理中,请稍候...
echo.
@netsh interface ip set address "本地连接" dhcp >NUL
@netsh interface ip set dns "本地连接" dhcp >NUL
@echo 网络地址: 自动获取
goto end

:manul
echo.
set   VAR1=
set/p VAR1=请输入IP地址:
echo.
if "%VAR1%"=="" goto setGATEWAY
echo 正在处理中,请稍候...
echo.
@netsh interface ip set address "本地连接" static addr=%VAR1% mask=255.255.255.0 >NUL
@echo   IP地址: %VAR1%
@echo.

:setGATEWAY
set   VAR2=
set/p VAR2=请输入网关地址:
echo.
if "%VAR2%"=="" goto setDNS
echo 正在处理中,请稍候...
echo.
@netsh interface ip set address "本地连接" static gateway=%VAR2% gwmetric=1 >NUL
@echo   网关地址: %VAR2%
@echo.

:setDNS
set   VAR3=
set/p VAR3=请输入DNS地址:
echo.
if "%VAR3%"=="" goto START
echo 正在处理中,请稍候...
echo.
@netsh interface ip set dns "本地连接" static addr=%VAR3% register=PRIMARY >NUL
@echo   DNS地址: %VAR3%
@echo.

:end
@echo.
@ipconfig /all > c:\ipconRt.txt

:netaddress
for /f "tokens=1,2* delims=:" %%i in ('findstr /NIC:"IP Address" c:\ipconRt.txt') do (
  echo   IP地址:%%k
  goto gateway
)
:gateway
for /f "tokens=1,2* delims=:" %%i in ('findstr /NIC:"Gateway" c:\ipconRt.txt') do (
  echo 网关地址:%%k
  goto mask
)
:mask
for /f "tokens=1,2* delims=:" %%i in ('findstr /NIC:"Mask" c:\ipconRt.txt') do (
  echo 子网掩码:%%k
  goto dns
)
:dns
for /f "tokens=1,2* delims=:" %%i in ('findstr /NIC:"DNS Servers" c:\ipconRt.txt') do (
  echo  DNS地址:%%k
  goto dispE
)
:dispE
echo.
del c:\ipconRt.txt
echo 处理完成,谢谢使用!
echo.
pause
作者: abcd     时间: 2008-4-29 11:38
最好用code括起来。