检测某个网站是否启动的脚本?

Oma*_*med 3 bash scripts networking internet

我可以创建一个脚本来检测某个网站是否已启动然后执行某些操作吗?提前致谢。

L. *_*mes 8

是的。如果你运行这个脚本checksitestatus.sh并使用http://google.com它会说site is up. 如果你输入http://googlex.com它会说site is down

您必须lynx从存储库安装到:

$ sudo apt-get install lynx
Run Code Online (Sandbox Code Playgroud)

脚本(checksitestatus.sh):

#!/bin/bash

if [[ $# -eq 0 ]] ; then
    echo 'Missing parameter - site to check... exiting.'
    exit 0
fi

site=$1    
siteisdown=$(lynx -dump $site 2>&1 | egrep 'Alert!: Unable to connect to remote host.')

if [[ "$siteisdown" ]]
then
    echo "Site is down"
    # any other code here
else
    echo "Site is up"
    # any other code here
fi
Run Code Online (Sandbox Code Playgroud)