Linux 替代 ping
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15959594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alternative to ping
提问by David542
I want to see if an Amazon EC2 instance is open, but by default Amazon EC2 blocks ping requests. There is a way to do this by changing the security policy of the instance. Excluding that, what would be the closest alternative to "see if a server is responsive" and is light-weight?
我想查看 Amazon EC2 实例是否已打开,但默认情况下 Amazon EC2 会阻止 ping 请求。有一种方法可以通过更改实例的安全策略来做到这一点。排除这一点,“查看服务器是否响应”并且是轻量级的最接近的替代方案是什么?
Is curl
a good option?
是curl
个好选择吗?
采纳答案by cnicutar
I think curl
is exactly what you want. Like all well-behaved unix programs, it returns an error code if anything goes wrong:
我认为curl
这正是你想要的。像所有表现良好的 unix 程序一样,如果出现任何问题,它会返回一个错误代码:
[cnicutar@ariel ~]$ curl www.no-such-website.com
[cnicutar@ariel ~]$ echo $?
6
Also you may want to use --connect-timeout
to make sure it doesn't wait forever.
此外,您可能希望使用--connect-timeout
它来确保它不会永远等待。
回答by jman
CURL won't work as a quick check if the server is up and the web server daemon is down. One alternative is to send a TCP ACK with tools like hping3
. If you get a RST, the server is UP.
如果服务器已启动且 Web 服务器守护程序已关闭,则 CURL 将无法快速检查。一种替代方法是使用诸如hping3
. 如果您获得 RST,则服务器已启动。
hping3 -c 1 -V -p 80 -s 5050 -A example.fqdn
What you could do is try the CURL test first and on failure try the second method to confirm that the server is down and not just the web server daemon.
您可以先尝试 CURL 测试,如果失败,请尝试第二种方法以确认服务器已关闭,而不仅仅是 Web 服务器守护程序。
The problem with this method is that a stray TCP packet like this could be filtered by an intermediate proxy.
这种方法的问题在于,像这样的流浪 TCP 数据包可能会被中间代理过滤掉。
回答by Lars Hansson
If you can ssh to it then it's up. A simple way of checking this with nc is: nc -zv 111.222.333.444 22 (Replace 111.222.333.444 with the ip address of your instance)
如果您可以通过 ssh 连接到它,那么就可以了。使用 nc 进行检查的一种简单方法是: nc -zv 111.222.333.444 22 (将 111.222.333.444 替换为您实例的 IP 地址)