Shell脚本检查服务器是否可访问? [英] Shell script to check whether a server is reachable?
问题描述
我有5个Solaris服务器,分布在不同的位置.有时由于种种原因(由于网络问题或服务器本身突然关闭),无法从我的位置访问这些服务器中的某些.
I have 5 Solaris servers present across different locations. Sometimes some of these servers are not reachable from my location due to various reasons (either because of network problems or the server itself goes down suddenly).
因此,我想编写一个Bash shell脚本来检查它们是否可以访问.我尝试过的是:
So I would like to write a Bash shell script to check wether they are reachable. What I have tried is:
ssh ipaddress "uname -a"
设置了无密码认证.如果没有任何输出,我会生成一封邮件.
Password less authentication is set. If I don't get any any output I will generate a mail.
- 还有其他方法可以检查服务器的可达性吗?
- 哪个是最好的方法?
- 我尝试过正确吗?
推荐答案
您可以使用 ping -c4 $ ip_address
,其中 $ ip_address
是远程服务器的ip,解析输出以捕获成功的数据包和/或失败的数据包,然后使用 mail -s
通过电子邮件发送日志.
You can use ping -c4 $ip_address
where $ip_address
is the ip of your remote server and parse the output to capture the successful packets and/or failed packets and use mail -s
to send the log via email.
这是使您入门的基础,您可以在此基础上继续进行.
Here is something to get you started and you can build on it.
ping -c4 www.google.com | awk '/---/,0'
这将给出这样的输出-
[jaypal:~/Temp] ping -c4 www.google.com | awk '/---/,0'
--- www.l.google.com ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 36.638/36.870/37.159/0.196 ms
我检查了Solaris man-page
的 ping
.Solaris框上 ping
的输出是不同的.另外,在Linux上,您可以通过说明 -c
和数据包数量来限制数据包.在Solaris上,您必须-
I checked the Solaris man-page
for ping
. Output from ping
on Solaris box is different. Also, on Linux you limit the packets by stating -c
and number of packets. On Solaris you would have to do -
ping -s www.google.com 2 4
/usr/sbin/ping -s [-l | -U] [-adlLnrRv] [-A addr_family]
[-c traffic_class] [-g gateway [ -g gateway...]] [-
F flow_label] [-I interval] [-i interface] [-P tos] [-
p port] [-t ttl] host [data_size] [npackets]
^ ^
| |
---------------------------------------
不幸的是,我没有方便的solaris盒子来帮助您.
Unfortunately I don't have a solaris box handy to help you out with.
这篇关于Shell脚本检查服务器是否可访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!