当前位置: 移动技术网 > IT编程>网络>Dos/Bat > 批处理、VBS实现自动设置IP、默认网关、DNS、WINS、IE代理(全)

批处理、VBS实现自动设置IP、默认网关、DNS、WINS、IE代理(全)

2017年12月08日  | 移动技术网IT编程  | 我要评论
因为公司有同事负责大连、沈阳两个城市,经常在两地来回走动,到每个城市后,都要自己手动更改相应的网络配置,况且到外地时住的是酒店, 酒店上网是自动获得ip,又要将网络设置取消

因为公司有同事负责大连、沈阳两个城市,经常在两地来回走动,到每个城市后,都要自己手动更改相应的网络配置,况且到外地时住的是酒店, 酒店上网是自动获得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 自动设置代理服务器
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 代理服务器设置已经清空
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

五、以上配合结合,放在一个文件里,可以这样写:
网络综合配置.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,都可以实现我们想要的功能。总的来说,还是根据自己的特定要求来选择!再执行命令时,要注意权限的问题!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网