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

查看:110
本文介绍了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天全站免登陆