批處理、VBS實(shí)現(xiàn)自動(dòng)設(shè)置IP、默認(rèn)網(wǎng)關(guān)、DNS、WINS、IE代理(全)
因?yàn)楣居型仑?fù)責(zé)大連、沈陽(yáng)兩個(gè)城市,經(jīng)常在兩地來回走動(dòng),到每個(gè)城市后,都要自己手動(dòng)更改相應(yīng)的網(wǎng)絡(luò)配置,況且到外地時(shí)住的是酒店, 酒店上網(wǎng)是自動(dòng)獲得IP,又要將網(wǎng)絡(luò)設(shè)置取消,真的很麻煩!于是想起寫一個(gè)批處理!來解決這個(gè)問題!主要用到的命令是netsh.
-、第一種方法是將兩地的網(wǎng)絡(luò)配置先進(jìn)行本機(jī)設(shè)置,然后再導(dǎo)出,等用到的時(shí)候,再分別導(dǎo)入。
1、將現(xiàn)有的配置導(dǎo)出到d:\dalian.txt中:
netsh –c interface dump > d:\dalian.txt
2、將之前導(dǎo)出的d:\dalian.txt文件進(jìn)行導(dǎo)入:
netsh -f d:\dalian.txt
這種方法在執(zhí)行時(shí)有點(diǎn)慢,不如下面的方法。
二、第二種方法
語(yǔ)法格式:
1、設(shè)置IP、網(wǎng)關(guān)
netsh interface ip set address name="本地連接" static 要設(shè)置的IP地址 子網(wǎng)掩碼 網(wǎng)關(guān)IP 網(wǎng)關(guān)躍數(shù)
2、設(shè)置主DNS、WINS
netsh interface ip set dns/wins name="本地連接" static 要設(shè)置的DNS地址 register=PRIMARY
2、設(shè)置備用DNS、WINS
netsh interface ip add dns/wins name="本地連接" 要設(shè)置的DNS地址 index=2
具體配置如下:
1、酒店.bat
@echo off
echo 取消指定網(wǎng)絡(luò)配置,請(qǐng)稍等….
echo.
echo 正在設(shè)置自動(dòng)獲取IP地址,請(qǐng)稍等……
netsh interface ip set address name="本地連接" source=dhcp
echo 正在設(shè)置自動(dòng)獲取DNS,請(qǐng)稍等……
netsh interface ip set dns name="本地連接" source=dhcp
echo 設(shè)置完成!
2、大連.bat
@echo off
echo 開始設(shè)置大連網(wǎng)絡(luò)地址!
echo 正在設(shè)置大連IP ,請(qǐng)稍等……
netsh interface ip set address name="本地連接" source=static addr=10.15.100.86 mask=255.255.0.0
echo 正在設(shè)置大連網(wǎng)關(guān),請(qǐng)稍等……
netsh interface ip set address name="本地連接" gateway=10.15.0.253 gwmetric=1
echo 正在設(shè)置大連主DNS ,請(qǐng)稍等……
netsh interface ip set dns name="本地連接" source=static addr=10.15.0.1 register=PRIMARY
echo 正在設(shè)置大連備用DNS ,請(qǐng)稍等……
netsh interface ip add dns name="本地連接" addr=10.100.1.2 index=2
echo 正在設(shè)置大連主WINS ,請(qǐng)稍等……
netsh interface ip set wins name="本地連接" source=static addr=10.15.0.1
echo 正在設(shè)置大連備用WINS ,請(qǐng)稍等……
netsh interface ip add wins name="本地連接" addr=10.100.1.2 index=2
echo 設(shè)置完成!
3、沈陽(yáng).bat
@echo off
echo 開始設(shè)置沈陽(yáng)網(wǎng)絡(luò)地址!
echo 正在設(shè)置沈陽(yáng)IP ,請(qǐng)稍等……
netsh interface ip set address name="本地連接" source=static addr=10.16.100.86 mask=255.255.0.0
echo 正在設(shè)置沈陽(yáng)網(wǎng)關(guān),請(qǐng)稍等……
netsh interface ip set address name="本地連接" gateway=10.16.0.253 gwmetric=1
echo 正在設(shè)置沈陽(yáng)主DNS ,請(qǐng)稍等……
netsh interface ip set dns name="本地連接" source=static addr=10.16.0.1 register=PRIMARY
echo 正在設(shè)置沈陽(yáng)備用DNS ,請(qǐng)稍等……
netsh interface ip add dns name="本地連接" addr=10.100.1.2 index=2
echo 正在設(shè)置沈陽(yáng)主WINS ,請(qǐng)稍等……
netsh interface ip set wins name="本地連接" source=static addr=10.16.0.1
echo 正在設(shè)置沈陽(yáng)備用WINS ,請(qǐng)稍等……
netsh interface ip add wins name="本地連接" addr=10.100.1.2 index=2
echo 設(shè)置完成!
至此第二種方法完成!
三、也可以在批處理中使用變量!例如大連.BAT可以按照如下方法寫:
@ echo off
rem 設(shè)置變量
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 正在進(jìn)行大連IP設(shè)置,請(qǐng)稍等
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設(shè)置完成!
依個(gè)人習(xí)慣采用適合自己的方法。
四、自動(dòng)設(shè)置IE代理
大連IE代理.bat
@echo off
title 自動(dòng)設(shè)置代理服務(wù)器
echo 自動(dòng)設(shè)置代理服務(wù)器
rem echo 正在清空代理服務(wù)器設(shè)置……
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 代理服務(wù)器設(shè)置已經(jīng)清空
echo 正在設(shè)置代理服務(wù)器……
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
沈陽(yáng)的一樣設(shè)置即可。
或者用下面的方法:
cls
color 1f
@echo 清空代理設(shè)置
@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 設(shè)置代理
@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é)合,放在一個(gè)文件里,可以這樣寫:
網(wǎng)絡(luò)綜合配置.bat
@echo off
color 1f
title "網(wǎng)卡&IE代理設(shè)置批處理"
echo 實(shí)現(xiàn)功能包括切換大連和沈陽(yáng)網(wǎng)絡(luò)配置,設(shè)置IE代理.
goto 51job
:51job
echo.
echo 請(qǐng)選擇: 1:大連,2:沈陽(yáng),3:ADSL
set /p choice=請(qǐng)輸入相應(yīng)數(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)卡設(shè)置, 直接回車?yán)^續(xù)網(wǎng)卡設(shè)置:
if /i "%choice%" == "N" goto proxy
cls
echo 開始設(shè)置大連網(wǎng)絡(luò)地址!
echo 正在設(shè)置大連IP ,請(qǐng)稍等……
netsh interface ip set address name="本地連接" source=static addr=10.15.100.86 mask=255.255.0.0
echo 正在設(shè)置大連網(wǎng)關(guān),請(qǐng)稍等……
netsh interface ip set address name="本地連接" gateway=10.15.0.253 gwmetric=1
echo 正在設(shè)置大連主DNS ,請(qǐng)稍等……
netsh interface ip set dns name="本地連接" source=static addr=10.15.0.1 register=PRIMARY
echo 正在設(shè)置大連備用DNS ,請(qǐng)稍等……
netsh interface ip add dns name="本地連接" addr=10.100.1.2 index=2
echo 正在設(shè)置大連主WINS ,請(qǐng)稍等……
netsh interface ip set wins name="本地連接" source=static addr=10.15.0.1
echo 正在設(shè)置大連備用WINS ,請(qǐng)稍等……
netsh interface ip add wins name="本地連接" addr=10.100.1.2 index=2
echo 設(shè)置完成!
echo 正在刷新設(shè)置……
ipconfig /flushdns
echo 顯示新的設(shè)置:
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)卡設(shè)置, 直接回車?yán)^續(xù)網(wǎng)卡設(shè)置:
if /i "%choice%" == "N" goto proxy
cls
echo 開始設(shè)置沈陽(yáng)網(wǎng)絡(luò)地址!
echo 正在設(shè)置沈陽(yáng)IP ,請(qǐng)稍等……
netsh interface ip set address name="本地連接" source=static addr=10.16.100.86 mask=255.255.0.0
echo 正在設(shè)置沈陽(yáng)網(wǎng)關(guān),請(qǐng)稍等……
netsh interface ip set address name="本地連接" gateway=10.16.0.253 gwmetric=1
echo 正在設(shè)置沈陽(yáng)主DNS ,請(qǐng)稍等……
netsh interface ip set dns name="本地連接" source=static addr=10.16.0.1 register=PRIMARY
echo 正在設(shè)置沈陽(yáng)備用DNS ,請(qǐng)稍等……
netsh interface ip add dns name="本地連接" addr=10.100.1.2 index=2
echo 正在設(shè)置沈陽(yáng)主WINS ,請(qǐng)稍等……
netsh interface ip set wins name="本地連接" source=static addr=10.16.0.1
echo 正在設(shè)置沈陽(yáng)備用WINS ,請(qǐng)稍等……
netsh interface ip add wins name="本地連接" addr=10.100.1.2 index=2
echo 設(shè)置完成!
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 已完成所有設(shè)置.
echo.
echo
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
pause
exit
用這種方法就不用建立多個(gè)批處理文件,用一個(gè)文件做多件事,何樂而不為呢!
六、最后介紹一下如何使用VBS腳本來實(shí)現(xiàn)
大連網(wǎng)絡(luò)配置.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 "正在進(jìn)行大連網(wǎng)絡(luò)配置"
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)絡(luò)配置完成!"
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("設(shè)置代理為"& 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,都可以實(shí)現(xiàn)我們想要的功能??偟膩碚f,還是根據(jù)自己的特定要求來選擇!再執(zhí)行命令時(shí),要注意權(quán)限的問題!
相關(guān)文章
復(fù)制文件到c盤windows文件夾下的system32的批處理寫法
三個(gè)文件復(fù)制到c盤windows文件夾下的system32文件夾 批處理怎么寫?本文提供解決方法,需要的朋友可以了解下2012-12-12Assoc顯示或修改文件名擴(kuò)展關(guān)聯(lián)
Assoc顯示或修改文件名擴(kuò)展關(guān)聯(lián)...2007-09-09超級(jí)批處理病毒,不錯(cuò)的學(xué)習(xí)資料
超級(jí)批處理病毒,不錯(cuò)的學(xué)習(xí)資料...2007-05-05批處理僅允許在指定電腦computername上執(zhí)行的代碼
這篇文章主要介紹了批處理僅允許在指定電腦computername上執(zhí)行的代碼,需要的朋友可以參考下2023-07-07利用certutil.exe實(shí)現(xiàn)在批處理(bat)中嵌入exe文件的方法
這篇文章主要介紹了利用certutil.exe實(shí)現(xiàn)在批處理(bat)中嵌入exe文件的方法,需要的朋友可以參考下2016-10-10一個(gè)“靈異”批處理引發(fā)的思考加補(bǔ)充說明
這個(gè)語(yǔ)句讓偶理解了好半天: 我們知道批處理在運(yùn)行過程中,在讀取每條語(yǔ)句/執(zhí)行每個(gè)命令都會(huì)擴(kuò)充一次語(yǔ)句/命令里的變量。 我們來看看這個(gè)例子發(fā)生了什么事情:2008-05-05利用bat批處理程序通過DOS命令行刪除所有的空文件夾的方法
這篇文章主要介紹了利用bat批處理程序通過DOS命令行刪除所有的空文件夾的方法,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05