批处理、VBS实现自动设置IP、默认网关、DNS、WINS、IE代理的代码

语法格式:
1、设置IP、网关
netsh interface ip set address name="本地连接" static 要设置的IP地址 子网掩码 网关IP 网关跃数

2、设置主DNS、WINS
netsh interface ip set dns/wins name="本地连接" static 要设置的DNS地址 register=PRIMARY

2、设置备用DNS、WINS
netsh interface ip add dns/wins name="本地连接" 要设置的DNS地址 index=2

具体配置如下:
1、酒店.bat


代码如下:

@echo off
echo 取消指定网络配置,请稍等….
echo.
echo 正在设置自动获取IP地址,请稍等……
netsh interface ip set address name="本地连接" source=dhcp
echo 正在设置自动获取DNS,请稍等……
netsh interface ip set dns name="本地连接" source=dhcp
echo 设置完成! 
 


2、大连.bat
代码如下:

@echo off
echo 开始设置大连网络地址!
echo 正在设置大连IP ,请稍等……
netsh interface ip set address name="本地连接" source=static addr=10.15.100.86 mask=255.255.0.0
echo 正在设置大连网关,请稍等……
netsh interface ip set address name="本地连接" gateway=10.15.0.253 gwmetric=1
echo 正在设置大连主DNS ,请稍等……
netsh interface ip set dns name="本地连接" source=static addr=10.15.0.1 register=PRIMARY
echo 正在设置大连备用DNS ,请稍等……
netsh interface ip add dns name="本地连接" addr=10.100.1.2 index=2
echo 正在设置大连主WINS ,请稍等……
netsh interface ip set wins name="本地连接" source=static addr=10.15.0.1
echo 正在设置大连备用WINS ,请稍等……
netsh interface ip add wins name="本地连接" addr=10.100.1.2 index=2
echo 设置完成! 

3、沈阳.bat
代码如下:

@echo off
echo 开始设置沈阳网络地址!
echo 正在设置沈阳IP ,请稍等……
netsh interface ip set address name="本地连接" source=static addr=10.16.100.86 mask=255.255.0.0
echo 正在设置沈阳网关,请稍等……
netsh interface ip set address name="本地连接" gateway=10.16.0.253 gwmetric=1
echo 正在设置沈阳主DNS ,请稍等……
netsh interface ip set dns name="本地连接" source=static addr=10.16.0.1 register=PRIMARY
echo 正在设置沈阳备用DNS ,请稍等……
netsh interface ip add dns name="本地连接" addr=10.100.1.2 index=2
echo 正在设置沈阳主WINS ,请稍等……
netsh interface ip set wins name="本地连接" source=static addr=10.16.0.1
echo 正在设置沈阳备用WINS ,请稍等……
netsh interface ip add wins name="本地连接" addr=10.100.1.2 index=2
echo 设置完成! 

 至此第二种方法完成!

三、也可以在批处理中使用变量!例如大连.BAT可以按照如下方法写:
代码如下:

@ echo off
rem 设置变量
set Nic=本地连接
rem //可以根据你的需要更改,
set Addr=10.15.100.86
set Mask=255.255.0.0
set Gway=10.15.0.253
set Dns1=10.15.0.1
set Dns2=10.100.1.2
set Wins1=10.15.0.1
set Wins2=10.100.1.2
rem //以上依次为IP地址、子网掩码、网关、首选DNS、备用DNS、首选WINS、备用WINS
echo ------------------------------------------------------
echo 正在进行大连IP设置,请稍等
rem //可以根据你的需要更改
echo. IP地址 = %Addr%
echo. 子网掩码 = %Mask%
netsh interface ip set address name=%Nic% source=static addr=%Addr% mask=%Mask% >nul
echo. 网关 = %Gway%
netsh interface ip set address name=%Nic% gateway=%Gway% gwmetric=1 >nul
echo. 首选DNS = %Dns1%
netsh interface ip set dns name=%Nic% source=static addr=%Dns1% register=PRIMARY >nul
echo. 备用DNS = %Dns2%
netsh interface ip add dns name=%Nic% addr=%Dns2% index=2 >nul
echo. 首选WINS = %Wins1%
netsh interface ip set wins name=%Nic% source=static addr=%Wins1% register=PRIMARY >nul
echo. 备用WINS = %Wins2%
netsh interface ip add wins name=%Nic% addr=%Wins2% index=2 >nul
echo ------------------------------------------------------
echo IP设置完成! 

依个人习惯采用适合自己的方法。
四、自动设置IE代理
大连IE代理.bat
代码如下:

@echo off
title 自动设置代理服务器
echo 自动设置代理服务器
rem echo 正在清空代理服务器设置……
rem reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f
rem reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /d "" /f
rem reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyOverride /t REG_SZ /d 0 /f
rem echo 代理服务器设置已经清空
echo 正在设置代理服务器……
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /d "10.15.0.2:3128" /f
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyOverride /t REG_SZ /d "10.*.*.*;192.168.*.*;<local>" /f 

沈阳的一样设置即可。

或者用下面的方法:
代码如下:

cls
color 1f
@echo 清空代理设置
@echo Windows Registry Editor Version 5.00>>1.reg
@echo [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]>>1.reg
@echo "ProxyEnable"=dword:00000000>>1.reg
@echo "ProxyServer"="">>1.reg
@echo "ProxyOverride"="">>1.reg
regedit /s 1.reg
del 1.reg
@echo 设置代理
@echo Windows Registry Editor Version 5.00>>1.reg
@echo [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]>>1.reg
@echo "ProxyEnable"=dword:00000001>>1.reg
@echo "ProxyServer"="10.15.0.2:8080">>1.reg
@echo "ProxyOverride"="10.*.*.*;192.168.*.*;<local>">>1.reg
regedit /s 1.reg
del 1.reg 
 

五、以上配合结合,放在一个文件里,可以这样写:
网络综合配置.bat
复制代码 代码如下:

@echo off
color 1f
title "网卡&IE代理设置批处理"
echo 实现功能包括切换大连和沈阳网络配置,设置IE代理.
goto 51job
:51job
echo.
echo 请选择: 1:大连,2:沈阳,3:ADSL
set /p choice=请输入相应数字后回车:
if /i "%choice%" == "1" goto dlnet
if /i "%choice%" == "2" goto synet
if /i "%choice%" == "3" goto adsl
goto 51job
:adsl
cls
color 1f
netsh interface ip set address name="本地连接" source=dhcp
netsh interface ip set dns name="本地连接" source=dhcp
cls
goto noproxy
:noproxy
@echo Windows Registry Editor Version 5.00>>1.reg
@echo [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]>>1.reg
@echo "ProxyEnable"=dword:00000000>>1.reg
@echo "ProxyServer"="">>1.reg
@echo "ProxyOverride"="">>1.reg
regedit /s 1.reg
del 1.reg
goto exit
:dlnet
cls
color 1f
echo.
set /p choice=输入" N "后回车跳过网卡设置, 直接回车继续网卡设置:
if /i "%choice%" == "N" goto proxy
cls
echo 开始设置大连网络地址!
echo 正在设置大连IP ,请稍等……
netsh interface ip set address name="本地连接" source=static addr=10.15.100.86 mask=255.255.0.0
echo 正在设置大连网关,请稍等……
netsh interface ip set address name="本地连接" gateway=10.15.0.253 gwmetric=1
echo 正在设置大连主DNS ,请稍等……
netsh interface ip set dns name="本地连接" source=static addr=10.15.0.1 register=PRIMARY
echo 正在设置大连备用DNS ,请稍等……
netsh interface ip add dns name="本地连接" addr=10.100.1.2 index=2
echo 正在设置大连主WINS ,请稍等……
netsh interface ip set wins name="本地连接" source=static addr=10.15.0.1
echo 正在设置大连备用WINS ,请稍等……
netsh interface ip add wins name="本地连接" addr=10.100.1.2 index=2
echo 设置完成!
echo 正在刷新设置……
ipconfig /flushdns
echo 显示新的设置:
ipconfig /all
goto dlproxy
:dlproxy
cls
color 1f
@echo Windows Registry Editor Version 5.00>>1.reg
@echo [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]>>1.reg
@echo "ProxyEnable"=dword:00000001>>1.reg
@echo "ProxyServer"="10.15.0.2:8080">>1.reg
@echo "ProxyOverride"="10.*.*.*;192.168.*.*;<local>">>1.reg
regedit /s 1.reg
del 1.reg 
echo 正在关闭浏览器: 
taskkill /f /t /im IEXPLORE.exe 

echo 正在开启浏览器 
"C:\Program Files\Internet Explorer\IEXPLORE.EXE" 
goto 
exit 
:synet 
cls 
color 1f 
echo. 
set /p choice=输入" N 
"后回车跳过网卡设置, 直接回车继续网卡设置: 
if /i "%choice%" == "N" goto proxy 
cls 
echo 
开始设置沈阳网络地址! 
echo 正在设置沈阳IP ,请稍等…… 
netsh interface ip set address 
name="本地连接" source=static addr=10.16.100.86 mask=255.255.0.0 
echo 
正在设置沈阳网关,请稍等…… 
netsh interface ip set address name="本地连接" 
gateway=10.16.0.253 gwmetric=1 
echo 正在设置沈阳主DNS ,请稍等…… 
netsh interface ip 
set dns name="本地连接" source=static addr=10.16.0.1 register=PRIMARY 
echo 
正在设置沈阳备用DNS ,请稍等…… 
netsh interface ip add dns name="本地连接" addr=10.100.1.2 
index=2 
echo 正在设置沈阳主WINS ,请稍等…… 
netsh interface ip set wins name="本地连接" 
source=static addr=10.16.0.1 
echo 正在设置沈阳备用WINS ,请稍等…… 
netsh interface ip 
add wins name="本地连接" addr=10.100.1.2 index=2 
echo 设置完成! 
goto syproxy 

:syproxy 
cls 
color 1f 
@echo Windows Registry Editor Version 
5.00>>1.reg 
@echo 
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet 
Settings]>>1.reg 
@echo "ProxyEnable"=dword:00000001>>1.reg 

@echo "ProxyServer"="10.16.0.2:8080">>1.reg 
@echo 
"ProxyOverride"="10.*.*.*;192.168.*.*;<local>">>1.reg 
regedit /s 
1.reg 
del 1.reg 
echo 正在关闭浏览器: 
taskkill /f /t /im IEXPLORE.exe 

echo 正在开启浏览器 
"C:\Program Files\Internet Explorer\IEXPLORE.EXE" 
goto 
exit 
:exit 
cls 
echo. 
echo. 
echo. 
echo. 
echo. 

echo. 
echo. 
echo. 
echo 已完成所有设置. 
echo. 
echo 
echo. 

echo. 
echo. 
echo. 
echo. 
echo. 
echo. 
echo. 
echo. 

pause 
exit 

用这种方法就不用建立多个批处理文件,用一个文件做多件事,何乐而不为呢!
六、最后介绍一下如何使用VBS脚本来实现
大连网络配置.vbs
复制代码 代码如下:

on error resume next
strIPAddress = array("10.15.100.86")
strSubnetMask = array("255.255.0.0")
strGateway = array("10.15.0.253")
strGatewayMetric = array("1")
strwinsOne = "10.15.0.1"
strwinsTwo = "10.100.1.2"
strdnsOne = "10.15.0.1"
strdnsTwo = "10.100.1.2"
strComputer = "."
Set objShell = CreateObject("Wscript.shell")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNetCards = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
wscript.echo "正在进行大连网络配置"
For Each objNetCard in colNetCards
errEnable = objNetCard.EnableStatic(strIPAddress,strSubnetMask)
errGateways = objNetCard.SetGateways(strGateway,strGatewayMetric)
arrDNSServers = Array(strdnsone, strdnstwo)
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
SetWins = objNetCard.SetWINSServer(strwinsOne,strwinsTwo)
Next
wscript.echo "大连网络配置完成!"

IE代理配置.vbs
代码如下:
strMachines = "10.15.0.2:3128;10.16.0.2:3128"
aMachines = split(strMachines, ";")
For Each machine2 in aMachines
machinearr = split(machine2, ":")
machine = machinearr(0)
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& machine & "'")
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
WScript.Echo(machine2 & " is not reachable")
else
WScript.Echo(machine2 & " is OK")
if confirm("设置代理为"& machine2 &"?") then
msgbox SetIEProxy(1,machine2)
end if
End If
Next
Next
function confirm(s)
confirm = (msgbox(s,vbYesNo,s) = 6)
end function
Function SetIEProxy(ProxyEnable,ProxyIP)
On Error Resume Next
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objReg = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}\\" & strComputer & _
"\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\"
strEntryName = "ProxyEnable"
dwvalue = ProxyEnable
objReg.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strEntryName,dwValue
strEntryName = "ProxyServer"
dwvalue = ProxyIP
objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, strEntryName,dwValue
strEntryName = "ProxyOverride"
dwvalue = "10.*.*.*;192.168.*.*;<local>"
objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, strEntryName,dwValue
If Err = 0 Then
SetIEProxy = True
Else
SetIEProxy = False
End If
End Function
msgbox "ok" 
 

至此所有的方法已经向大家介绍了一遍,不管是BAT还是VBS,都可以实现我们想要的功能。总的来说,还是根据自己的特定要求来选择!再执行命令时,要注意权限的问题!
详细出处参考:http://www.jb51.net/article/30072.htm

猜你喜欢

转载自xiaolincc26201206092327.iteye.com/blog/1696864