我有一个通过共享服务器运行的Python脚本。此脚本直接运行的速度与用户的连接速度有关。在我们的办公室,Wifi速度非常慢,因此尝试运行此脚本非常慢。许多用户解释了启动脚本与实际显示无法启动之间的延迟,因此他们尝试再次打开它。
我想要一个警告框来显示用户是否连接到Wifi而不是以太网。我知道我可以通过检查ipconfig
手动确定这一点,但我想在运行我的脚本的批处理文件中执行此操作。
如何确定计算机是否连接到以太网或Wifi?
这是我到目前为止所拥有的:
CHECK IF WIFI
if WifiIsOn (
echo set WshShell = WScript.CreateObject("WScript.Shell") > %tmp%\tmp.vbs
echo WScript.Quit (WshShell.Popup( "You are connected to wifi, this may slow the execution of the program and cause bugs!" ,10 ,"Wifi Warning", 6)) >> %tmp%\tmp.vbs
cscript /nologo %tmp%\tmp.vbs
if %errorlevel%==11 (
del %tmp%\tmp.vbs
start script
)
if %errorlevel%==4 (
RECHECK WIFI
)
if %errorlevel%==2 (
del %tmp%\tmp.vbs
)
) else (
start script
)
对于好奇的人,我从James K的回答中得到了如何进行对话here
编辑:以下是ipconfig
显示的内容:
Windows IP Configuration
Wireless LAN adapter Wireless Network Connection 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Bluetooth Network Connection:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Wireless Network Connection:
Connection-specific DNS Suffix . : stuff
Link-local IPv6 Address . . . . . : stuff
IPv4 Address. . . . . . . . . . . : stuff
Subnet Mask . . . . . . . . . . . : stuff
Default Gateway . . . . . . . . . : stuff
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . : stuff
Link-local IPv6 Address . . . . . : stuff
IPv4 Address. . . . . . . . . . . : stuff
Subnet Mask . . . . . . . . . . . : stuff
Default Gateway . . . . . . . . . : stuff
Tunnel adapter isatap.stuff:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . : stuff
Tunnel adapter Local Area Connection* 13:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Tunnel adapter isatap.{stuff}:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Tunnel adapter isatap.{stuff}:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
答案 0 :(得分:2)
请尝试下面的代码。 我检查配置(ipconfig)。所以我设置了3种可能的WIFI状态:开,关,不可用(没有wifi适配器)。
@echo off
setlocal enabledelayedexpansion
set WIFIon=NA
set wifiadapter=false
for /f "tokens=* delims=" %%a in ('ipconfig') do (
echo %%a | find /i "Wireless" | find /i "adapter" | find /i "connection">nul
if "!errorlevel!"=="0" (
rem echo Found wireless adapter
set wifiadapter=true
) else if "!wifiadapter!"=="true" (
echo %%a | find /i "adapter" | find /i "connection">nul
if "!errorlevel!"=="0" (
rem echo Found other adapter adapter, Set wifi adapter =false
set wifiadapter=false
)
)
if "!wifiadapter!"=="true" (
rem echo checking line in WIFI region: %%a
echo %%a | find /i "Media State">nul
if "!errorlevel!"=="0" (
rem echo Found media state
echo %%a | find /i "Media disconnected">nul
if "!errorlevel!"=="0" (
rem echo WIFIon=false
set WIFIon=false
goto endcheck
) else (
rem echo WIFIon=true
set WIFIon=true
goto endcheck
)
)
echo %%a | find /i "IPv4 Address" | findstr ":.*[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*">nul
if "!errorlevel!"=="0" (
rem echo Found WIFI ip. set wifi=true
set WIFIon=true
goto endcheck
)
)
)
:endcheck
echo WiFiState: !WIFIon!
if "!WIFIon!"=="true" (
echo WIFI is on. Do other stuff here.
) else if "!WIFIon!"=="false" (
echo WIFI is off. Do other stuff here.
) else if "!WIFIon!"=="NA" (
echo No WIFI Adapter found. Do other stuff here.
)
答案 1 :(得分:1)
在尝试确定启动客户端备份是否有意义时(例如,设备连接到公司LAN),我遇到了这种情况。
那么,我们对LAN连接了解多少? Ping响应时间应始终为<1ms
。如果更高,则设备最有可能通过WIFI或VPN连接。如果我们没有给出响应时间,则目的地无法访问。
所以我最终分析了ping命令的输出,寻找字符串<1ms
。如果这不起作用,我将检查TTL=
,因为这对于Windows的所有本地化版本都是相同的,并且只有在得到响应时才会在输出中。
为了便于使用,我想出了以下脚本,它返回了您可以检查的三个不同的退出代码。
:: @example isUp [server]
::
:: @description Check if a server is available
::
:: @param server: FQDN or IP of the server in question
::
:: @return (exitcode) 0: The server is pingable in LAN
:: @return (exitcode) 1: The server is pingable, but with latency - probably in WLAN or VPN
:: @return (exitcode) 2: The server is not pingable
@echo off
set server=%1
set under1ms="^<1ms"
set timeout="TTL="
ping %server% -n 1 -4 | findstr /C:"%under1ms%" 1>nul
if errorlevel 1 (
ping %server% -n 1 -4 | findstr /C:"%timeout%" 1>nul
if errorlevel 1 ( exit /B 2 ) else ( exit /B 1 )
) else ( exit /B 0 )
编辑:忘了提到我从哪里偷走了我的片段。有关如何在字符串中查找子字符串,请参阅以下优秀答案:
https://stackoverflow.com/a/7218493/2532203