SSHD密钥登录脚本
batch_ssh_non_pwd_login.sh
# !/bin/bash SERVERS="master01" PASSWORD="123456" for SERVER in $SERVERS do yum -y install expect # apt-get -y install expect # 批量创建文件夹 echo 'no this dir and then will create it.' expect -c "set timeout -1; spawn ssh root@$SERVER mkdir -p /root/bin/ssh expect { *password:* {send -- $PASSWORD\r;exp_continue;} *(yes/no)* {send -- yes\r;exp_continue;} eof {exit 0;} }" expect -c "set timeout -1; spawn scp ./ssh_non_pwd_login.sh $SERVER:/root/bin/ssh expect { *password:* {send -- $PASSWORD\r;exp_continue;} *(yes/no)* {send -- yes\r;exp_continue;} eof {exit 0;} }" expect -c "set timeout -1; spawn ssh root@$SERVER /root/bin/ssh/ssh_non_pwd_login.sh expect { *password:* {send -- $PASSWORD\r;exp_continue;} *(yes/no)* {send -- yes\r;exp_continue;} eof {exit 0;} }" done
ssh_non_pwd_login.sh
# !/bin/bash SERVERS="node1 node2" PASSWORD=123456 auto_gen_ssh_key() { expect -c "set timeout -1; spawn ssh-keygen; expect { *(/root/.ssh/id_rsa)* {send -- \r;exp_continue;} *passphrase)* {send -- \r;exp_continue;} *again* {send -- \r;exp_continue;} 0*(y/n)* {send -- y\r;exp_continue;} *password:* {send -- $PASSWORD\r;exp_continue;} eof {exit 0;} }"; } auto_ssh_copy_id() { expect -c "set timeout -1; spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $1; expect { *(yes/no)* {send -- yes\r;exp_continue;} *password:* {send -- $2\r;exp_continue;} eof {exit 0;} }"; } auto_copy_id_to_all() { for SERVER in $SERVERS do auto_ssh_copy_id $SERVER $PASSWORD done } #apt-get -y install expect yum -y install expect auto_gen_ssh_key auto_copy_id_to_all
继续阅读

我的微信
这是我的微信扫一扫
评论