Mah*_*oni 25 linux curl tcp http 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:'
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
我已经构建了一个强大的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
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管道.
小智 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)