批處理、VBS實現自動化佈建IP、預設閘道、DNS、WINS、IE代理(全)_DOS/BAT

來源:互聯網
上載者:User

因為公司有同事負責大連、瀋陽兩個城市,經常在兩地來回走動,到每個城市後,都要自己手動更改相應的網路設定,況且到外地時住的是酒店, 酒店上網是自動獲得IP,又要將網路設定取消,真的很麻煩!於是想起寫一個批處理!來解決這個問題!主要用到的命令是netsh.
-、第一種方法是將兩地的網路設定先進行本機設定,然後再匯出,等用到的時候,再分別匯入。
1、將現有的設定匯出到d:\dalian.txt中:
netsh –c interface dump > d:\dalian.txt

2、將之前置出的d:\dalian.txt檔案進行匯入:
netsh -f d:\dalian.txt
這種方法在執行時有點慢,不如下面的方法。

二、第二種方法
文法格式:
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 自動化佈建Proxy 伺服器
echo 自動化佈建Proxy 伺服器
rem echo 正在清空Proxy 伺服器設定……
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 Proxy 伺服器設定已經清空
echo 正在設定Proxy 伺服器……
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,都可以實現我們想要的功能。總的來說,還是根據自己的特定要求來選擇!再執行命令時,要注意許可權的問題!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.