批處理、VBS實現(xiàn)自動設置IP、默認網(wǎng)關(guān)、DNS、WINS、IE代理(全)
因為公司有同事負責大連、沈陽兩個城市,經(jīng)常在兩地來回走動,到每個城市后,都要自己手動更改相應的網(wǎng)絡配置,況且到外地時住的是酒店, 酒店上網(wǎng)是自動獲得IP,又要將網(wǎng)絡設置取消,真的很麻煩!于是想起寫一個批處理!來解決這個問題!主要用到的命令是netsh.
-、第一種方法是將兩地的網(wǎng)絡配置先進行本機設置,然后再導出,等用到的時候,再分別導入。
1、將現(xiàn)有的配置導出到d:\dalian.txt中:
netsh –c interface dump > d:\dalian.txt
2、將之前導出的d:\dalian.txt文件進行導入:
netsh -f d:\dalian.txt
這種方法在執(zhí)行時有點慢,不如下面的方法。
二、第二種方法
語法格式:
1、設置IP、網(wǎng)關(guān)
netsh interface ip set address name="本地連接" static 要設置的IP地址 子網(wǎng)掩碼 網(wǎng)關(guān)IP 網(wǎng)關(guān)躍數(shù)
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 取消指定網(wǎng)絡配置,請稍等….
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 開始設置大連網(wǎng)絡地址!
echo 正在設置大連IP ,請稍等……
netsh interface ip set address name="本地連接" source=static addr=10.15.100.86 mask=255.255.0.0
echo 正在設置大連網(wǎng)關(guān),請稍等……
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 開始設置沈陽網(wǎng)絡地址!
echo 正在設置沈陽IP ,請稍等……
netsh interface ip set address name="本地連接" source=static addr=10.16.100.86 mask=255.255.0.0
echo 正在設置沈陽網(wǎng)關(guān),請稍等……
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 //可以根據(jù)你的需要更改,
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地址、子網(wǎng)掩碼、網(wǎng)關(guān)、首選DNS、備用DNS、首選WINS、備用WINS
echo ------------------------------------------------------
echo 正在進行大連IP設置,請稍等
rem //可以根據(jù)你的需要更改
echo. IP地址 = %Addr%
echo. 子網(wǎng)掩碼 = %Mask%
netsh interface ip set address name=%Nic% source=static addr=%Addr% mask=%Mask% >nul
echo. 網(wǎng)關(guān) = %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 代理服務器設置已經(jīng)清空
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
五、以上配合結(jié)合,放在一個文件里,可以這樣寫:
網(wǎng)絡綜合配置.bat
@echo off
color 1f
title "網(wǎng)卡&IE代理設置批處理"
echo 實現(xiàn)功能包括切換大連和沈陽網(wǎng)絡配置,設置IE代理.
goto 51job
:51job
echo.
echo 請選擇: 1:大連,2:沈陽,3:ADSL
set /p choice=請輸入相應數(shù)字后回車:
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 "后回車跳過網(wǎng)卡設置, 直接回車繼續(xù)網(wǎng)卡設置:
if /i "%choice%" == "N" goto proxy
cls
echo 開始設置大連網(wǎng)絡地址!
echo 正在設置大連IP ,請稍等……
netsh interface ip set address name="本地連接" source=static addr=10.15.100.86 mask=255.255.0.0
echo 正在設置大連網(wǎng)關(guān),請稍等……
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 正在關(guān)閉瀏覽器:
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 "后回車跳過網(wǎng)卡設置, 直接回車繼續(xù)網(wǎng)卡設置:
if /i "%choice%" == "N" goto proxy
cls
echo 開始設置沈陽網(wǎng)絡地址!
echo 正在設置沈陽IP ,請稍等……
netsh interface ip set address name="本地連接" source=static addr=10.16.100.86 mask=255.255.0.0
echo 正在設置沈陽網(wǎng)關(guān),請稍等……
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 正在關(guān)閉瀏覽器:
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腳本來實現(xiàn)
大連網(wǎng)絡配置.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 "正在進行大連網(wǎng)絡配置"
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 "大連網(wǎng)絡配置完成!"
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"
至此所有的方法已經(jīng)向大家介紹了一遍,不管是BAT還是VBS,都可以實現(xiàn)我們想要的功能??偟膩碚f,還是根據(jù)自己的特定要求來選擇!再執(zhí)行命令時,要注意權(quán)限的問題!
相關(guān)文章
復制文件到c盤windows文件夾下的system32的批處理寫法
三個文件復制到c盤windows文件夾下的system32文件夾 批處理怎么寫?本文提供解決方法,需要的朋友可以了解下2012-12-12批處理僅允許在指定電腦computername上執(zhí)行的代碼
這篇文章主要介紹了批處理僅允許在指定電腦computername上執(zhí)行的代碼,需要的朋友可以參考下2023-07-07利用certutil.exe實現(xiàn)在批處理(bat)中嵌入exe文件的方法
這篇文章主要介紹了利用certutil.exe實現(xiàn)在批處理(bat)中嵌入exe文件的方法,需要的朋友可以參考下2016-10-10利用bat批處理程序通過DOS命令行刪除所有的空文件夾的方法
這篇文章主要介紹了利用bat批處理程序通過DOS命令行刪除所有的空文件夾的方法,感興趣的朋友跟隨腳本之家小編一起學習吧2018-05-05