如何使用bash测试Internet连接? [英] How to test an Internet connection with bash?

查看:88
本文介绍了如何使用bash测试Internet连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不ping通某些网站的情况下测试Internet连接? 我的意思是,如果有连接但站点已关闭怎么办?是否可以检查与世界的联系?

How can an internet connection be tested without pinging some website? I mean, what if there is a connection but the site is down? Is there a check for a connection with the world?

推荐答案

无ping

#!/bin/bash

wget -q --spider http://google.com

if [ $? -eq 0 ]; then
    echo "Online"
else
    echo "Offline"
fi

-q :静音模式

-蜘蛛:不了解,只需检查页面可用性

--spider : don't get, just check page availability

$?:shell返回码

0 :shell一切正常"代码

0 : shell "All OK" code

没有wget

#!/bin/bash

echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1

if [ $? -eq 0 ]; then
    echo "Online"
else
    echo "Offline"
fi

这篇关于如何使用bash测试Internet连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆