获取redis主从复制链SHELL脚本
vi redisnode.sh
#!/bin/sh master_host=$1 master_port=$2 auth=$3 #判断输入密码是否为空,为空则转化为'' if test ! -n "$auth" -o $# -lt 3 then #echo " PASSWD IS NULL" auth='""' fi #把所有节点格式化保存到一个参数all_note all_note='' #获取redis 的master节点 function get_redis_master { master_host=$master_host master_port=$master_port count=0 while test $count -lt 5 do #判断输入的节点是否存在 comm="/usr/local/redis/bin/redis-cli -h $master_host -p $master_port -a $auth " redis_exist=`echo "INFO Replication"| $comm |wc -l 2>/dev/null` if [ $redis_exist -eq 0 ];then echo " $master_host:$master_port redis not exists or passwd error!" master_host='' master_port='' break fi echo "INFO Replication" |$comm |grep -q "role:master" if test X"$?" == X"0" ;then break fi comm="/usr/local/redis/bin/redis-cli -h $master_host -p $master_port -a $auth " master_host=`echo "INFO Replication" |$comm |grep "master_host"|cut -d: -f2 |sed 's/ //g'` master_port=`echo "INFO Replication" |$comm |grep "master_port"|cut -d: -f2 |sed 's/ //g'` count=$((count+1)) done all_note="${master_host}:${master_port}" #打印主节点 echo "MASTER NOTE: ${master_host}:${master_port} " } #获取给定节点的从节点 function get_redis_slave { master_host=$1 master_port=$2 comm="/usr/local/redis/bin/redis-cli -h $master_host -p $master_port -a $auth " slave_host=`echo "info Replication" | $comm |grep -Ei slave.:ip|awk -F= '{print $2,$3}' |awk -F, '{print $1,$2}'|awk '{print $1":"$3 }'|awk BEGIN{RS=EOF}'{gsub(/ /," ");print}'` } #获取给定节点的所有复制链 function get_all_redis_slave { master_host=$master_host master_port=$master_port count=2 all_host=$master_host:$master_port while test $count -lt 20 do condition=`echo $all_host |sed 's/ //g'` if test ! -n "$condition" ;then break fi for line in $all_host do tmpline=$tmpline" "$line master_host=`echo $line |awk -F: '{print $1}'` master_port=`echo $line |awk -F: '{print $2}'` get_redis_slave $master_host $master_port all_host=`echo $all_host |sed "s/$line//g"` all_host=$slave_host" "$all_host done all_host=`echo $all_host |sed "s/^ //g"` condition=`echo $all_host |sed 's/ //g'` if test ! -n "$condition" ;then #echo "" break fi #分行分级打印节点 echo "The ${count}TH NODE: $all_host " #一行打印所有节点 all_note="$all_note $all_host" count=$(($count+1)) done } get_redis_master get_all_redis_slave
sh redisnode.sh ip port password
有密码的redis要求密码主从一致。