页面“Expect基础”与“Redis批量删除key”之间的差异

来自linux中国网wiki
(页面间的差异)
跳到导航 跳到搜索
 
 
第1行: 第1行:
[[category:shell]]  
+
[[category:ops]] [[category:redis]]   
=Question=
 
有个项目要迁移 新来一批机器 要做初始化 于是抽空练了一下expect
 
  有可能有这个 警告 expect: spawn id exp4 not open 不过效果是没问题的
 
  
=利用expect批量添加pubkey=
+
[[Redis-bigkeys]]
==多台添加pubkey ==
+
=redis cluster 批量删除key=
 
<pre>
 
<pre>
cat main
+
#June 19 2020  有空结果一下原来的版本再优化一下
 +
cat redis_del_key.sh
 
#!/bin/bash
 
#!/bin/bash
for ip  in `cat list`
+
# Usage
 +
#redis_list=("host:post" "host:post")
  
do
+
redis_list=( "172.16.200.12:7003" "172.16.200.7:7004" "172.16.200.7:7005" "172.16.200.13:7000" "172.16.200.13:7001" "172.16.200.12:7002")
#echo $ip
+
#redis_list=( "172.16.200.13:7000" "172.16.200.13:7001" "172.16.200.12:7002")
./addkey  $ip
+
#redis_list=("172.16.200.7:7004")
 +
password="3636password"
  
done
+
for info in ${redis_list[@]}
 +
    do
 +
        echo "开始执行:$info" 
 +
        ip=`echo $info | cut -d : -f 1`
 +
        port=`echo $info | cut -d : -f 2`
  
 +
      for  loop in  `cat key.txt`
 +
        do
 +
        #echo $loop  |xargs -t -n1 redis-cli -c  -h $ip -p $port -a $password -c del
 +
        echo $loop  |xargs -t -n1 redis-cli -c  -h $ip -p $port -a $password -c keys
 +
        done
  
 +
    done
 +
    echo "success done完成"
  
cat addkey
+
# cat key.txt
#!/usr/local/bin/expect
+
infnce:wonderfulCache
#define var
+
ext_info
set timeout 17
 
# #<==接受第一个参数,赋值host
 
set host [lindex $argv 0]
 
set password "evan=="
 
  
  
#spawn  执行的命令在这里,这里用 添加 pub key 作为例子
 
spawn ssh-copy-id  -i /home/evan/lx/ssh/opspub root@$host
 
#spawn ssh-copy-id  -i /home/evan/.ssh/id_rsa.pub root@$host
 
#ssh-copy-id  -i /home/evan/.ssh/id_rsa.pub [email protected]
 
  
#expect 去掉交互,让他自动交互完成
+
#原来的版本
expect {                 
 
"*yes/no" { send "yes\r"; exp_continue} 
 
"*password:" { send "$password\r" }     
 
 
#send  "exit\r" 
 
expect eof
 
  
cat list
+
del hello_user_*
192.168.7.4
 
192.168.7.46
 
  
##上面用了copy-id  如果想用scp  etc
+
redis-cli keys
  
    #!/usr/bin/expect   
 
    set timeout 5   
 
    set hostno [lindex $argv 0]   
 
    spawn scp ~/.ssh/id_dsa.pub impala$hostno:~/.ssh/pub_key   
 
    expect "*password*"   
 
    send "111111\r"   
 
    spawn ssh impala$hostno "cat ~/.ssh/pub_key/ >> ~/.ssh/authorized_keys"   
 
    expect "*password*"   
 
    send "111111\r"   
 
    spawn ssh impala$hostno "chmod 600 ~/.ssh/authorized_keys"   
 
    expect "*password*"   
 
    send "111111\r"   
 
    expect eof   
 
  
(3)分析:
+
redis-cli keys "hello_user_*" | xargs redis-cli del
set可以设置超时,或者设置一个变量的值
 
spawn是执行一个命令
 
expect等待一个匹配的输出流中的内容
 
send是匹配到之后向输入流写入的内容
 
[lindex $argv 0]表示脚本的第0个参数
 
expect eof表示读取到文件结束符
 
  
</pre>
 
  
==多台修改ssh安全(远程执行命令和退出) ==
+
./redis-cli -h IP -p PORT -a PASSWORD keys 'key*' | xargs ./redis-cli -h IP -p PORT -a PASSWORD del
<pre>
 
  cat  run
 
#!/usr/local/bin/expect
 
  #define var
 
set timeout 17
 
set host [lindex $argv 0]
 
set password "evan=="
 
  
#spawn
+
redis-cli    -c  -h    172.31.22.218  -p 7000  -a pass keys 'hello_user_*'| xargs -i redis-cli    -c  -h    172.31.22.218  -p 7000  -a pass  del {}
spawn ssh root@$host
+
redis-cli    -c  -h    172.31.22.218  -p 7001  -a pass keys 'hello_user_*'| xargs -i redis-cli    -c  -h    172.31.22.218  -p 7001  -a pass  del {}
  
expect {
+
redis-cli    -c  -h    172.31.16.135  -p 7002  -a pass keys 'hello_user_*'| xargs -i  redis-cli    -c  -h    172.31.16.135  -p 7002 -a pass  del {}
 +
redis-cli    -c  -h    172.31.16.135  -p 7003  -a pass keys 'hello_user_*'| xargs  -i redis-cli    -c  -h    172.31.16.135  -p 7003 -a pass  del {}
  
     "*#*" { send "sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config\r";
+
redis-cli    -c  -h     172.31.23.43  -p 7004  -a pass keys 'hello_user_*'| xargs -i  redis-cli    -c  -h    172.31.23.43  -p 7004 -a pass  del {}
send "sed -i 's/^PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config\r"
+
redis-cli    -c  -h    172.31.23.43  -p 7005  -a pass keys 'hello_user_*'| xargs -i  redis-cli    -c  -h    172.31.23.43  -p 7005 -a pass del {}
send "systemctl restart sshd\r"
 
#send "\r"
 
    }
 
}
 
send "exit\r"  
 
expect eof
 
  
cat main
 
#!/bin/bash
 
for ip  in `cat list`
 
  
do
 
#echo $ip
 
#./addkey  $ip
 
./run  $ip
 
  
done
+
#!/bin/bash
 
+
redis_comm=/usr/local/redis-5.0.3/bin/redis-cli
#run
+
redis_ser01=192.168.50.175
bash main
+
redis_ser02=192.168.50.174
</pre>
+
$redis_comm -c -h $redis_ser01  -p 7001 -a xxx  keys $1 | xargs -i $redis_comm -c -h $redis_ser01  -p 7001 -a xxx del {}
 +
$redis_comm -c -h $redis_ser02  -p 7001 -a xxx  keys $1 | xargs -i $redis_comm -c -h $redis_ser02  -p 7001 -a xxx del {}
  
==单个==
+
运行:
<pre>
 
  
vi addkey
+
./redis_del_key.sh  匹配的key*
  
#!/usr/local/bin/expect
+
err  
#SERVERS="101.00.208.197 120.40.043.52 "
 
set timeout 5 
 
set host [lindex $argv 0]
 
spawn ssh-copy-id  -i /home/evan/.ssh/id_rsa.pub root@$host
 
#ssh-copy-id  -i /home/evan/.ssh/id_rsa.pub [email protected]
 
expect {                 
 
"*yes/no" { send "yes\r"; exp_continue} 
 
"*password:" { send "P2xMGipLpSG7dA==\r" }     
 
}  
 
  
expect eof
+
-i  {} 都是一定要的
  
 +
redis-cli -n 0 keys "test*" | xargs redis-cli -n 0 del
 +
(error) CROSSSLOT Keys in request don't hash to the same slot
  
用法
 
./addkey  18.80.216.19 #这是你的IP
 
 
</pre>
 
</pre>
  
=on dovo=
+
=see also=
<pre>
 
#!/usr/bin/expect
 
#good on new kali  这个在新的kali 上是ok的
 
set timeout 30
 
set sshIP "4.88.1.2"
 
set keypassword "ZdvV"
 
set rootPassword "5D"
 
#spawn ssh -o StrictHostKeyChecking=no -i /root/key  evan@$sshIP
 
expect "Enter passphrase"
 
send "$keypassword\r"
 
expect "]$"
 
send "sudo -i\r"
 
expect "xxxxx"
 
send "$rootPassword\r"
 
expect "]#"
 
## run command
 
#send "cat /root/1  && echo 'test was ok .';exit\r"
 
#send "bash /data/tmp/dbins  && echo 'dbins  was ok.';exit\r"
 
#expect "52wan"
 
#send "exit\r"
 
#expect eof {exit 0}
 
interact
 
  
  
#!/usr/bin/expect
+
[https://blog.csdn.net/yangyangye/article/details/100523387 redis集群批量删除模糊key shell脚本]
# on old kali  这个在老的kali
 
set timeout 30
 
set sshIP "4.88.1.2"
 
set keypassword "Zd"
 
set rootPassword "5D"
 
spawn ssh -o StrictHostKeyChecking=no -i /home/key  evan@$sshIP
 
expect "Enter passphrase"
 
send "$keypassword\r"
 
expect "52wan"
 
send "sudo -i\r"
 
expect "password for 52wan:"
 
send "$rootPassword\r"
 
expect "]#"
 
## run command
 
#send "cat /root/1  && echo 'test was ok .';exit\r"
 
#send "bash /data/tmp/dbins  && echo 'dbins  was ok.';exit\r"
 
#expect "52wan"
 
#send "exit\r"
 
#expect eof {exit 0}
 
interact
 
 
 
</pre>
 
 
 
==sudo addkey ==
 
<pre>
 
addkey
 
#!/bin/bash
 
#cat main evan  只能秀 sed  1a  不能和3a 会不成功
 
#for ip  in `cat li`
 
for ip  in `cat list`
 
 
 
do
 
 
 
ssh -i .key -o "StrictHostKeyChecking no"  centos@$ip  "sudo mkdir -p /root/.ssh"
 
ssh -i .key -o "StrictHostKeyChecking no" centos@$ip "sudo sed -i '1a ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAdfasfBAQDTR3R8Bz2mj lx' /root/.ssh/authorized_keys"
 
done
 
</pre>
 
 
 
=see also=
 
[https://www.cnblogs.com/jpinsz/p/10772750.html expect 分发ssh key脚本写得非常规范]
 
  
[https://www.cnblogs.com/lixigang/articles/4849527.html Shell脚本学习之expect命令完整学习]
+
[https://www.cnblogs.com/feng0520/p/11067025.html redis cluster集群批量删除中的key]
  
[https://www.cnblogs.com/changyaoguo/p/5764008.html ssh+expect批量分发]
+
[https://my.oschina.net/u/1255588/blog/1563672 Redis集群批量删除key]
  
 +
[https://blog.csdn.net/zj20142213/article/details/80879744  redis集群批量删除指定的key]
  
[https://www.cnblogs.com/zhaoyangjian724/p/3798006.html expect: spawn id exp4 not open]
+
https://blog.csdn.net/qq_36090419/article/details/80537684

2020年6月22日 (一) 11:37的版本


Redis-bigkeys

redis cluster 批量删除key

#June 19 2020   有空结果一下原来的版本再优化一下 
cat redis_del_key.sh
#!/bin/bash
# Usage 
#redis_list=("host:post" "host:post")

redis_list=( "172.16.200.12:7003" "172.16.200.7:7004" "172.16.200.7:7005" "172.16.200.13:7000" "172.16.200.13:7001" "172.16.200.12:7002")
#redis_list=( "172.16.200.13:7000" "172.16.200.13:7001" "172.16.200.12:7002")
#redis_list=("172.16.200.7:7004")
password="3636password"

for info in ${redis_list[@]}
    do
        echo "开始执行:$info"  
        ip=`echo $info | cut -d : -f 1`
        port=`echo $info | cut -d : -f 2`

      for  loop in  `cat key.txt`
         do
         #echo $loop  |xargs -t -n1 redis-cli -c  -h $ip -p $port -a $password -c del
         echo $loop  |xargs -t -n1 redis-cli -c  -h $ip -p $port -a $password -c keys 
         done

    done
    echo "success done完成"

# cat key.txt 
infnce:wonderfulCache
ext_info



#原来的版本

del hello_user_* 

redis-cli keys


 redis-cli keys "hello_user_*" | xargs redis-cli del


./redis-cli -h IP -p PORT -a PASSWORD keys 'key*' | xargs  ./redis-cli -h IP  -p PORT -a PASSWORD del

redis-cli    -c  -h    172.31.22.218   -p 7000  -a pass keys 'hello_user_*'| xargs -i redis-cli    -c  -h    172.31.22.218   -p 7000  -a pass  del {}
redis-cli    -c  -h    172.31.22.218   -p 7001  -a pass keys 'hello_user_*'| xargs -i redis-cli    -c  -h    172.31.22.218   -p 7001  -a pass  del {}

redis-cli    -c  -h    172.31.16.135  -p 7002  -a pass keys 'hello_user_*'| xargs -i  redis-cli    -c  -h    172.31.16.135   -p 7002 -a pass  del {}
redis-cli    -c  -h    172.31.16.135  -p 7003  -a pass keys 'hello_user_*'| xargs  -i redis-cli    -c  -h    172.31.16.135   -p 7003 -a pass  del {}

redis-cli    -c  -h     172.31.23.43   -p 7004  -a pass keys 'hello_user_*'| xargs -i  redis-cli    -c  -h    172.31.23.43   -p 7004 -a pass  del {}
redis-cli    -c  -h     172.31.23.43   -p 7005  -a pass keys 'hello_user_*'| xargs -i  redis-cli    -c  -h    172.31.23.43   -p 7005 -a pass  del {}



#!/bin/bash
redis_comm=/usr/local/redis-5.0.3/bin/redis-cli
redis_ser01=192.168.50.175
redis_ser02=192.168.50.174
$redis_comm -c -h $redis_ser01  -p 7001 -a xxx  keys $1 | xargs -i $redis_comm -c -h $redis_ser01  -p 7001 -a xxx del {}
$redis_comm -c -h $redis_ser02  -p 7001 -a xxx  keys $1 | xargs -i $redis_comm -c -h $redis_ser02  -p 7001 -a xxx del {}

运行:

./redis_del_key.sh  匹配的key*

err  

-i  {} 都是一定要的 

redis-cli -n 0 keys "test*" | xargs redis-cli -n 0 del
(error) CROSSSLOT Keys in request don't hash to the same slot

see also

redis集群批量删除模糊key shell脚本

redis cluster集群批量删除中的key

Redis集群批量删除key

redis集群批量删除指定的key

https://blog.csdn.net/qq_36090419/article/details/80537684