Vas*_*914 1 networking scripting shell-script ping bourne-shell
需要改进不断测试网站的脚本。
目前已使用以下脚本,但它提供了大量失败的电子邮件,而网站仍在运行:
#!/bin/bash
while true; do
date > wsdown.txt ;
cp /dev/null pingop.txt ;
ping -i 1 -c 1 -W 1 website.com > pingop.txt ;
sleep 1 ;
if grep -q "64 bytes" pingop.txt ; then
:
else
mutt -s "Website Down!" bruno.bvaraujo@gmail.com < wsdown.txt ;
sleep 10 ;
fi
done
Run Code Online (Sandbox Code Playgroud)
现在或以某种方式改进此脚本或使用另一种方式思考。
Ark*_*zyk 13
你不需要;在每一行的末尾,这不是 C。
你不需要:
cp /dev/null pingop.txt
Run Code Online (Sandbox Code Playgroud)
因为脚本中的下一行
ping -i 1 -c 1 -W 1 google.com > pingop.txt
Run Code Online (Sandbox Code Playgroud)
pingop.txt无论如何都会覆盖内容。如果我们在这里,ping如果您不打算发送或稍后处理它,您甚至不需要将输出保存到文件中,只需执行以下操作:
if ping -i 1 -c 1 -W 1 website.com >/dev/null 2>&1
then
sleep 1
else
mutt -s "Website Down!" bruno.bvaraujo@gmail.com < wsdown.txt
sleep 10
Run Code Online (Sandbox Code Playgroud)
回答您关于误报的问题 -ping可能不是测试网站是否启动的最佳方式。有些网站只是不响应 ICMP 请求,例如:
$ ping -i 1 -c 1 -W 1 httpbin.org
PING httpbin.org (3.222.220.121) 56(84) bytes of data.
--- httpbin.org ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
Run Code Online (Sandbox Code Playgroud)
然而,http://httpbin.org是了。如果您website.com在示例中使用,则很可能使用 HTTP/HTTPS 访问它,在这种情况下,请考虑使用curl -Is:
$ curl -Is "httpbin.org" >/dev/null 2>&1
$ echo $?
0
$ curl -Is "non-existing-domain-lalalala.com" >/dev/null 2>&1
$ echo $?
6
Run Code Online (Sandbox Code Playgroud)
OP 询问评论中ping和curl评论中的速度差异。如果您正在测试响应ping以下内容的网站,则没有太大区别:
$ time curl -Is google.com >/dev/null 2>&1
real 0m0.068s
user 0m0.002s
sys 0m0.001s
$ time ping -i 1 -c 1 -W 1 google.com
PING google.com (216.58.215.110) 56(84) bytes of data.
64 bytes from waw02s17-in-f14.1e100.net (216.58.215.110): icmp_seq=1 ttl=54 time=8.06 ms
--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 8.068/8.068/8.068/0.000 ms
real 0m0.061s
user 0m0.000s
sys 0m0.000s
Run Code Online (Sandbox Code Playgroud)
但在测试网站,不回应的时候ping则curl
不仅更加可靠,而且快于中国平安与-W你现在使用:
$ time ping -i 1 -c 1 -W 1 httpbin.org
PING httpbin.org (3.222.220.121) 56(84) bytes of data.
--- httpbin.org ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
real 0m1.020s
user 0m0.000s
sys 0m0.000s
$ time curl -Is httpbin.org >/dev/null 2>&1
real 0m0.256s
user 0m0.003s
sys 0m0.000s
Run Code Online (Sandbox Code Playgroud)