我想向 Redis 发送一个 PING
以检查连接是否正常,现在我可以安装 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
,但我找不到其他任何东西。知道我该怎么做吗?
最佳答案
当你想使用 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 接口(interface),例如您可以使用 netcat。
$ (printf "PING\r\n";) | nc <redis-host> 6379
+PONG
对于受密码保护的 redis,您可以像这样使用 netcat:
$ (printf "AUTH <password>\r\n";) | nc <redis-host> 6379
+PONG
使用 netcat,您必须自己构建 RESP 协议(protocol)。参见 http://redis.io/topics/protocol
更新2018-01-09
我已经构建了一个强大的 bash 函数,它可以通过 tcp 不惜一切代价对 redis 实例执行 ping 操作
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
关于linux - 滥用cURL与Redis通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33243121/