Batch processing, VBS to automatically set IP, default gateway, DNS, wins, ie proxy (all)

Time:2020-4-1

Because the company has colleagues in charge of two cities, Dalian and Shenyang. They often walk back and forth between the two cities. After arriving at each city, they have to manually change the corresponding network configuration. Moreover, when they go to other places, they live in hotels, which automatically obtain IP when they surf the Internet, and cancel the network settings. It’s really troublesome! So think of writing a batch! To solve this problem! The main command used is Netsh
-The first method is to set the network configuration of the two places first, then export it, and then import it separately when necessary.
1. Export the existing configuration to D: \ dalian.txt:
netsh –c interface dump > d:\dalian.txt

2. Import the previously exported D: \ dalian.txt file:
netsh -f d:\dalian.txt
This method is a little slow in execution, which is not as good as the following method.

2、 The second method
Syntax format:
1. Set up IP and gateway
Netsh interface IP set address name = “local area connection” static IP address to set subnet mask gateway IP gateway hop

2. Set primary DNS, wins
Netsh interface IP set DNS / wins name = “local connection” static DNS address to be set register = primary

2. Set alternate DNS, wins
Netsh interface IP add DNS / wins name = “local connection” DNS address to be set index = 2

The specific configuration is as follows:
1. Hotel.bat

Copy codeThe code is as follows:
@echo off
Echo cancel the specified network configuration, please wait .
echo.
Echo is setting to get IP address automatically. Please wait
Netsh interface IP set address name = “local connection” source = DHCP
Echo is setting up to get DNS automatically. Please wait
Netsh interface IP set DNS name = “local connection” source = DHCP
Echo setup complete!

2. Dalian.bat

Copy codeThe code is as follows:
@echo off
Echo starts to set Dalian network address!
Echo is setting up Dalian IP, please wait
Netsh interface IP set address name = “local connection” source = static addr = 10.15.100.86 mask = 255.255.0.0
Echo is setting up Dalian gateway, please wait
Netsh interface IP set address name= “local area connection” gateway=10.15.0.253 gwmetric=1
Echo is setting up Dalian master DNS, please wait
Netsh interface IP set DNS name = “local connection” source = static addr = 10.15.0.1 register = primary
Echo is setting up Dalian standby DNS, please wait
Netsh interface IP add DNS name = “local connection” addr = 10.100.1.2 index = 2
Echo is setting up Dalian main wins, please wait
Netsh interface IP set wins name = “local connection” source = static addr = 10.15.0.1
Echo is setting up Dalian backup wins, please wait
Netsh interface IP add wins name = “local connection” addr = 10.100.1.2 index = 2
Echo setup complete!

3. Shenyang.bat

Copy codeThe code is as follows:
@echo off
Echo starts to set Shenyang network address!
Echo is setting Shenyang IP, please wait
Netsh interface IP set address name = “local connection” source = static addr = 10.16.100.86 mask = 255.255.0.0
Echo is setting up Shenyang gateway, please wait
Netsh interface IP set address name = “local connection” gateway = 10.16.0.253 gwmetric = 1
Echo is setting Shenyang master DNS, please wait
Netsh interface IP set DNS name = “local connection” source = static addr = 10.16.0.1 register = primary
Echo is setting Shenyang standby DNS, please wait
Netsh interface IP add DNS name = “local connection” addr = 10.100.1.2 index = 2
Echo is setting up Shenyang main wins. Please wait
Netsh interface IP set wins name = “local connection” source = static addr = 10.16.0.1
Echo is setting up Shenyang backup wins, please wait
Netsh interface IP add wins name = “local connection” addr = 10.100.1.2 index = 2
Echo setup complete!

Now the second method is finished!

3、 You can also use variables in batch processing! For example, Dalian. Bat can be written as follows:

Copy codeThe code is as follows:
@ echo off
REM set variable
Set NIC = local connection
REM / / can be changed according to your needs,
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 address, subnet mask, gateway, preferred DNS, standby DNS, preferred wins, standby wins
echo ——————————————————
Echo is setting up IP in Dalian, please wait a moment
REM / / can be changed according to your needs
Echo. IP address =% addr%
Echo. Subnet mask =% mask%
netsh interface ip set address name=%Nic% source=static addr=%Addr% mask=%Mask% >nul
Echo. Gateway =% gway%
netsh interface ip set address name=%Nic% gateway=%Gway% gwmetric=1 >nul
Echo. Preferred DNS =% dns1%
netsh interface ip set dns name=%Nic% source=static addr=%Dns1% register=PRIMARY >nul
Echo. Alternate DNS =% dns2%
netsh interface ip add dns name=%Nic% addr=%Dns2% index=2 >nul
Echo. Preferred wins =% wins1%
netsh interface ip set wins name=%Nic% source=static addr=%Wins1% register=PRIMARY >nul
Echo. Alternate wins =% wins2%
netsh interface ip add wins name=%Nic% addr=%Wins2% index=2 >nul
echo ——————————————————
Echo IP setting completed!

According to personal habits to adopt their own methods.
4、 Set ie proxy automatically
Dalian ie agent.bat

Copy codeThe code is as follows:
@echo off
Title automatically set proxy server
Echo automatically set up the proxy server
REM echo clearing proxy settings
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 settings cleared
Echo setting up 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

The same setting in Shenyang.
Or use the following method:

Copy codeThe code is as follows:
cls
color 1f
@Echo clear agent settings
@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 set proxy
@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

5、 In combination with the above, put it in a file and write as follows:
Network integrated configuration.bat

Copy codeThe code is as follows:
@echo off
color 1f
Title “batch processing of network card & ie agent settings”
The functions of echo include switching network configuration between Dalian and Shenyang and setting ie agent
goto 51job
:51job
echo.
Echo: 1: Dalian, 2: Shenyang, 3: ADSL
Set / P choice = please enter the corresponding number and enter:
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 = “local connection” source = DHCP
Netsh interface IP set DNS name = “local connection” 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 = enter “n” to skip network card settings, and enter directly to continue network card settings:
if /i “%choice%” == “N” goto proxy
cls
Echo starts to set Dalian network address!
Echo is setting up Dalian IP, please wait
Netsh interface IP set address name = “local connection” source = static addr = 10.15.100.86 mask = 255.255.0.0
Echo is setting up Dalian gateway, please wait
Netsh interface IP set address name= “local area connection” gateway=10.15.0.253 gwmetric=1
Echo is setting up Dalian master DNS, please wait
Netsh interface IP set DNS name = “local connection” source = static addr = 10.15.0.1 register = primary
Echo is setting up Dalian standby DNS, please wait
Netsh interface IP add DNS name = “local connection” addr = 10.100.1.2 index = 2
Echo is setting up Dalian main wins, please wait
Netsh interface IP set wins name = “local connection” source = static addr = 10.15.0.1
Echo is setting up Dalian backup wins, please wait
Netsh interface IP add wins name = “local connection” addr = 10.100.1.2 index = 2
Echo setup complete!
Echo refreshing settings
ipconfig /flushdns
Echo displays the new settings:
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 closing browser:
taskkill /f /t /im IEXPLORE.exe
Echo opening browser
“C:\Program Files\Internet Explorer\IEXPLORE.EXE”
goto exit
:synet
cls
color 1f
echo.
Set / P choice = enter “n” to skip network card settings, and enter directly to continue network card settings:
if /i “%choice%” == “N” goto proxy
cls
Echo starts to set Shenyang network address!
Echo is setting Shenyang IP, please wait
Netsh interface IP set address name = “local connection” source = static addr = 10.16.100.86 mask = 255.255.0.0
Echo is setting up Shenyang gateway, please wait
Netsh interface IP set address name = “local connection” gateway = 10.16.0.253 gwmetric = 1
Echo is setting Shenyang master DNS, please wait
Netsh interface IP set DNS name = “local connection” source = static addr = 10.16.0.1 register = primary
Echo is setting Shenyang standby DNS, please wait
Netsh interface IP add DNS name = “local connection” addr = 10.100.1.2 index = 2
Echo is setting up Shenyang main wins. Please wait
Netsh interface IP set wins name = “local connection” source = static addr = 10.16.0.1
Echo is setting up Shenyang backup wins, please wait
Netsh interface IP add wins name = “local connection” addr = 10.100.1.2 index = 2
Echo setup complete!
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 closing browser:
taskkill /f /t /im IEXPLORE.exe
Echo opening browser
“C:\Program Files\Internet Explorer\IEXPLORE.EXE”
goto exit
:exit
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
Echo has completed all setup
echo.
echo
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
pause
exit

In this way, you don’t need to create multiple batch files and do multiple things with one file. Why not!
6、 Finally, how to use vbs script to realize
Dalian network configuration.vbs

Copy codeThe code is as follows:
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 “Dalian network configuration in progress”
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 “Dalian network configuration completed! “

Ie agent configuration.vbs

Copy codeThe code is as follows:
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 (“set agent to” & 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”

So far, all the methods have been introduced to you, whether bat or VBS, can achieve the functions we want. Generally speaking, we should choose according to our specific requirements! When executing the command again, pay attention to the problem of authority!