Ansible:检查服务是否在特定端口上侦听 [英] Ansible: Check if service is listening on a specific port

查看:1957
本文介绍了Ansible:检查服务是否在特定端口上侦听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何使用Ansible确认服务是否在特定端口上运行?

How would you go about using Ansible to confirm whether a service is running on a specific port?

例如:

  • Apache是​​否在端口80上运行?
  • MySQL是否正在侦听端口3912?
  • Tomcat是否正在侦听8080端口?

我知道有servicewait_for命令,它们分别检查服务是否正在运行以及端口是否在使用中-但到目前为止,我还没有发现任何东西可以检查特定的服务是否正在运行.在特定端口上侦听. servicewait_for将表示有一个服务和一个端口,但是不能保证该端口被该特定服务占用-它可以被任何东西占用.据我了解,wait_for只是检查它是否正在使用.

I understand that there are the service and wait_for commands, which individually check if a service is running and if a port is in use - but I've not found anything so far to check if a particular service is listening on a particular port. service and wait_for will indicate there's a service and a port, but there's no guarantee that the port is taken by that particular service - it could be taken by anything. wait_for, as I understand it, simply checks if it's being used.

wait_for上有一个regex_search参数,该参数提到在套接字连接中搜索特定的字符串,但是据我了解,这只是读取该套接字中的所有信息,而不是对所访问内容的任何访问权发送该信息.

There is a regex_search parameter on wait_for which mentions searching in a socket connection for a particular string, but as I understand it this is simply reading any information that comes down that socket rather than having any access to what is sending that information.

我们该怎么办?

推荐答案

有两种解释您的问题的方法,因此,我将尝试同时回答它们:

There are a couple of ways of interpreting your question, so I'm going to try to answer them both:

如果您的目标是验证特定端口是否正在为特定应用程序协议服务,我将通过运行适当的客户端进行检查.

If your goal is to verify that a particular port is serving a particular application protocol, I would check this by running an appropriate client.

  • 要检查Apache和Tomcat,我会GET一个特定的URL并检查结果代码.例如:

  • For checking Apache and Tomcat, I would GET a specific url and check the result code. For example:

- name: check if apache is running
  command: curl -sf http://webserver/check_url

对于Tomcat同样如此.

And similarly for Tomcat.

要检查MySQL,我将使用MySQL客户端:

For checking MySQL, I would use the MySQL client:

- name: check if mysql is running
  command: mysql -h dbhost -P dbport -e 'select 1'

如果您实际上想查看哪个进程使某个特定端口保持打开状态,那么我想您可以将ssgrep组合在一起,但这似乎很奇怪且不必要.像这样:

If you actually wanted to see what process was holding a particular port open, I guess you could combine ss and grep, but that seems weird and unnecessary. Something like:

    - name: check if httpd has port 80 open
      shell: ss -tp state listening sport = :80 | grep httpd

如果要检查特定的进程ID,可以使用与lsof类似的东西:

If you want to check a particular process id, you could so something similar with lsof:

    - name: check that pid {{apache_pid}} is listening on port 80
      shell: lsof -p 1036 -P | grep 'TCP \*:80'

但是,我并不一定会发现这些选项特别有用.前面部分中的服务检查似乎更合适.

But again, I don't necessarily find these options particularly useful. The service checks in the earlier section seem to be more appropriate.

这篇关于Ansible:检查服务是否在特定端口上侦听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆