Batch processing, VBS implementation automatically set IP, default gateway, DNS, WINS, ie agent (full) _dos/bat

Source: Internet
Author: User
Tags dns2 get ip goto

Because the company has colleagues in charge of Dalian, Shenyang Two cities, often in both directions back and forth, to each city, they have to manually change the corresponding network configuration, and to live in the field is the hotel, the hotel is automatically access to the Internet IP, but also the network settings canceled, really troublesome! So I thought of writing a batch process! To solve this problem! The main command to use is netsh.
-The first method is to set up the network configuration of the two places first, and then export, and so on, and then import separately.
1. Export 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 approach is a bit slow to execute, as is the case with the following method.

Two, the second method
Syntax format:
1, set IP, gateway
netsh interface ip set address name= "Local connection" static IP address subnet Mask gateway IP Gateway hop number to set

2, set the primary DNS, WINS
netsh interface ip set dns/wins name= "Local connection" static the DNS address to set Register=primary

2. Set up alternate DNS, WINS
netsh interface ip add dns/wins name= DNS address to be set for "Local Area Connection" index=2

The specific configuration is as follows:
1, hotel. bat

Copy Code code as follows:

@echo off
echo cancels specifying network configuration, please wait ....
Echo.
Echo is setting up an automatic Get IP address, please wait a moment ...
netsh interface ip set address name= "Local Area Connection" Source=dhcp
ECHO is setting up automatic DNS access, please wait ...
netsh interface ip set DNS name= "Local Area Connection" Source=dhcp
echo Setup Complete!


2, Dalian. bat
Copy Code code as follows:

@echo off
Echo starts setting up the Dalian network address!
Echo is setting up Dalian IP, please wait a moment ...
netsh interface ip set address name= "Local Area Connection" Source=static addr=10.15.100.86 mask=255.255.0.0
Echo is setting up the Dalian Gateway, please wait a moment ...
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 a moment ...
netsh interface ip set DNS name= "Local Area Connection" Source=static addr=10.15.0.1 register=primary
Echo is setting up Dalian standby DNS, please wait a moment ...
netsh interface ip add DNS name= "Local Area Connection" addr=10.100.1.2 index=2
Echo is setting up Dalian main wins, please wait a moment ...
netsh interface ip set wins name= "Local Area Connection" Source=static addr=10.15.0.1
Echo is setting up a standby wins in Dalian, please wait a moment ...
netsh interface ip add wins name= "Local Area Connection" addr=10.100.1.2 index=2
echo Setup Complete!


3, Shenyang. bat
Copy Code code as follows:

@echo off
Echo starts setting up Shenyang network address!
Echo is setting up Shenyang IP, please wait a moment ...
netsh interface ip set address name= "Local Area Connection" Source=static addr=10.16.100.86 mask=255.255.0.0
Echo is setting up Shenyang Gateway, please wait a moment ...
netsh interface ip set address name= "Local Area Connection" gateway=10.16.0.253 gwmetric=1
Echo is setting up Shenyang Master DNS, please wait a moment ...
netsh interface ip set DNS name= "Local Area Connection" Source=static addr=10.16.0.1 register=primary
Echo is setting up Shenyang standby DNS, please wait a moment ...
netsh interface ip add DNS name= "Local Area Connection" addr=10.100.1.2 index=2
Echo is setting up Shenyang Master wins, please wait a moment ...
netsh interface ip set wins name= "Local Area Connection" Source=static addr=10.16.0.1
Echo is setting up Shenyang standby wins, please wait a moment ...
netsh interface ip add wins name= "Local Area Connection" addr=10.100.1.2 index=2
echo Setup Complete!

The second method is complete!

Third, you can also use variables in batch processing! such as Dalian. BAT can be written in the following way:
Copy Code code as follows:

@ echo off
REM Setting 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//above are IP addresses, subnet masks, gateways, preferred DNS, alternate DNS, preferred wins, alternate wins
Echo------------------------------------------------------
ECHO is in Dalian IP settings, please wait
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 Settings complete!


Use your own methods to suit your personal habits.
Four, automatically set IE agent
Dalian IE Agent. bat
Copy Code code as follows:

@echo off
Title automatically set proxy server
echo automatically sets proxy server
REM echo is emptying proxy server 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 Server Setup is empty
Echo is setting up the proxy server ...
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

Shenyang, the same settings can be.
Or use the following method:
Copy Code code as follows:

Cls
Color 1f
@echo Empty 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 up Agents
@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

Five, together with the above, put in a file, you can write:
Integrated network configuration. bat
Copy Code code as follows:

@echo off
Color 1f
Title "Network Card &ie agent Settings Batch"
Echo implementation features include switching between the Dalian and Shenyang network configurations and setting up IE proxies.
Goto 51job
: 51job
Echo.
echo Please choose: 1: Dalian, 2: Shenyang, 3:ADSL
set/p choice= Please enter the appropriate number to return to:
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 Area Connection" Source=dhcp
netsh interface ip set DNS name= "Local Area 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
:d lnet
Cls
Color 1f
Echo.
set/p choice= Enter "N" after the carriage return skips the NIC setting, direct return to continue the network card setting:
if/i "%choice%" = = "N" goto Proxy
Cls
Echo starts setting up the Dalian network address!
Echo is setting up Dalian IP, please wait a moment ...
netsh interface ip set address name= "Local Area Connection" Source=static addr=10.15.100.86 mask=255.255.0.0
Echo is setting up the Dalian Gateway, please wait a moment ...
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 a moment ...
netsh interface ip set DNS name= "Local Area Connection" Source=static addr=10.15.0.1 register=primary
Echo is setting up Dalian standby DNS, please wait a moment ...
netsh interface ip add DNS name= "Local Area Connection" addr=10.100.1.2 index=2
Echo is setting up Dalian main wins, please wait a moment ...
netsh interface ip set wins name= "Local Area Connection" Source=static addr=10.15.0.1
Echo is setting up a standby wins in Dalian, please wait a moment ...
netsh interface ip add wins name= "Local Area Connection" addr=10.100.1.2 index=2
echo Setup Complete!
echo is refreshing settings ...
Ipconfig/flushdns
Echo shows the new settings:
Ipconfig/all
Goto Dlproxy
:d Lproxy
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 is shutting down the browser:
taskkill/f/t/im IEXPLORE.exe
Echo is opening the browser
"C:\Program files\internet Explorer\iexplore. EXE "
Goto exit
: synet
Cls
Color 1f
Echo.
set/p choice= Enter "N" after the carriage return skips the NIC setting, direct return to continue the network card setting:
if/i "%choice%" = = "N" goto Proxy
Cls
Echo starts setting up Shenyang network address!
Echo is setting up Shenyang IP, please wait a moment ...
netsh interface ip set address name= "Local Area Connection" Source=static addr=10.16.100.86 mask=255.255.0.0
Echo is setting up Shenyang Gateway, please wait a moment ...
netsh interface ip set address name= "Local Area Connection" gateway=10.16.0.253 gwmetric=1
Echo is setting up Shenyang Master DNS, please wait a moment ...
netsh interface ip set DNS name= "Local Area Connection" Source=static addr=10.16.0.1 register=primary
Echo is setting up Shenyang standby DNS, please wait a moment ...
netsh interface ip add DNS name= "Local Area Connection" addr=10.100.1.2 index=2
Echo is setting up Shenyang Master wins, please wait a moment ...
netsh interface ip set wins name= "Local Area Connection" Source=static addr=10.16.0.1
Echo is setting up Shenyang standby wins, please wait a moment ...
netsh interface ip add wins name= "Local Area 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 is shutting down the browser:
taskkill/f/t/im IEXPLORE.exe
Echo is opening the browser
"C:\Program files\internet Explorer\iexplore. EXE "
Goto exit
: Exit
Cls
Echo.
Echo.
Echo.
Echo.
Echo.
Echo.
Echo.
Echo.
ECHO has completed all settings.
Echo.
Echo
Echo.
Echo.
Echo.
Echo.
Echo.
Echo.
Echo.
Echo.
Echo.
Pause
Exit

In this way, you do not have to set up a number of batch files, with a file to do more than one thing, why not!
Vi. Finally, how to use the VBS script to implement
Dalian Network configuration. vbs
Copy Code code 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 "is in the Dalian network Configuration"
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 Complete!" "

IE agent configuration. vbs
Copy Code code 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"

All of this has been introduced to you, whether it's a bat or a VBS, to achieve the functionality we want. In general, or according to their specific requirements to choose! When you execute the command again, be aware of the issue of permissions!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.