linux - 滥用cURL与Redis通信

标签 linux http curl tcp redis

我想向 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/

相关文章:

c++ - linux 上的 Stanford Stanford C++ 库

algorithm - 是否有一种算法可以在 Http 上工作,用于估计时钟偏差?

java - 为什么 Spring 优先处理 405 Http Error 而不是 406 Http Error

http - 如何使用带重定向的 Chrome 网络调试器

ruby - Sinatra 使用 curl 丢失了部分查询字符串

php - 使用 CURL 通过 SFTP 删除文件

php - 单击按钮自动登录另一个网站 - cURL 或 Javascript?

linux - phpunit.xml 的位置/路径

linux - "ASCII"模式下的SFTP上传

linux - TCP SYN/SYN ACK 的 IP header 中的 Dscp 值