由于公司网络使用DHCP,而自己家里是用静态IP的,每天手动切换很是麻烦,所以写了个批处理来切换网络连接。
公司的无线网络连上后,还要做web认证;家里网络连上后,还要连vpn,脚本顺便把这些都做了,直接开机启动即可。
wlan_config.bat:
@ECHO OFF @rem 家庭无线名称 set home=xxx @rem 公司无线名称 set office=yyy echo detecting current wlan config... setlocal enabledelayedexpansion netsh wlan show networks | findstr /C:"%home%" > nul IF !errorlevel! equ 0 ( ipconfig /all | findstr /C:"192.168.0.100" > nul IF !errorlevel! equ 0 ( echo already static ip, exit... ) ELSE ( echo use static ip... call %~dp0home.bat echo connect %home%... netsh wlan connect %home% ) echo connect vpn... call %~dp0vpn.bat echo done ) ELSE ( netsh wlan show networks | findstr /C:"%office%" > nul IF !errorlevel! equ 0 ( echo use dhcp... call %~dp0dhcp.bat echo connect %office%... netsh wlan connect %office% call %~dp0auth.bat echo done ) ELSE ( echo unknow wlan, use dhcp default... call %~dp0dhcp.bat echo done pause ) )
其中用到了home.bat,dhcp.bat,vpn.bat,auth.bat,如果不需要执行某个bat,去掉调用的那句就可以了。
这些bat都要放在相同目录下。
home.bat(设置静态IP):
netsh interface ip set address "无线网络连接" static 192.168.0.100 255.255.255.0 192.168.0.1 1 netsh interface ip set dns "无线网络连接" static 202.101.172.35 netsh interface ip add dns "无线网络连接" 202.101.172.47
dhcp.bat(设置为DHCP):
netsh interface ip set address name="无线网络连接" source=dhcp netsh interface ip set dns name="无线网络连接" source=dhcp register=PRIMARY
vpn.bat(vpn拨号):
rasdial vpn username pwd
auth.bat(web认证,这个脚本需要cygwin才能运行,因为用了curl):
@echo off d: chdir d:\cygwin\bin curl -d "username=xxx&password=yyy" http://wireless-gateway.netease.com/login.html >nul
最后在启动里创建一个快捷方式指向wlan_config.bat就可以了。