滥用cURL与Redis沟通

Mah*_*oni 25 linux curl tcp http redis

我想发送一个PINGRedis来检查连接是否正常,现在我可以安装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:'
Run Code Online (Sandbox Code Playgroud)

User-Agent完全可以通过添加完全摆脱它-A "",但我找不到其他任何东西.知道我怎么能这样做吗?

Mar*_*kus 42

当你想使用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"}
Run Code Online (Sandbox Code Playgroud)

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

$ (printf "PING\r\n";) | nc localhost 6379 
+PONG
Run Code Online (Sandbox Code Playgroud)

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

更新2018-01-09

我已经构建了一个强大的bash函数,它可以不惜任何代价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)
    }
Run Code Online (Sandbox Code Playgroud)

用法 redis-ping localhost

  • 只是`回声PING | nc localhost 6379`对我来说很好. (16认同)
  • 整个printf的东西对我没有任何作用,但当我刚刚运行nc命令并在PING中输入时,我得到了我的PONG. (2认同)

Joe*_*l B 35

不卷曲,但不需要HTTP接口或nc(非常适用于没有安装nc的容器)

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

应该给你

+PONG

您可以阅读更多关于这篇精彩文章的内容.

  • 真棒!这只是我的一天 (4认同)
  • @EricHu`3 <>`打开文件描述符3作为读写文件`/ dev/tcp/127.0.0.1/6379`然后`>&3`将stdout从echo重定向到FD3和`<&3`重定向stdin从FD3到头部. (3认同)

Pet*_*r M 15

我需要为@Markus提供的nc添加一个睡眠,以使其从远程系统工作:

(printf "PING\r\n"; sleep 1) | nc remote.redis.hostname 6379
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅请求/响应协议和RTT:Redis管道.

  • 在 AWS EC2 到 AWS ElastiCache Redis 上,“sleep 1”对我来说是必要的,尽管“echo PING”与“printf”一样有效 (2认同)

Den*_*sel 7

您还可以使用telnet localhost 6379,如果连接成功,请键入ping

外出使用quit


小智 6

为了仅检查主机是否已响应,我使用了以下命令:

echo "quit" | curl -v telnet://HOST:PORT
Run Code Online (Sandbox Code Playgroud)

结果

*   Trying..
* TCP_NODELAY set
* Connected to ..
+OK
* Closing connection 0
Run Code Online (Sandbox Code Playgroud)

扩展解决方案netcat,如果您需要关闭连接,这对我有用:

(printf "AUTH <password>\r\nPING\r\nQUIT\r\n";) | nc HOST PORT
Run Code Online (Sandbox Code Playgroud)

输出

+OK
+PONG
+OK
Run Code Online (Sandbox Code Playgroud)