首页 > 解决方案 > 检查终端中的特定网站是否已启动?

问题描述

有没有一种简单的方法可以从控制台检查 Internet 连接?我正在尝试在 shell 脚本中玩耍。我似乎的一个想法是 wget --spider http://www.google.com/并检查 HTTP 响应代码以解释 Internet 连接是否工作正常。

这就是我正在尝试的:

#!/bin/bash

# Sending the output of the wget in a variable and not what wget fetches
RESULT=`wget --spider http://google.com 2>&1`
FLAG=0

# Traverse the string considering it as an array of words
for x in $RESULT; do
    if [ "$x" = '200' ]; then
        FLAG=1 # This means all good
    fi
done

有没有办法做到这一点?

标签: bashshellnetworking

解决方案


您可以使用pingorcurl命令来完成。检查man更多。


推荐阅读