滥用 cURL 与 Redis 通信

新手上路,请多包涵

我想发送一个 PING 到 Redis 以检查连接是否正常,现在我可以安装 redis-cli ,但我不想和 curl 已经在那里了。那么我该如何滥用 curl 来做到这一点?基本上我需要关闭这里发送的内容:

 > GET / HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:6379
> Accept: */*
>
-ERR wrong number of arguments for 'get' command
-ERR unknown command 'User-Agent:'
-ERR unknown command 'Host:'
-ERR unknown command 'Accept:'

通过添加 -A "" User-Agent 但我找不到其他任何东西。知道我该怎么做吗?

原文由 Mahoni 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.2k
2 个回答

当你想使用 curl 时,你需要 REST over RESP,比如 webdis、tinywebdis 或 turbowebdis。见 https://github.com/markuman/tinywebdis#turbowebdis-tinywebdis–cherrywebdis

 $ curl -w '\n' http://127.0.0.1:8888/ping
{"ping":"PONG"}

如果没有 redis 的 REST 接口,您可以使用 netcat 例如。

 $ (printf "PING\r\n";) | nc <redis-host> 6379
+PONG

对于受密码保护的 redis,您可以像这样使用 netcat:

 $ (printf "AUTH <password>\r\n";) | nc <redis-host> 6379
+PONG

使用 netcat,您必须自己构建 RESP 协议。见 http://redis.io/topics/protocol

更新 2018-01-09

我已经构建了一个强大的 bash 函数,它可以通过 tcp 不惜一切代价 ping redis 实例

    function redis-ping() {
            # ping a redis server at any cost
            redis-cli -h $1 ping 2>/dev/null || \
                    echo $((printf "PING\r\n";) | nc $1 6379 2>/dev/null || \
                    exec 3<>/dev/tcp/$1/6379 && echo -e "PING\r\n" >&3 && head -c 7 <&3)
    }

用法 redis-ping localhost

原文由 Markus 发布,翻译遵循 CC BY-SA 4.0 许可协议

不是 curl,但不需要 HTTP 接口或 nc(非常适合没有安装 nc 的容器之类的东西)

exec 3<>/dev/tcp/127.0.0.1/6379 && echo -e "PING\r\n" >&3 && head -c 7 <&3

应该给你

+PONG

您可以从 这篇精彩的文章 中了解更多关于正在发生的事情。

原文由 Joel B 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题