在 Linux 终端快速检测网站是否宕机的 6 个方法( 二 )


# wget -S --spider https://www.magesh.co.inSpider mode enabled. Check if remote file exists.--2019-11-15 01:22:00-- https://www.magesh.co.in/Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'Resolving www.magesh.co.in (www.magesh.co.in)… 104.18.35.52, 104.18.34.52, 2606:4700:30::6812:2334, …Connecting to www.magesh.co.in (www.magesh.co.in)|104.18.35.52|:443… connected.HTTP request sent, awaiting response… HTTP/1.1 200 OK Date: Thu, 14 Nov 2019 19:52:01 GMT Content-Type: text/html Connection: keep-alive Set-Cookie: __cfduid=db73306a2f1c72c1318ad4709ef49a3a01573761121; expires=Fri, 13-Nov-20 19:52:01 GMT; path=/; domain=.magesh.co.in; HttpOnly Vary: Accept-Encoding Last-Modified: Sun, 14 Jun 2015 11:52:38 GMT X-Cache: HIT from Backend CF-Cache-Status: DYNAMIC Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" Server: cloudflare CF-RAY: 535b85fe381ee684-LHRLength: unspecified [text/html]Remote file exists and could contain further links,but recursion is disabled -- not retrieving.如果你只想看 HTTP 状态码而不是返回的全部结果,用下面的 wget 命令:
# wget --spider -S "www.magesh.co.in" 2>&1 | awk '/HTTP// {print $2}' 200如果你想看一个网站是否宕机,用下面的 bash 脚本:
# vi wget-url-check.sh#!/bin/bashif wget --spider -S "https://www.google.com" 2>&1 | grep -w "200|301" ; then echo "Google.com is up"else echo "Google.com is down"fi当你把脚本内容添加到一个文件后,执行文件,查看结果:
# wget-url-check.shHTTP/1.1 200 OKGoogle.com is up如果你想看多个网站的状态,使用下面的 shell 脚本:
# vi curl-url-check-1.sh#!/bin/bashfor site in www.google.com google.co.in www.xyzzz.comdoif wget --spider -S "$site" 2>&1 | grep -w "200|301" ; then echo "$site is up"else echo "$site is down"fiecho "----------------------------------"done当你把上面脚本内容添加到一个文件后,执行文件,查看结果:
# sh wget-url-check-1.shHTTP/1.1 200 OKwww.google.com is up----------------------------------HTTP/1.1 301 Moved Permanentlygoogle.co.in is up----------------------------------www.xyzzz.com is down----------------------------------方法 5:使用 lynx 命令检测一个网站是否宕机lynx 是一个在 可寻址光标字符单元终端(cursor-addressable character cell terminals)上使用的基于文本的高度可配的 web 浏览器,它是最古老的 web 浏览器并且现在仍在活跃开发 。
# lynx -head -dump http://www.magesh.co.inHTTP/1.1 200 OKDate: Fri, 15 Nov 2019 08:14:23 GMTContent-Type: text/htmlConnection: closeSet-Cookie: __cfduid=df3cb624024b81df7362f42ede71300951573805662; expires=Sat, 14-Nov-20 08:14:22 GMT; path=/; domain=.magesh.co.in; HttpOnlyVary: Accept-EncodingLast-Modified: Sun, 14 Jun 2015 11:52:38 GMTX-Cache: HIT from BackendCF-Cache-Status: DYNAMICServer: cloudflareCF-RAY: 535fc5704a43e694-LHR如果你只想看 HTTP 状态码而不是返回的全部结果,用下面的 lynx 命令:
# lynx -head -dump https://www.magesh.co.in 2>&1 | awk '/HTTP// {print $2}' 200如果你想看一个网站是否宕机,用下面的 bash 脚本:
# vi lynx-url-check.sh#!/bin/bashif lynx -head -dump http://www.magesh.co.in 2>&1 | grep -w "200|301" ; then echo "magesh.co.in is up"else echo "magesh.co.in is down"fi当你把脚本内容添加到一个文件后,执行文件,查看结果:
# sh lynx-url-check.shHTTP/1.1 200 OKmagesh.co.in is up如果你想看多个网站的状态,使用下面的 shell 脚本:
# vi lynx-url-check-1.sh#!/bin/bashfor site in http://www.google.com https://google.co.in http://www.xyzzz.comdoif lynx -head -dump "$site" 2>&1 | grep -w "200|301" ; then echo "$site is up"else echo "$site is down"fiecho "----------------------------------"done当你把上面脚本内容添加到一个文件后,执行文件,查看结果:
# sh lynx-url-check-1.shHTTP/1.0 200 OKhttp://www.google.com is up----------------------------------HTTP/1.0 301 Moved Permanentlyhttps://google.co.in is up----------------------------------www.xyzzz.com is down----------------------------------方法 6:使用 ping 命令检测一个网站是否宕机ping 命令 (Packet Internet Groper)是网络工具的代表,用于在互联网协议(IP)的网络中测试一个目标主机是否可用/可连接 。通过向目标主机发送 ICMP 回应请求报文包并等待 ICMP 回应响应报文来检测主机的可用性 。它基于已发送的包、接收到的包和丢失了的包来统计结果数据,通常包含最小/平均/最大响应时间 。
# ping -c 5 2daygeek.comPING 2daygeek.com (104.27.157.177) 56(84) bytes of data.64 bytes from 104.27.157.177 (104.27.157.177): icmp_seq=1 ttl=58 time=228 ms64 bytes from 104.27.157.177 (104.27.157.177): icmp_seq=2 ttl=58 time=227 ms64 bytes from 104.27.157.177 (104.27.157.177): icmp_seq=3 ttl=58 time=250 ms64 bytes from 104.27.157.177 (104.27.157.177): icmp_seq=4 ttl=58 time=171 ms64 bytes from 104.27.157.177 (104.27.157.177): icmp_seq=5 ttl=58 time=193 ms--- 2daygeek.com ping statistics ---5 packets transmitted, 5 received, 0% packet loss, time 13244msrtt min/avg/max/mdev = 170.668/213.824/250.295/28.320 ms


推荐阅读