前言
网上有很多开wifi的批处理,不能实现自动设置共享,后来出现了VBS脚本辅助的,但是,要预先改好网络接口名才行。
这是多次改进的版本,修正了许多问题,实现了全自动选择网络接口,打开后一个回车就可以自动选择网络接口创建出热点!
批处理方式首次实现,之前博客打不开,先放无忧论坛了,bat吧也发了一次,看到过的就不用提醒了。
将下面的 bat批处理脚本 内容复制到 记事本 中保存为: WiFi热点设置工具bat_v3.7.3.bat
运行即可,如果是administrator登录的,可能出现循环弹cmd窗口现象,请右键选择“以管理员权限打开”。
bat批处理脚本
@ECHO OFF
Title WiFi热点设置工具 v3.7.3 [By 若水]
setlocal ENABLEDELAYEDEXPANSION
mode con: cols=65 lines=25
color 0a
:: 批处理获取管理员权限
:-------------------------------------
%1 mshta vbscript:createobject("shell.application").shellexecute("""%~0""","::",,"runas",1)(window.close)&&exit /b
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
set selcet=1
GOTO ICSvbs
:MENU
cls
ECHO.
ECHO. =-= WiFi热点设置工具 v3.7.3 =-=
ECHO. [支持Win7、Win8、Win10]
ECHO. [2017.3.3]
ECHO. ┏━━━━━━━━━━━━━━━━━━━━━━━━┓
ECHO.
ECHO. 1 开启 热点 自动 3 查看 热点 状态
ECHO. 2 开启 热点 手动 4 关闭 热点
ECHO.
ECHO. 5 更改 热点 设置 7 网络连接控制中心
ECHO. 6 查看 热点 设置 0 退 出
ECHO.
ECHO. ┗━━━━━━━━━━━━━━━━━━━━━━━━┛
ECHO.
Set /p selcet=请输入数字并按 [Enter] 确定(默认为:%selcet%):
SET selcet=%selcet:~0,1%
If "%selcet%"=="1" set "auto=true" && Goto wifi_start
If "%selcet%"=="2" set "auto=false" && Goto wifi_start
If "%selcet%"=="3" Goto wifi_status
If "%selcet%"=="4" Goto wifi_stop
If "%selcet%"=="5" Goto wifi_config
If "%selcet%"=="6" Goto wifi_show_config
If "%selcet%"=="7" set "selcet=3" && ncpa.cpl
If "%selcet%"=="0" Goto exit
GOTO MENU
:wifi_start
set selcet=3
cls
ECHO. =-=-=-=-=开启无线承载网络=-=-=-=-=
echo.
echo 1.检测 Internet访问
set lost=false
set pingurl=114.114.114.114
FOR /F "usebackq tokens=1 delims= " %%i in ( `ping -n 3 -w 999 %pingurl%^|find "(100%"` ) do (
if not %%i=="" ( set lost=true
)
)
if %lost%==true (
echo 无Internet访问,请连接Internet。
ping -n 5 -w 999 127.0.0.1 >nul 2>nul
GOTO wifi_start
)
echo 2.检测 承载网络支持
set Support=false
FOR /F "usebackq tokens=1,2,3 delims= " %%i in ( `netsh wlan show drivers ^| find "支持的承载网络"` ) do (
if "%%k"=="否" ( echo 发现一块无线网卡 不支持 “承载网络”!
)
if "%%k"=="是" ( echo 发现一块无线网卡 支持 “承载网络”!
set Support=true
)
)
if Support==false ( pause && GOTO MENU
)
ECHO.
echo 3.开启 必要服务
FOR /D %%i in ( "icssvc" "ALG" "BFE" "MpsSvc" "LanmanServer" "LanmanWorkstation" "Browser" "Dhcp" "RpcLocator" "Dnscache" "FDResPub" "upnphost" "SSDPSRV" "lmhosts") do (
set "Adapter=%%i"
sc config !Adapter! start= AUTO>nul
sc start !Adapter!>nul
)
:wan_config_auto
netsh wlan stop hostednetwork>nul
netsh wlan set hostednetwork mode=disallow>nul
ping -n 3 -w 999 127.0.0.1 >nul 2>nul
if %auto%==false goto wan_config
echo 4.查询 Internet网络接口
set WAN=""
set lost=""
set /a n=1
:wan_config_auto_re
FOR /D %%i in ( "PPP 适配器" "以太网适配器" "无线局域网适配器" ) do (
set "Adapter=%%i"
if %%i=="PPP %适配器" (
FOR /F "usebackq tokens=2* delims= " %%j in (`ipconfig^|find !Adapter!`) do set "Adapter=%%k") else (
FOR /F "usebackq tokens=1* delims= " %%j in (`ipconfig^|find !Adapter!`) do set "Adapter=%%k")
FOR /F "usebackq tokens=3 delims= " %%l in (`netsh interface ipv4 show address "!Adapter:~0,-1!"^|find "IP"`) do (
FOR /F "usebackq tokens=* delims=" %%m in (`ping -n 3 -w 999 -S %%l %pingurl%^|find "往返"`) do ( set "lost=%%m")
if not !lost!=="" (
set WAN="!Adapter:~0,-1!"
echo 发现:!Adapter:~0,-1!
goto startap
)
)
)
echo 第%n%次未找到
if %WAN%=="" (
if %n%==4 (
set auto=false
echo 没有找到联网的网络接口,请尝试手动指定。
ping -n 3 -w 999 127.0.0.1 >nul 2>nul
GOTO wifi_start
) else (
set /a n=%n%+1
GOTO wan_config_auto_re
)
)
:wan_config
echo 4.选择 Internet网络接口:
echo 确认请输入:y,否则直接按 [Enter] 键。
ECHO.
:wan_config_re
set WAN=""
FOR /F "usebackq tokens=2* delims= " %%i in ( `ipconfig^|find "PPP 适配器"` ) do (
set WANt=%%j
set /p a=是否选择 “!WANt:~0,-1!” :
if !a!==y ( set WAN="!WANt:~0,-1!"
goto startap
)
)
FOR /F "usebackq tokens=1* delims= " %%i in ( `ipconfig^|find "以太网适配器"` ) do (
set WANt=%%j
set /p a= 是否选择 “!WANt:~0,-1!” :
if !a!==y ( set WAN="!WANt:~0,-1!"
goto startap
)
)
FOR /F "usebackq tokens=1* delims= " %%i in ( `ipconfig^|find "无线局域网适配器"` ) do (
set WANt=%%j
set /p a= 是否选择 “!WANt:~0,-1!” :
if !a!==y ( set WAN="!WANt:~0,-1!"
goto startap
)
)
if %WAN%=="" ( goto wan_config_re )
:startap
ECHO.
echo 5.启动 无线承载网络
netsh wlan set hostednetwork mode=allow>nul
netsh wlan start hostednetwork>nul 2>nul
if not "%errorlevel%"=="0" (
FOR /D %%i in ( "无线网络连接" "WLAN" ) do (
set "Adapter=%%i"
echo 开启失败,尝试重置无线网卡,以便顺利开启热点。
FOR /F "usebackq tokens=1 delims=," %%i in ( `getmac /v /fo csv^|find !Adapter!` ) do (
netsh interface set interface %%i disabled>nul 2>nul
netsh interface set interface %%i enabled>nul 2>nul
)
)
goto wifi_start
)
echo 6.查询 Microsoft承载网络接口
set LAN=""
FOR /F "usebackq tokens=1 delims=," %%i in ( `getmac /v /fo csv^|find "Microsoft"` ) do (
set LAN=%%i
)
echo 发现:%LAN%
ECHO.
echo 7.设置 Internet连接共享: %WAN%---^>%LAN%
netsh interface ip set address name=%LAN% source=DHCP>nul
netsh interface ip set dnsservers name=%LAN% source=DHCP>nul
cscript /nologo %temp%\ICS.vbs "off"
for /f "delims=" %%i in ('cscript /nologo %temp%\ICS.vbs %WAN% %LAN%') do echo %%i
ECHO.
echo 8.验证 Internet连接共享
echo 等待设置生效,大约5s后开始验证。
ping -n 5 -w 999 127.0.0.1 >nul 2>nul
set lost=""
FOR /F "usebackq tokens=3 delims= " %%i in (`netsh interface ipv4 show address %LAN%^|find "IP"`) do (
FOR /F "usebackq tokens=* delims=" %%j in (`ping -n 3 -w 999 -S %%i %pingurl%^|find "往返"`) do set "lost=%%j"
)
if %lost%=="" (
echo Internet连接共享设置失败!
ECHO.
echo 尝试关闭无线承载网络,并再次开启。
echo 关闭ICS共享
cscript /nologo %temp%\ICS.vbs "off"
ECHO.
netsh wlan stop hostednetwork
netsh wlan set hostednetwork mode=disallow
goto wifi_start
) else (
echo Internet连接共享设置成功!)
ECHO.
pause
GOTO MENU
:wifi_status
cls
set selcet=3
ECHO. =-=-=-=-=无线承载网络状态=-=-=-=-=
ECHO.
echo 承载网络状态
echo -----------------------
netsh wlan show hostednetwork|find "模式"
netsh wlan show hostednetwork|find " 状态"
netsh wlan show hostednetwork|find " 客户端数"
ECHO.
echo 已连接客户端
echo -----------------------
set "ip=---------------"
set "mac=-----------------"
FOR /F "usebackq tokens=1-6 delims=: " %%a in ( `netsh wlan show hostednetwork ^| find "已经过身份验证"` ) do (
set "mac=%%a-%%b-%%c-%%d-%%e-%%f"
FOR /F "usebackq tokens=1 delims=: " %%i in ( `ARP -A^|find "!mac!"` ) do (
set "ip=%%i"
)
)
echo IP地址 MAC地址
echo %ip% %mac%
ECHO.
ECHO.
pause
GOTO MENU
:wifi_stop
cls
set selcet=0
ECHO. =-=-=-=-=关闭无线承载网络=-=-=-=-=
ECHO.
echo 关闭ICS共享
cscript /nologo %temp%\ICS.vbs "off"
ECHO.
netsh wlan stop hostednetwork
netsh wlan set hostednetwork mode=disallow
pause
GOTO MENU
:wifi_config
cls
set selcet=1
ECHO. =-=-=-=-=配置无线承载网络=-=-=-=-=
ECHO.
echo 第一步:输入新SSID Enter结束
echo.
set /p SSID=请输入新SSID:
if "%SSID:~1,1%"=="" echo 最少输入2位&goto begin
echo.
echo.
echo 第二步:输入8位以上新密码 Enter结束
echo.
set /p pw=请输入密码(8-16位):
if "%pw:~7,1%"=="" echo 最少输入8位&goto key
if "%pw:~16,1%" neq "" echo 超过16位了&goto key
netsh wlan set hostednetwork ssid=%SSID% key=%pw%
pause
GOTO MENU
:wifi_show_config
cls
set selcet=3
ECHO. =-=-=-=-=无线承载网络设置=-=-=-=-=
ECHO.
echo 承载网络设置
echo -----------------------
netsh wlan show hostednetwork|find "名称"
netsh wlan show hostednetwork setting=security|find "用户安全密钥 "
netsh wlan show hostednetwork setting=security|find "身份验证"
netsh wlan show hostednetwork|find "最多客户端数"
ECHO.
pause
GOTO MENU
:ICSvbs
if exist "%temp%\ICS.vbs" ( del /f /a /q "%temp%\ICS.vbs" )
(
echo '使用方法
echo 'cscript /nologo %%^~dp0\ics.vbs "本地连接" "无线网络连接 2"
echo 'cscript /nologo %%^~dp0\ics.vbs "off"
echo Option Explicit
echo Dim ICSSC_DEFAULT,CONNECTION_PUBLIC,CONNECTION_PRIVATE,CONNECTION_ALL
echo Dim NetSharingManager,PublicConnection, PrivateConnection,EveryConnectionCollection
echo ICSSC_DEFAULT = 0
echo CONNECTION_PUBLIC = 0 '公有网络接口
echo CONNECTION_PRIVATE = 1 '私有网络接口
echo CONNECTION_ALL = 2 '全部网络接口
echo Dim objArgs
echo Dim priv_con,publ_con,switch
echo Dim Reinfo
echo Main^(^)
echo Wscript.quit
echo '主函数
echo Function Main^(^)
echo Dim bReturn
echo bReturn = "false"
echo Set objArgs = Wscript.Arguments
echo If objArgs.Count = 2 Then
echo publ_con = objArgs^(0^)
echo priv_con = objArgs^(1^)
echo switch = "on"
echo ICSshare publ_con,priv_con,switch
echo ElseIf objArgs.Count = 1 Then
echo switch = objArgs^(0^)
echo ICSshare bReturn,bReturn,switch
echo End If
echo Main = bReturn
echo End Function
echo Private Function ICSshare^(ByVal sPublic, ByVal sPrivate, ByVal bEnable^)
echo On Error Resume Next
echo Dim NetSharingManager,EveryConnectionCollection, Item, EveryConnection, objNCProps
echo set NetSharingManager = Wscript.CreateObject^("HNetCfg.HNetShare.1"^)
echo set EveryConnectionCollection = NetSharingManager.EnumEveryConnection
echo for each Item in EveryConnectionCollection
echo set EveryConnection = NetSharingManager.INetSharingConfigurationForINetConnection^(Item^)
echo set objNCProps = NetSharingManager.NetConnectionProps^(Item^)
echo If objNCProps.name = sPublic And bEnable = "on" Then
echo EveryConnection.EnableSharing CONNECTION_PUBLIC
echo WSH.echo " 已设置WAN接口:“" ^& objNCProps.name ^& "”"
echo ElseIf objNCProps.name = sPrivate And bEnable = "on" Then
echo EveryConnection.EnableSharing CONNECTION_PRIVATE
echo WSH.echo " 已设置LAN接口:“" ^& objNCProps.name ^& "”"
echo ElseIf bEnable = "on" Then
echo EveryConnection.EnableSharing CONNECTION_ALL
echo ElseIf bEnable = "off" Then
echo EveryConnection.DisableSharing
echo End If
echo Next
echo End Function
)>%temp%\ICS.vbs
GOTO MENU
:Exit
cls
del /f /a /q "%temp%\ICS.vbs"