【问题标题】:Verify rabbitmq credentials are valid验证 rabbitmq 凭据是否有效
【发布时间】:2013-06-17 13:29:10
【问题描述】:

我想编写一个在部署后运行的简单冒烟测试,以验证 RabbitMQ 凭据是否有效。检查rabbitmq用户名/密码/虚拟主机是否有效的最简单方法是什么?

编辑: 最好使用 bash 脚本进行检查。或者,使用 Python 脚本。

【问题讨论】:

    标签: python rabbitmq


    【解决方案1】:

    由于您没有提供有关语言等的任何详细信息:

    您可以简单地向管理 API 发出 HTTP GET 请求。

    $ curl -i -u guest:guest http://localhost:15672/api/whoami
    

    RabbitMQ Management HTTP API

    【讨论】:

    • 区分"reason":"Login failed""reason":"Not management user",即使用户不是管理员,您也可以判断凭据是否有效
    【解决方案2】:

    这是一种使用 Python 进行检查的方法:

    #!/usr/bin/env python
    import socket
    from kombu import Connection
    host = "localhost"
    port = 5672
    user = "guest"
    password = "guest"
    vhost = "/"
    url = 'amqp://{0}:{1}@{2}:{3}/{4}'.format(user, password, host, port, vhost)
    with Connection(url) as c:
        try:
            c.connect()
        except socket.error:
            raise ValueError("Received socket.error, "
                             "rabbitmq server probably isn't running")
        except IOError:
            raise ValueError("Received IOError, probably bad credentials")
        else:
            print "Credentials are valid"
    

    【讨论】:

      【解决方案3】:

      你也可以试试rabbitmqctl

      rabbitmqctl authenticate_user username password
      

      并在 Bash 中检查返回码。

      【讨论】:

      • 在 Ubuntu 16.04 上它显示 Error: could not recognise command 和关于评论的帮助。 authenticate_user 命令有什么要求?
      • 这可能是您的 RabbitMQ 版本不是最新的。您能否检查rabbitmqctl --help 是否列出了命令authenticate_user
      【解决方案4】:

      使用 Python:

      >>> import pika
      >>> URL = 'amqp://guest:guest@localhost:5672/%2F'
      >>> parameters = pika.URLParameters(URL)
      >>> connection = pika.BlockingConnection(parameters)
      >>> connection.is_open
      True
      >>> connection.close()
      

      【讨论】:

        猜你喜欢
        • 2021-12-17
        • 1970-01-01
        • 1970-01-01
        • 2018-11-18
        • 1970-01-01
        • 1970-01-01
        • 2015-12-20
        • 2014-10-15
        • 1970-01-01
        相关资源
        最近更新 更多