Batch file CMD-如何检查您是否已连接到internet?

Batch file CMD-如何检查您是否已连接到internet?,batch-file,cmd,Batch File,Cmd,我想检查我的计算机是否连接到网络,如果我连接或未连接,则打印bool语句。没有更多…您只需从目标网络ping一个IP地址并检查结果: ping -n 2 192.168.2.1 > nul if "%errorlevel%" == "0" goto connected echo Not Connected goto end :connected echo Connected :end   当然,您必须提供一个有效的IP地址来回复ping。另一种类似的方式: @echo off&

我想检查我的计算机是否连接到网络,如果我连接或未连接,则打印bool语句。没有更多…

您只需从目标网络ping一个IP地址并检查结果:

ping -n 2 192.168.2.1 > nul
if "%errorlevel%" == "0" goto connected
echo Not Connected
goto end

:connected
echo Connected

:end
 
当然,您必须提供一个有效的IP地址来回复ping。

另一种类似的方式:

@echo off&cls
ping -n 2 www.google.com >nul && echo connected || echo not connected

@CAMOBAP嗯,我只需要一个简单的批处理脚本,它将检查电脑与网络的连接,并打印出是否连接。没别的了……好吧,我试过了[ping-n2 google.com>nul&&echo connected&pause>nul | | echo not connected&pause>nul]。是的,当我连接到网络时,它显示已连接,但如果未连接,它将不输出任何内容…然后我尝试[…;echo not connected&pause>nul]。当我断开连接时,输出是正确的,但当我连接时,它同时输出“已连接”和“未连接”…谢谢!真的很有效!接下来的问题是,如果我已连接但无法访问互联网,该怎么办?我试过了,但它也说即使没有互联网接入,也已连接…@pajanustan只是用互联网上的IP地址替换IP地址。您还可以使用DNS名称,如
stackoverflow.com