From 075a47c631949867b1b20b04543b974d2981c7f8 Mon Sep 17 00:00:00 2001 From: Olaf Bohlen Date: Thu, 20 Apr 2017 13:58:47 +0200 Subject: [PATCH 1/2] be more portable, fixed authentication this patch fixes the problem when connecting to databases which require an authentication, we need to authenticate first, then select the database also this patch replaces head -c 1 with dd bs=1 count=1 to be more compatible to non-linux platforms also it removes status=noxfer, since the stderr is thrown away anyway and it would break with non-gnu dd --- redi.sh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/redi.sh b/redi.sh index 5798506..37e7a6f 100755 --- a/redi.sh +++ b/redi.sh @@ -32,12 +32,11 @@ function redis_read_bulk() { exit 1 fi - echo $(dd bs=1 count=$BYTE_COUNT status=noxfer <&$FILE_DESC 2>/dev/null) - dd bs=1 count=2 status=noxfer <&$FILE_DESC 1>/dev/null 2>&1 # we are removing the extra character \r + echo $(dd bs=1 count=$BYTE_COUNT <&$FILE_DESC 2>/dev/null) + dd bs=1 count=2 <&$FILE_DESC 1>/dev/null 2>&1 # we are removing the extra character \r } function redis_read() { - typeset -i FILE_DESC=$1 if [[ $# -eq 2 ]]; then @@ -48,7 +47,7 @@ fi while read -r socket_data do typeset first_char - first_char=$(printf %b "$socket_data" | head -c1) + first_char=$(printf %b "$socket_data" | dd bs=1 count=1 2>/dev/null) case $first_char in '+') @@ -169,14 +168,15 @@ fi exec {FD}<> /dev/tcp/"$REDIS_HOST"/"$REDIS_PORT" -redis_select_db "$REDIS_DB" >&$FD -redis_read $FD 1>/dev/null 2>&1 - +# do we need to authenticate first? if [[ ! -z $REDIS_PW ]]; then redis_compose_cmd "$REDIS_PW" >&$FD - redis_read $FD 1>/dev/null 2>&1 + redis_read $FD 1>/dev/null 2>&1 fi +redis_select_db "$REDIS_DB" >&$FD +redis_read $FD 1>/dev/null 2>&1 + if [[ ! -z $REDIS_GET ]]; then if [[ $REDIS_ARRAY -eq 1 ]]; then redis_get_array "$REDIS_GET" "$REDIS_ARRAY_RANGE" >&$FD From ca9f3776bd0243927487a407f8cbf917f7d64f7a Mon Sep 17 00:00:00 2001 From: Olaf Bohlen Date: Thu, 20 Apr 2017 16:01:34 +0200 Subject: [PATCH 2/2] added keysearch --- redi.sh | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/redi.sh b/redi.sh index 37e7a6f..69697f8 100755 --- a/redi.sh +++ b/redi.sh @@ -100,6 +100,11 @@ function redis_get_var() { printf %b "*2\r\n\$3\r\nGET\r\n\$${#REDIS_VAR}\r\n$REDIS_VAR\r\n" } +function redis_keys() { + typeset REDIS_VAR="$@" + printf %b "KEYS $REDIS_VAR\r\n" +} + function redis_set_var() { typeset REDIS_VAR="$1" shift @@ -125,7 +130,7 @@ function redis_set_array() { done } -while getopts g:s:r:P:H:p:d:ha opt; do +while getopts g:s:k:r:P:H:p:d:ha opt; do case $opt in p) REDIS_PW=${OPTARG} @@ -139,6 +144,9 @@ while getopts g:s:r:P:H:p:d:ha opt; do g) REDIS_GET=${OPTARG} ;; + k) + REDIS_KEY=${OPTARG} + ;; a) REDIS_ARRAY=1 ;; @@ -154,15 +162,15 @@ while getopts g:s:r:P:H:p:d:ha opt; do h) echo echo USAGE: - echo " $0 [-a] [-r ] [-s ] [-g ] [-p ] [-d ] [-H ] [-P ]" + echo " $0 [-a] [-r ] [-s ] [-g ] [-k ] [-p ] [-d ] [-H ] [-P ]" echo exit 1 ;; esac done -if [[ -z $REDIS_GET ]] && [[ -z $REDIS_SET ]]; then - echo "You must either GET(-g) or SET(-s)" >&2 +if [[ -z "$REDIS_GET" ]] && [[ -z "$REDIS_SET" ]] && [[ -z "$REDIS_KEY" ]]; then + echo "You must either GET(-g), SET(-s) or KEYS(-k)" >&2 exit 1 fi @@ -196,6 +204,14 @@ if [[ ! -z $REDIS_GET ]]; then exit 0 fi +if [[ ! -z "$REDIS_KEY" ]]; then + redis_keys "$REDIS_KEY" >&$FD + redis_read $FD + + exec {FD}>&- + exit 0 +fi + while read -r line do REDIS_TODO=$line